using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using Microsoft.Win32;

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

namespace Cosmos.Win
{
    public partial class frmWebMasterDown : Form, IFormSaleOpenUs
    {
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private TranStatus m_cTrnStatus = new TranStatus();                         // 거래정보

        private string m_sPosMenuKey = string.Empty;
        public string PosMenuKey
        {
            get { return this.m_sPosMenuKey; }
            set { this.m_sPosMenuKey = value;}
        }

        /// <summary>
        /// 생성자
        /// </summary>
        public frmWebMasterDown()
        {
            InitializeComponent();

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

            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cTrnStatus = (TranStatus)StateObject.TRAN;    // POS 거래정보
        }

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

                FormManager.SetFormAllControlFont(this, m_cPosStatus.Base.FONT);
                FormManager.MovePopUpForm(this, false, m_cPosStatus.Sale.ScreenSizeUser);

                picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.SCH_MESSAGE_BOX);

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

        private void frmWebMasterDown_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 + "()", "");
        }

        private void frmWebMasterDown_Activated(object sender, EventArgs e)
        {
            try
            {
                if (this.Tag.ToString() == "START")
                {
                    this.Tag = "";
                    lblMessage.Text = MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0585);
                    this.Update();

                    if (m_sPosMenuKey == "MEMUKEY")
                    {
                        // 상품키 마스터 다운로드
                        DownLoadMenuKeyMaster();
                    }

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

        /// <summary>
        /// 상품키 마스터 다운로드
        /// </summary>
        private void DownLoadMenuKeyMaster()
        {
            try
            {
                IMasterDownUs cMstDownSrv = (IMasterDownUs)sManager.InitServiceInstance(ServiceLists.BSV_BASIC.DLL, ServiceLists.BSV_BASIC.MASTER_DOWN);

                Hashtable htMasterList = new Hashtable();
                
                htMasterList.Add(0, MasterLists.MST_ITEM_KEY_L_CLSS.ID);               // 47 - 상품 키 대분류 마스터
                htMasterList.Add(1, MasterLists.MST_ITEM_KEY_CLSS.ID);                 // 49 - 상품 키 분류 마스터
                htMasterList.Add(2, MasterLists.MST_ITEM_KEY.ID);                      // 50 - 상품 키 마스터
                htMasterList.Add(3, MasterLists.MST_BOOKMK.ID);                        // 51 - 즐겨찾기 마스터

                string sReturn = cMstDownSrv.Execute(new object[] { htMasterList, m_cPosStatus.Base.MstDownDate, m_cPosStatus.Base.MstBatchDownDate, this });
                if(sReturn != UserCom.RST_OK)
                {
                    UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                         System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                         System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                         System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                         "Failed specified master download and insert.");
                    return;
                }

                // 복합매장점포일 경우 복합매장 정보를 추가로 Down
                DataTable dtData = (DataTable)cMstDownSrv.GetData(new string[] { m_cPosStatus.Base.CmpCd, m_cPosStatus.Base.StoreNo });
                if (dtData != null && dtData.Rows.Count > 0)
                {
                    Hashtable htComplexStoreMstList = new Hashtable();
                    htComplexStoreMstList.Add(0, MasterLists.MST_ITEM_KEY_L_CLSS.ID);      // 47 - 상품키 대분류 마스터
                    htComplexStoreMstList.Add(1, MasterLists.MST_ITEM_KEY_CLSS.ID);        // 49 - 상품 키 분류 마스터
                    htComplexStoreMstList.Add(2, MasterLists.MST_ITEM_KEY.ID);             // 50 - 상품 키 마스터
                    htComplexStoreMstList.Add(3, MasterLists.MST_BOOKMK.ID);               // 51 - 즐겨찾기 마스터
                    
                    foreach (DataRow dr in dtData.Rows)
                    {
                        sReturn = cMstDownSrv.Execute(new object[] { htComplexStoreMstList,  m_cPosStatus.Base.MstDownDate, m_cPosStatus.Base.MstBatchDownDate, CmUtil.GetDataRowStr(dr, "SUB_BRAND_CD"), CmUtil.GetDataRowStr(dr, "SUB_STOR_CD"), this });
                        if (sReturn != UserCom.RST_OK)
                        {
                            UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                                 System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                                 System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                                 System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                                 "Failed Complex store specified master download and insert.");
                            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);
            }
        }



        #region IFormSaleOpenUs 멤버

        public void SetPanelShowFlag(bool bShow)
        {
            return;
        }

        public void InitProgressbar()
        {
            return;
        }

        public void SaleOpenProgress(string sMstId, int nProgress)
        {
            return;
        }

        public void SetLabelMsg(string sMsg)
        {
            return;
        }

        public void SetProgressbarStyle(ProgressBarStyle pbStyle)
        {
            return;
        }

        public void ProgressProgressBar(int iStep)
        {
            return;
        }

        #endregion

        
    }
}
