using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TABS.MailCarrier.WebSDK;
using TABS.MailCarrier.WebSDK.Mail;

public partial class Mail_Folders : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string defLang = WebUser.CurrentUser.UiLanguage;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(defLang);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(defLang);
        base.InitializeCulture();
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        WebUtil.SetMcCommonCss(Page.Header);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.Title = AppSetting.GetTitleInfo(Request).Title;

            UserMailbox userMbox = new UserMailbox(WebUser.CurrentUser.DomainUserID);
            long spaceAllowed = userMbox.SpaceAllowed;
            long spaceUsed = userMbox.SpaceUsed;

            UsageLabel.Text = string.Format("{0} / {1} ({2:p1})",
                WebUtil.GetFormattedFileSize(spaceUsed), WebUtil.GetFormattedFileSize(spaceAllowed), (double)spaceUsed / spaceAllowed);
        }
    }

    protected string GetIndent(int indent)
    {
        return (indent * 16).ToString();
    }

    protected string GetFolderInfo(Guid id, string folderName, int newMsgCount, int msgCount)
    {
        string info = string.Format("<img src='../Images/Mail/{0}' border='0' /> ", MailUtil.GetMailboxImageName(folderName)); ;
        if (id == MailUtil.ReadRecipients)
        {
            info += Resources.Mail.ReadRecipientsMailbox;
        }
        else if (id == MailUtil.MailToApprove)
        {
            info += Resources.Mail.MailToApprove;
            info += string.Format(" <span class='badge'>{0}</span>", msgCount);
        }
        else if (id == MailUtil.MailWaitForApproval)
        {
            info += Resources.Mail.MailWaitForApproval;
        }
        else if (folderName == MailUtil.AllMailsMailboxName || folderName == MailUtil.UnreadMailsMailboxName || folderName == MailUtil.FlaggedMailsMailboxName || folderName == MailUtil.ReadRecipientsMailboxName)
        {
            info += MailUtil.GetMailboxDisplayName(folderName);
        }
        else
        {
            if (newMsgCount > 0)
                info += string.Format("{0} (<span class='badge'>{1}</span>/{2})", MailUtil.GetMailboxDisplayName(folderName), newMsgCount, msgCount);
            else
                info += string.Format("{0} ({1}/{2})", MailUtil.GetMailboxDisplayName(folderName), newMsgCount, msgCount);
        }
        return info;
    }

    protected string GetFolderInfoEx(Guid id, string folderName, int newMsgCount, int msgCount, object objItems)
    {
        string info = MailUtil.GetMailboxFontIcon(folderName) + " ";
        if (id == MailUtil.ReadRecipients)
        {
            info += Resources.Mail.ReadRecipientsMailbox;
        }
        else if (id == MailUtil.MailToApprove)
        {
            Dictionary<string, string> items = (Dictionary<string, string>)objItems;
            // 승인할 메일
            info += Resources.Mail.MailToApprove;
            if (!items.ContainsKey("EmptyView")) info += string.Format(" <span class='badge'>{0}</span>", msgCount);
        }
        else if (id == MailUtil.MailToApprove_Delegate)
        {
            // 대리자 승인할 메일
            info += folderName;
        }
        else if (id == MailUtil.MailWaitForApproval)
        {
            // 승인 대기 메일
            info += Resources.Mail.MailWaitForApproval;
        }
        else if (id == MailUtil.AllMails || id == MailUtil.UnreadMails || id == MailUtil.FlaggedMails || id == MailUtil.ReadRecipients)
        {
            info += MailUtil.GetMailboxDisplayName(folderName);
        }
        else if (id == MailUtil.SpamNotification)
        {
            info += Resources.Mail.SpamNotifybox;
        }
        else
        {
            if (newMsgCount > 0)
                info += string.Format("{0} (<span class='badge'>{1}</span>/{2})", Server.HtmlEncode(MailUtil.GetMailboxDisplayName(folderName)), newMsgCount, msgCount);
            else
                info += string.Format("{0} ({1}/{2})", Server.HtmlEncode(MailUtil.GetMailboxDisplayName(folderName)), newMsgCount, msgCount);
        }
        return info;
    }

    protected string GetSpaceUsage(Guid id, long spaceUsed)
    {
        if (id == MailUtil.ReadRecipients || id == MailUtil.MailToApprove || id == MailUtil.MailWaitForApproval || id == MailUtil.SpamNotification ||
            id == MailUtil.AllMails || id == MailUtil.UnreadMails || id == MailUtil.FlaggedMails || id == MailUtil.MailToApprove_Delegate)
        {
            return "";
        }
        else
        {
            return WebUtil.GetFormattedFileSize(spaceUsed);
        }
    }

    protected string GetClickEventFunction(Guid id, string folderName, object objItems)
    {
        if (id == MailUtil.ReadRecipients)
        {
            return "location.href = 'ReadRecipients.aspx'";
        }
        else if (id == MailUtil.MailToApprove)
        {
            Dictionary<string, string> items = (Dictionary<string, string>)objItems;
            if (!items.ContainsKey("EmptyView"))
                return "location.href = 'PermitSend/ConfirmerList.aspx'";
            else
                return "return false";
        }
        else if (id == MailUtil.MailToApprove_Delegate)
        {
            Dictionary<string, string> items = (Dictionary<string, string>)objItems;
            return string.Format("location.href = 'PermitSend/ConfirmerList.aspx?delegate={0}'", items["DelegateID"]);
        }
        else if (id == MailUtil.MailWaitForApproval)
        {
            return "location.href = 'PermitSend/ConfirmedList.aspx'";
        }
        else if (id == MailUtil.SpamNotification)
        {
            return "window.open('../External/SpamList.aspx', 'SpamNotification');";
        }
        else
        {
            return string.Format("location.href = 'List.aspx?folder={0}&init=true'", id.ToString());
        }
    }
}