using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;
using TABS.MailCarrier.WebSDK.Mail;

/// <summary>
/// 메일함 폴더에 포함되어 있는 메시지 목록을 GridView에 출력하기 위한 데이터 소스 클래스입니다.
/// </summary>
public class MsgDataSource
{
    private int _totalCount;

    public MsgDataSource()
    {
    }

    public Message[] GetMessages(string folder, bool isNew, bool storePN, int maximumRows, int startRowIndex, string sortExpression)
    {
        MailboxFolder.SortColumn sortColumn = MailboxFolder.SortColumn.RecvTime;
        MailboxFolder.SortOrder sortOrder = MailboxFolder.SortOrder.Descending;
        string[] sortExpr = sortExpression.Split(' ');
        if (sortExpression.Length > 0 && sortExpr.Length == 1)
        {
            sortColumn = GetSortExpression(sortExpr[0]);
            sortOrder = MailboxFolder.SortOrder.Ascending;
        }
        else if (sortExpr.Length == 2)
        {
            sortColumn = GetSortExpression(sortExpr[0]);
            sortOrder = MailboxFolder.SortOrder.Descending;
        }

        Guid folderId = new Guid(folder);
        Message[] messages = null;
        if (folderId == MailUtil.AllMails || folderId == MailUtil.UnreadMails)
        {
            UserMailbox userMbox = new UserMailbox(WebUser.CurrentUser.DomainUserID);
            messages = userMbox.GetAllReceivedMessages(startRowIndex / maximumRows, maximumRows, sortColumn, sortOrder, isNew, storePN, out _totalCount);
        }
        else if (folderId == MailUtil.FlaggedMails)
        {
            UserMailbox userMbox = new UserMailbox(WebUser.CurrentUser.DomainUserID);
            messages = userMbox.GetAllFlaggedMessages(startRowIndex / maximumRows, maximumRows, sortColumn, sortOrder, 0, storePN, out _totalCount);
        }
        else
        {
            MailboxFolder mboxfolder = new MailboxFolder(folderId);
            messages = mboxfolder.GetMessagesEx(startRowIndex / maximumRows, maximumRows, sortColumn, sortOrder, storePN, out _totalCount);
        }
        return messages;
    }


    public int TotalNumberOfProducts(string folder, bool isNew, bool storePN)
    {
        return _totalCount;
    }

    private MailboxFolder.SortColumn GetSortExpression(string sortExpression)
    {
        if (sortExpression == "Priority")
        {
            return MailboxFolder.SortColumn.Priority;
        }
        else if (sortExpression == "AttachCount")
        {
            return MailboxFolder.SortColumn.Attachment;
        }
        else if (sortExpression == "FromName")
        {
            return MailboxFolder.SortColumn.FromName;
        }
        else if (sortExpression == "Subject")
        {
            return MailboxFolder.SortColumn.Subject;
        }
        else if (sortExpression == "RecvTime")
        {
            return MailboxFolder.SortColumn.RecvTime;
        }
        else if (sortExpression == "Size")
        {
            return MailboxFolder.SortColumn.MsgSize;
        }
        return MailboxFolder.SortColumn.RecvTime;
    }
}
