using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Cosmos.CommonManager;
using Cosmos.Common;
using Cosmos.BaseFrame;
using Cosmos.UserFrame;

namespace Cosmos.Win
{
    public partial class frmSchPosLog : Form
    {
        #region 변수 선언
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        #endregion 변수 선언

        #region 생성자 & 소멸자
        public frmSchPosLog()
        {
            InitializeComponent();

            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
            //this.UpdateStyles();

            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
        }

        private void frmSchPosLog_Load(object sender, EventArgs e)
        {
            UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "");
            InitControl();
        }

        private void frmSchPosLog_FormClosing(object sender, FormClosingEventArgs e)
        {
            UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", lblTitle.Text);
        }
        #endregion 생성자 & 소멸자

        #region 폼 컨트롤 초기화
        /// <summary>
        /// 폼 컨트롤 초기화
        /// </summary>
        private void InitControl()
        {
            try
            {
                if (m_cPosStatus.Sale.ScreenSizeUser == 1024)
                {
                    // 이미지 로딩 처리
                    picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_FULL_SIZE);
                    //this.Size = new Size(1024, 696);
                    //this.Location = new Point(0, 50);
                    FormManager.SetControlSize(picBack, 0, 0, 1024, 696);
                    FormManager.SetControlSize(this, 0, 50, picBack.Size.Width, picBack.Size.Height);

                    FormManager.SetControlSize(this.lblTitle, 16, 1, 950, 50);
                    FormManager.SetControlSize(this.btnExit, 965, 1, 58, 50);
                    FormManager.SetControlSize(this.txtChkMessage, 16, 123, 992, 556);
                    FormManager.SetControlSize(this.label1, 16, 67, 835, 2);
                    FormManager.SetControlSize(this.lblDate, 16, 68, 122, 40);
                    FormManager.SetControlSize(this.lblTableNo, 353, 68, 122, 40);
                    FormManager.SetControlSize(this.cbFileName, 490, 75, 343, 26);
                    FormManager.SetControlSize(this.btnSearch, 908, 68, 100, 40);
                    FormManager.SetControlSize(this.label3, 16, 122, 992, 2);
                    FormManager.SetControlSize(this.btnDate, 311, 75, 26, 26);
                    FormManager.SetControlSize(this.txtDate, 153, 75, 157, 26);
                    FormManager.SetControlSize(this.lblBox2, 474, 68, 377, 40);
                    FormManager.SetControlSize(this.lblBox1, 137, 68, 217, 40);
                }
                else
                {
                    // 이미지 로딩 처리
                    picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_SIZE_800X600);
                    FormManager.SetControlSize(picBack, 0, 0, 800, 600);
                    FormManager.SetControlSize(this, 0, 0, picBack.Size.Width, picBack.Size.Height);

                    FormManager.SetControlSize(this.lblTitle, 16, 1, 728, 50);
                    FormManager.SetControlSize(this.btnExit, 741, 1, 58, 50);
                    FormManager.SetControlSize(this.txtChkMessage, 22, 123, 756, 454);
                    FormManager.SetControlSize(this.label1, 22, 67, 642, 2);
                    FormManager.SetControlSize(this.lblDate, 22, 68, 102, 40);
                    FormManager.SetControlSize(this.lblTableNo, 309, 68, 102, 40);
                    FormManager.SetControlSize(this.cbFileName, 426, 75, 220, 26);
                    FormManager.SetControlSize(this.btnSearch, 678, 68, 100, 40);
                    FormManager.SetControlSize(this.label3, 22, 122, 756, 2);
                    FormManager.SetControlSize(this.btnDate, 267, 75, 26, 26);
                    FormManager.SetControlSize(this.txtDate, 139, 75, 127, 26);
                    FormManager.SetControlSize(this.lblBox2, 410, 68, 254, 40);
                    FormManager.SetControlSize(this.lblBox1, 123, 68, 189, 40);
                }

                // 이미지 로딩 처리
                //picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_FULL_SIZE);
                FormManager.SetFormAllControlFont(this, m_cPosStatus.Base.FONT);
                FormManager.SetTextBoxGlobalInfo(this, m_cPosStatus.Global.m_stCultureMaster.nGroupingDigits, m_cPosStatus.Global.m_stCultureMaster.strGroupingSymbol
                    , m_cPosStatus.Global.m_stCultureMaster.nDecimalDigits, m_cPosStatus.Global.m_stCultureMaster.strDecimalSymbol);
                FormManager.MovePopUpForm(this, false, m_cPosStatus.Sale.ScreenSizeUser);

                btnExit.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.BTN_CLOSE);
                if (btnExit.Image != null) btnExit.Text = "";
                btnDate.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.ICON_DATE_BASIC);
                if (btnDate.Image != null) btnDate.Text = "";

                txtDate.InputTypeFormat = m_cPosStatus.Global.m_stCultureMaster.strDatePattern;
                txtDate.Text = m_cPosStatus.Global.DateToCulture(System.DateTime.Now.ToString("yyyyMMdd"));

                lblTitle.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0588);
                lblDate.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0242);

                ComboBoxInit();

                txtChkMessage.Text = "";
                txtChkMessage.Font = new Font(m_cPosStatus.Base.FONT_FIX, txtChkMessage.Font.Size, txtChkMessage.Font.Style);

            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }

        /// <summary>
        /// 콤보박스 초기화
        /// </summary>
        private void ComboBoxInit()
        {
            try
            {
                string sDate = m_cPosStatus.Global.CultureToDate(txtDate.Text);

                cbFileName.Items.Clear();

                if (System.IO.Directory.Exists(BaseCom.NxLogPath))
                {
                    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(BaseCom.NxLogPath);
                    
                    foreach (var item in di.GetFiles())
                    {
                        if (sDate == item.CreationTime.ToString("yyyyMMdd"))
                        {
                            cbFileName.Items.Add(item.Name);
                        }
                    }
                }

                cbFileName.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
        #endregion 폼 컨트롤 초기화

        #region Control Event
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int nLoop = 0;
            String line = "";

            try
            {
                if (File.Exists(BaseCom.NxLogPath + cbFileName.Text.Trim()) == true)
                {
                    FileStream fsVersion = new FileStream(BaseCom.NxLogPath + cbFileName.Text.Trim(), FileMode.Open, FileAccess.Read);
                    StreamReader srVersion = new StreamReader(fsVersion, Encoding.Default);

                    //foreach (string file in files)
                    //{
                    //    string tabTitle = file + "     ";
                    //    TabPage tabPage = new TabPage(tabTitle);
                    //    tabPage.Size = new Size(500, 500);
                    //    tabCtrl.TabPages.Add(tabPage);

                    //    TextBox tbLogViewer = new TextBox();
                    //    tbLogViewer.Dock = System.Windows.Forms.DockStyle.Fill;
                    //    tbLogViewer.Location = new System.Drawing.Point(0, 0);
                    //    tbLogViewer.Multiline = true;
                    //    tbLogViewer.Name = "tbLogViewer_" + nLoop;
                    //    tbLogViewer.ScrollBars = System.Windows.Forms.ScrollBars.Both;
                    //    tbLogViewer.Size = new System.Drawing.Size(904, 877);
                    //    tbLogViewer.TabIndex = 0;
                    
                    //    tabPage.Controls.Add(tbLogViewer);

                    //    this.Controls.Add(tabCtrl);

                    //    while ((line = reader.ReadLine()) != null)
                    //    {
                    //        tbLogViewer.AppendText(UserLog.UserAES.StaticKeyDecrypt(line) + "\r\n");
                    //    }

                    //    nLoop++;
                    //}

                    WinManager.ShowSearchMessage(string.Format(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0231), MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0894)), this);

                    //while ((line = srVersion.ReadLine()) != null)
                    //{
                    //    txtChkMessage.AppendText(UserLog.UserAES.StaticKeyDecrypt(line) + "\r\n");
                    //}

                    //FileStream fsVersion = new FileStream(BaseCom.NxLogPath + cbFileName.Text.Trim(), FileMode.Open, FileAccess.Read);
                    //StreamReader srVersion = new StreamReader(fsVersion, Encoding.GetEncoding("ks_c_5601-1987"));
                    //srVersion.BaseStream.Seek(0, SeekOrigin.Begin);


                    while (srVersion.Peek() > -1)
                    {
                        //txtChkMessage.Text = UserLog.UserAES.StaticKeyDecrypt(srVersion.ReadToEnd());
                        txtChkMessage.Text = srVersion.ReadToEnd();
                    }

                    srVersion.Close();
                    fsVersion.Close();

                    WinManager.HideSearchMessage(this);

                }
                else
                {
                    // 로그 파일이 없을 경우
                    WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0441));
                    return;
                }
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
        #endregion Control Event

        private void dtpDateFrom_ValueChanged(object sender, EventArgs e)
        {
            ComboBoxInit();
        }

        private void btnDate_Click(object sender, EventArgs e)
        {
            try
            {
                string sDate = WinBasic.ShowForm(new string[] { FormManager.FORM_CALENDAR });
                if (sDate != "") txtDate.Text = m_cPosStatus.Global.DateToCulture(sDate);
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
    }
}
