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

public partial class Filebox_Upload : System.Web.UI.Page
{
    protected Guid _folderId;                       // 파일박스 folderId
    protected string _type;                         // 파일박스 타입 (Private, Share, Subscribe) 

    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");

        if (!IsPostBack)
        {
            Page.Title = AppSetting.GetTitleInfo(Request).Title;

            FileboxFolder folder = new FileboxFolder(_folderId);
            Filebox.RsAccessFlag accessFlag = Filebox.GetAccessFlag(WebUser.CurrentUser.DomainUserID, folder.FileboxID);

            if ((accessFlag & Filebox.RsAccessFlag.Upload) > 0)
            {
                UploadButton.Enabled = true;
            }
            else
            {
                UploadButton.Enabled = false;
                return;
            }

            long permitSize, usedSize;
            Filebox filebox = new Filebox(folder.FileboxID);
            permitSize = filebox.SpaceAllowed;
            if (permitSize != -1)
            {
                usedSize = filebox.SpaceUsed;
                AllocSizeLabel.Text = WebUtil.GetFormattedFileSize(permitSize);
                UsedSizeLabel.Text = WebUtil.GetFormattedFileSize(usedSize);
                long availSize = permitSize - usedSize;
                if (availSize < 0) availSize = 0;
                AvailSizeLabel.Text = WebUtil.GetFormattedFileSize(availSize);
            }
            else
            {
                UserFilebox userFilebox = new UserFilebox(WebUser.CurrentUser.DomainUserID);
                permitSize = userFilebox.SpaceAllowed;
                usedSize = userFilebox.SpaceUsed;
                AllocSizeLabel.Text = WebUtil.GetFormattedFileSize(permitSize);
                UsedSizeLabel.Text = WebUtil.GetFormattedFileSize(usedSize);
                long availSize = permitSize - usedSize;
                if (availSize < 0) availSize = 0;
                AvailSizeLabel.Text = WebUtil.GetFormattedFileSize(availSize);
            }
        }
    }

    protected void UploadButton_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;

        try
        {
            if (FileUpload1.HasFile)
            {
                string fileName = FileUpload1.FileName;
                int fileSize = FileUpload1.PostedFile.ContentLength;
                string ext = Path.GetExtension(fileName);
                string filePath = string.Format("{0}\\{1}{2}", GlobalConfig.WebShareTempPath, Guid.NewGuid(), ext);
                UploadResult uploadResult = new UploadResult(filePath);

                FileUpload1.SaveAs(filePath);

                string virusResult;
                if (fileSize < AppSetting.LimitVirusCheckSize)
                {
                    bool infected = WebUtil.VirusInfected(filePath, out virusResult);
                    if (infected)
                    {
                        FileInfo fi = new FileInfo(filePath);
                        if (fi.Exists) fi.Delete();
                        ShowWarningMessage(string.Format(GetLocalResourceObject("Msg01").ToString(), fileName, virusResult));
                        return;
                    }
                }

                FileboxFile file = new FileboxFile();
                file.Name = fileName;
                file.Size = (long)fileSize;
                file.Description = DescTextBox.Value;
                file.Creator = WebUser.CurrentUser.UserAccount;
                file.CreateTime = DateTime.UtcNow;
                folder.AddFile(file, filePath, WebUser.CurrentUser.DomainUserID);

                ShowSuccessMessage(string.Format(GetLocalResourceObject("Msg02").ToString(), fileName, WebUtil.GetFormattedFileSize(fileSize)));
                FileUpload1.Enabled = false;
                UploadButton.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            ShowWarningMessage(GetLocalResourceObject("Msg03").ToString() + ": " + ex.Message.ToString());
        }
    }

    private void ShowWarningMessage(string msg)
    {
        MsgBox.Visible = true;
        MsgBox.CssClass = "alert alert-danger";
        MsgBoxLabel.Text = msg;
    }

    private void ShowSuccessMessage(string msg)
    {
        MsgBox.Visible = true;
        MsgBox.CssClass = "alert alert-success";
        MsgBoxLabel.Text = msg;
        string script = "GotoListLater()";
        if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "successMsgScript"))
            ClientScript.RegisterClientScriptBlock(this.GetType(), "successMsgScript", script, true);
    }
}