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.Filebox;

public partial class Filebox_Folders : System.Web.UI.Page
{
    protected class FolderItem
    {
        public Guid _id = Guid.Empty;
        public string _folderName = "";
        public FileboxUtil.FolderFlag _flag = FileboxUtil.FolderFlag.None;
        public FileboxUtil.FolderType _type = FileboxUtil.FolderType.Private;
        public int _indent = 1;

        public Guid ID
        {
            get { return _id; }
            set { _id = value; }
        }

        public string FolderName
        {
            get { return _folderName; }
            set { _folderName = value; }
        }

        public FileboxUtil.FolderFlag Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }

        public FileboxUtil.FolderType Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public int Indent
        {
            get { return _indent; }
            set { _indent = value; }
        }
    }

    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;

            if (WebParam.GetBooleanParamValue("close", true) == false)
                closeToolButton.Attributes["class"] = "collapse";

            LoadFileboxes();
        }
    }

    private void LoadFileboxes()
    {
        List<FolderItem> folders = new List<FolderItem>();

        UserFilebox userFilebox = new UserFilebox(WebUser.CurrentUser.DomainUserID);

        FolderItem item;
        item = new FolderItem();
        item.FolderName = Resources.Filebox.PrivateFilebox;
        item.Flag = FileboxUtil.FolderFlag.Root;
        item.Type = FileboxUtil.FolderType.Private;
        folders.Add(item);
        foreach (Filebox filebox in userFilebox.MyFileboxes)
        {
            FileboxFolder folder = filebox.RootFolder;
            item = new FolderItem();
            item.ID = folder.ID;
            item.FolderName = filebox.Name;
            item.Flag = FileboxUtil.FolderFlag.Sub;
            item.Type = FileboxUtil.FolderType.Private;
            item.Indent = 2;
            folders.Add(item);
            TraverseSubFolders(folders, filebox.ID, folder, item.Type, item.Indent + 1);
        }

        item = new FolderItem();
        item.FolderName = Resources.Filebox.ShareFilebox;
        item.Flag = FileboxUtil.FolderFlag.Root;
        item.Type = FileboxUtil.FolderType.Share;
        folders.Add(item);
        foreach (Filebox filebox in userFilebox.ShareFileboxes)
        {
            if (filebox.AccessFlag != Filebox.RsAccessFlag.None)
            {
                FileboxFolder folder = filebox.RootFolder;
                item = new FolderItem();
                item.ID = folder.ID;
                item.FolderName = filebox.Name;
                item.Flag = FileboxUtil.FolderFlag.Sub;
                item.Type = FileboxUtil.FolderType.Share;
                item.Indent = 2;
                folders.Add(item);
                TraverseSubFolders(folders, filebox.ID, folder, item.Type, item.Indent + 1);
            }
        }

        FolderRepeater.DataSource = folders;
        FolderRepeater.DataBind();
    }

    private void TraverseSubFolders(List<FolderItem> folderItems, Guid fileboxID, FileboxFolder folder, FileboxUtil.FolderType folderType, int indent)
    {
        foreach (FileboxFolder subfolder in folder.SubFolders)
        {
            FolderItem item = new FolderItem();
            item.ID = subfolder.ID;
            item.FolderName = subfolder.Name;
            item.Flag = FileboxUtil.FolderFlag.Sub;
            item.Type = folderType;
            item.Indent = indent;
            folderItems.Add(item);
            TraverseSubFolders(folderItems, fileboxID, subfolder, folderType, indent + 1);
        }
    }

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

    protected string GetFileboxName(FolderItem folder)
    {
        string tag = "";
        switch (folder.Flag)
        {
            case FileboxUtil.FolderFlag.Root:
                tag = string.Format("<img src=\"../Images/Filebox/{0}\" alt=\"{1}\" /> <b>{1}</b>",
                    (folder.Type == FileboxUtil.FolderType.Private) ? FileboxUtil.GetMyFileboxImageName(folder.Flag) : FileboxUtil.GetShareFileboxImageName(folder.Flag), folder.FolderName);
                break;
            default:
                if (folder.Type == FileboxUtil.FolderType.Private)
                    tag = string.Format("<img src=\"../Images/Filebox/{0}\" alt=\"{1}\" /> {1}", FileboxUtil.GetMyFileboxImageName(folder.Flag), folder.FolderName);
                else
                    tag = string.Format("<img src=\"../Images/Filebox/{0}\" alt=\"{1}\" /> {1}", FileboxUtil.GetShareFileboxImageName(folder.Flag), folder.FolderName);
                break;
        }
        return tag;
    }

    protected string GetFileboxNameEx(FolderItem folder)
    {
        string tag = "";
        switch (folder.Flag)
        {
            case FileboxUtil.FolderFlag.Root:
                tag = string.Format("{0} <b>{1}</b>", FontIcon.FileboxRoot, Server.HtmlEncode(folder.FolderName));
                break;
            default:
                tag = string.Format("{0} {1}", FontIcon.FileboxFolder, Server.HtmlEncode(folder.FolderName));
                break;
        }
        return tag;
    }

    protected string GetClickEventFunction(FolderItem folder)
    {
        string tag = "";
        switch (folder.Flag)
        {
            case FileboxUtil.FolderFlag.Root:                
                break;
            default:
                if (folder.Type == FileboxUtil.FolderType.Private)
                    tag = string.Format("location.href = 'List.aspx?folder={0}&type=private'", folder.ID);
                else
                    tag = string.Format("location.href = 'List.aspx?folder={0}&type=share'", folder.ID);
                break;
        }
        return tag;
    }
}