using System;
using System.Collections.Generic;
using System.Web;
using TABS.MailCarrier.WebSDK.Board;

/// <summary>
/// 게시글 목록을 GridView에 출력하기 위한 DataSource 클래스
/// </summary>
public class ArticlesDataSource
{
    private const int MAX_FIXED_ARTICLES = 5;       // 최대 고정글은 5개로 제한
    private int _totalCount;

    public ArticlesDataSource()
    {
    }

    public Article[] GetArticles(string board, int maximumRows, int startRowIndex)
    {
        int totalCount;
        Board boardObj = new Board(new Guid(board));
        Article[] articles = null;
        Article[] genealArticles = null;
        Article[] fixedArticles = boardObj.GetFixedTopArticles();
        if (fixedArticles.Length > MAX_FIXED_ARTICLES) Array.Resize(ref fixedArticles, MAX_FIXED_ARTICLES);       // 최대 고정글은 MAX_FIXED_ARTICLES개로 제한

        int needPageSize = maximumRows - fixedArticles.Length;
        genealArticles = boardObj.GetArticles(WebUser.CurrentUser.DomainUserID, startRowIndex / maximumRows, needPageSize, out totalCount);

        // 일반글 내에 출력되는 고정글에는 스타일을 지정하지 않기 위해 FixedTop 플래그 제거
        foreach (Article article in genealArticles)
            if ((article.Flag & Article.AttrFlag.FixedTop) > 0) article.Flag &= ~Article.AttrFlag.FixedTop;

        // 글 데이터 소스 생성
        articles = new Article[fixedArticles.Length + genealArticles.Length];
        Array.Copy(fixedArticles, articles, fixedArticles.Length);
        Array.Copy(genealArticles, 0, articles, fixedArticles.Length, genealArticles.Length);

        _totalCount = totalCount + fixedArticles.Length * (((totalCount - 1) / needPageSize) + 1);
        return articles;
    }

    public int GetTotalCount(string board)
    {
        return _totalCount;
    }

    public Article[] GetArticlesByGroupCode(string board, string groupCode, int maximumRows, int startRowIndex)
    {
        int totalCount;
        Board boardObj = new Board(new Guid(board));
        Article[] articles = null;
        Article[] genealArticles = null;
        Article[] fixedArticles = boardObj.GetFixedTopArticles();
        if (fixedArticles.Length > MAX_FIXED_ARTICLES) Array.Resize(ref fixedArticles, MAX_FIXED_ARTICLES);       // 최대 고정글은 MAX_FIXED_ARTICLES개로 제한

        int needPageSize = maximumRows - fixedArticles.Length;
        genealArticles = boardObj.GetArticlesByGroupCode(WebUser.CurrentUser.DomainUserID, startRowIndex / maximumRows, needPageSize, new Guid(groupCode), out totalCount);

        // 일반글 내에 출력되는 고정글에는 스타일을 지정하지 않기 위해 FixedTop 플래그 제거
        foreach (Article article in genealArticles)
            if ((article.Flag & Article.AttrFlag.FixedTop) > 0) article.Flag &= ~Article.AttrFlag.FixedTop;

        // 글 데이터 소스 생성
        articles = new Article[fixedArticles.Length + genealArticles.Length];
        Array.Copy(fixedArticles, articles, fixedArticles.Length);
        Array.Copy(genealArticles, 0, articles, fixedArticles.Length, genealArticles.Length);

        _totalCount = totalCount + fixedArticles.Length * (((totalCount - 1) / needPageSize) + 1);
        return articles;
    }

    public int GetTotalCount(string board, string groupCode)
    {
        return _totalCount;
    }

}