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_List : System.Web.UI.Page
{
    protected Guid _folderId;                       // 파일박스 folderId
    protected string _type;                         // 파일박스 타입 (Private, Share, Subscribe) 
    protected bool _download = true;
    protected Filebox.RsAccessFlag _accessFlag = Filebox.RsAccessFlag.None;

    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)
    {
        _folderId = WebParam.GetGuidParamValue("folder");
        _type = WebParam.GetStrParamValue("type");
        Page.Title = AppSetting.GetTitleInfo(Request).Title;

        if (!IsPostBack)
        {
            if (_folderId == Guid.Empty) _folderId = GetPrivateFolderID();

            // 폴더에 대한 사용 권한을 얻는다.
            FileboxFolder folder = new FileboxFolder(_folderId);
            _accessFlag = Filebox.GetAccessFlag(WebUser.CurrentUser.DomainUserID, folder.FileboxID);

            if (_accessFlag == Filebox.RsAccessFlag.None)
            {
                FileGridView.DataSource = null;
                return;
            }

            // Admin: 모든 권한을 가짐
            // Upload: 업로드, 삭제 권한을 가짐
            // Download: 다운로드 권한을 가짐
            if ((_accessFlag & Filebox.RsAccessFlag.Upload) > 0)
            {
                BootstrapUtil.EnableButton(uploadToolButton, true);
                BootstrapUtil.EnableButton(deleteToolButton, true);
            }
            else
            {
                BootstrapUtil.EnableButton(uploadToolButton, false);
                BootstrapUtil.EnableButton(deleteToolButton, false);
            }
            if ((_accessFlag & Filebox.RsAccessFlag.Download) == 0)
            {
                _download = false;
            }

            FileGridView.Columns[1].HeaderText = folder.Name;
        }
        else
        {
        }
    }

    protected void FileGridView_PreRender(object sender, EventArgs e)
    {
        GridView gv = (GridView)sender;
        if ((gv.ShowHeader == true && gv.Rows.Count > 0) || (gv.ShowHeaderWhenEmpty == true))
        {
            gv.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
        if (gv.ShowFooter == true && gv.Rows.Count > 0)
        {
            gv.FooterRow.TableSection = TableRowSection.TableFooter;
        }
    }

    protected void DeleteButton_Click(object sender, EventArgs e)
    {
        // 폴더에 대한 사용 권한을 얻는다.
        FileboxFolder folder = new FileboxFolder(_folderId);
        Filebox.RsAccessFlag accessFlag = Filebox.GetAccessFlag(WebUser.CurrentUser.DomainUserID, folder.FileboxID);
        if ((accessFlag & (Filebox.RsAccessFlag.Admin | Filebox.RsAccessFlag.Upload)) == 0) return;

        foreach (GridViewRow row in FileGridView.Rows)
        {
            CheckBox check = (CheckBox)row.FindControl("SelectCheckBox");
            if (check.Checked)
            {
                Guid fileId = new Guid(FileGridView.DataKeys[row.DataItemIndex - (FileGridView.PageSize * FileGridView.PageIndex)].Value.ToString());
                FileboxFile file = new FileboxFile(fileId);
                file.Remove();
            }
        }
        FileGridView.DataBind();
    }

    #region Grid Support Methods

    protected string GetName(string name)
    {
        return Server.HtmlEncode(name);
    }

    protected string GetDescription(string description)
    {
        return Server.HtmlEncode(description);
    }

    protected string GetImageName(string fileName)
    {
        ExtIconMap iconMap = new ExtIconMap();
        return iconMap.GetIconName(fileName);
    }

    protected string GetSize(long size)
    {
        return WebUtil.GetFormattedFileSize(size);
    }

    protected string GetClickEventFunction(Guid id)
    {
        if (_download)
        {
            return string.Format("DownloadFile('Download.ashx?fileId={0}')", id);
        }
        else
        {
            return "";
        }
    }

    #endregion

    private Guid GetPrivateFolderID()
    {
        UserFilebox userFilebox = new UserFilebox(WebUser.CurrentUser.DomainUserID);
        return userFilebox.MyFileboxes[0].ID;    }
    
}