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;

public partial class Mail_SelectFile : 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)
        {
            Guid uploadSessionKey = WebParam.GetGuidParamValue("key");
            if (uploadSessionKey == Guid.Empty)
            {
                Response.End();
                return;
            }

            UploadResultList uploadResults = GetUploadResultList();
            if (uploadResults != null) uploadResults.Clear();
            ResultGridView.DataSource = uploadResults;
            ResultGridView.DataBind();
        }
    }

    protected void ResultGridView_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 UploadButton_Click(object sender, EventArgs e)
    {
        UploadResultList uploadResults = GetUploadResultList();

        FileUpload[] uploads = new FileUpload[2];
        uploads[0] = FileUpload1;
        uploads[1] = FileUpload2;
        try
        {
            foreach (FileUpload upload in uploads)
            {
                if (upload.HasFile)
                {
                    string ext = Path.GetExtension(upload.FileName);
                    string filePath = string.Format("{0}\\{1}{2}", GlobalConfig.WebShareTempPath, Guid.NewGuid(), ext);
                    UploadResult uploadResult = new UploadResult(filePath, upload.PostedFile.ContentType);

                    upload.SaveAs(filePath);

                    if (upload.PostedFile.ContentLength < AppSetting.LimitVirusCheckSize)
                    {
                        string virusResult;
                        uploadResult.Infected = WebUtil.VirusInfected(filePath, out virusResult);
                        uploadResult.VirusName = virusResult;
                        uploadResult.FileName = upload.FileName;
                        uploadResult.FileSize = upload.PostedFile.ContentLength;
                        uploadResults.Add(uploadResult);

                        if (uploadResult.Infected)
                        {
                            FileInfo fi = new FileInfo(filePath);
                            if (fi.Exists) fi.Delete();
                        }
                    }
                    else
                    {
                        uploadResult.Infected = false;
                        uploadResult.VirusName = GetLocalResourceObject("NoVirusCheck").ToString(); // 검사 안함
                        uploadResult.FileName = upload.FileName;
                        uploadResult.FileSize = upload.PostedFile.ContentLength;
                        uploadResults.Add(uploadResult);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = "오류: " + ex.Message.ToString();
        }

        ResultGridView.DataSource = uploadResults;
        ResultGridView.DataBind();
        UploadResultPanel.Visible = true;

        if (uploadResults.Count > 0)
        {
            UploadButton.Enabled = false;
            selectToolButton.Attributes.Remove("disabled");
        }
    }

    private UploadResultList GetUploadResultList()
    {
        Guid uploadSessionKey = WebParam.GetGuidParamValue("key");
        if (uploadSessionKey == Guid.Empty) return null;

        UploadResultList uploadResults = (UploadResultList)Session[uploadSessionKey.ToString()];
        if (uploadResults == null)
        {
            uploadResults = new UploadResultList();
            Session[uploadSessionKey.ToString()] = uploadResults;
        }

        return uploadResults;
    }
}