using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

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

namespace Cosmos.Win
{
    public partial class frmMenuSub : Form
    {
        #region 변수 선언
        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 Hashtable m_htMenuControls;                                         // 메뉴버튼 컨트롤 정보
        private ArrayList m_alMenuHead = new ArrayList();                           // 메뉴타이틀정의 배열
        private ArrayList m_alMenuProc = new ArrayList();                           // 메뉴정의 배열
        private IDataCommonUs m_cDataCommon = null;                                 // POS 공통함수 인터페이스

        private string m_sPosMenuKeyIn;
        public string PosMenuKeyIn { set { m_sPosMenuKeyIn = value; } get { return m_sPosMenuKeyIn; } }
        private string m_sPosMenuKeyOut;
        public string PosMenuKeyOut { set { m_sPosMenuKeyOut = value; } get { return m_sPosMenuKeyOut; } }

        /// <summary>
        /// 메뉴상단표시위치
        /// </summary>
        private int m_nTopPos = 0;
        #endregion

        #region 생성자
        public frmMenuSub()
        {
            InitializeComponent();

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

            m_cPosStatus = (PosStatus)StateObject.POS;      
            m_cTrnStatus = (TranStatus)StateObject.TRAN;

            m_cDataCommon = (IDataCommonUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_COMMON);

            SetMenuBtn();

        }
        
        private void frmMenu_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 + "()", "");
            // 메뉴 로드
            if (string.IsNullOrEmpty(PosMenuKeyIn) == false)
                LoadMenuList();

            // 콘트롤 초기화 처리
            InitControl();
        }

        private void frmMenu_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 메뉴 버튼 로딩
        // 메뉴리스트 로딩
        private void LoadMenuList()
        {
            try
            {
                m_alMenuHead = new ArrayList();
                m_alMenuProc = new ArrayList();

                DataTable dtFuncKey = null;

                int idx = 1;

                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //헤더 로드
                dtFuncKey = m_cDataCommon.SeletFuncKey(PosMenuKeyIn);
                if (dtFuncKey == null || dtFuncKey.Rows.Count <= 0)
                {
                    //return;
                }
                else
                {
                    foreach (DataRow dr in dtFuncKey.Rows)
                    {
                        string sPosName = CmUtil.GetDataRowStr(dr, PosMst.MST_FUNC_KEY.DATA.FUNC_NM);         // 기능명
                        m_alMenuHead.Add(new string[] { PosMenuKeyIn, sPosName });
                    }
                }
                
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


                if (PosMenuKeyIn == PosKey.MENU_KEY.SALE_LOSS)
                {
                    dtFuncKey = m_cDataCommon.SeletCommonCode(PosConst.COMMON_CODE_GROUP.S0001, "");
                    if (dtFuncKey == null || dtFuncKey.Rows.Count <= 0)
                    {
                        return;
                    }
                    else
                    {
                        //테스트
                        //DataRow[] dr1 = dtFuncKey.Select("CMM_GRP_CD = 'S0001' AND CMM_CD ='07'");
                        //foreach (DataRow dr2 in dr1)
                        //{
                        //    string sDispSeq = CmUtil.GetDataRowStr(dr2, PosMst.MST_COMMON.DATA.DISP_SEQ);    // 표시순번
                        //}

                        foreach (DataRow dr in dtFuncKey.Rows)
                        {
                            string sDispSeq = CmUtil.GetDataRowStr(dr, PosMst.MST_COMMON.DATA.DISP_SEQ);    // 표시순번
                            string sPosName = CmUtil.GetDataRowStr(dr, PosMst.MST_COMMON.DATA.CD_NM);       // 기능명
                            string sPosKey = CmUtil.GetDataRowStr(dr, PosMst.MST_COMMON.DATA.CMM_CD);       // 기능키

                            if (CmUtil.GetDataRowStr(dr, PosMst.MST_COMMON.DATA.CHAR_VAL_TITLE_VAL_01) == "1")
                            {
                                m_alMenuProc.Add(new string[] { PosMenuKeyIn, idx.ToString(), sPosName, sPosKey });   //배열저장
                                idx++;
                            }
                        }
                    }
                }
                else if (PosMenuKeyIn == PosKey.MENU_KEY.TOP_MENU_WEB)
                {
                    dtFuncKey = m_cDataCommon.SeletMstWebLinkMst("");
                    if (dtFuncKey == null || dtFuncKey.Rows.Count <= 0)
                    {
                        return;
                    }
                    else
                    {
                        int seqIndex = 0;
                        foreach (DataRow dr in dtFuncKey.Rows)
                        {
                            //string sDispSeq = CmUtil.GetDataRowStr(dr, PosMst.MST_WEB_LINKAGE.DATA.DISP_SEQ);    // 표시순번
                            string sPosName = CmUtil.GetDataRowStr(dr, PosMst.MST_WEB_LINKAGE.DATA.MENU_NM);       // 기능명
                            string sPosKey = CmUtil.GetDataRowStr(dr, PosMst.MST_WEB_LINKAGE.DATA.FUNC_ID);       // 기능키

                            //if (CmUtil.GetDataRowStr(dr, PosMst.MST_WEB_LINKAGE.DATA.DIV) == "2")
                            //{
                                //m_alMenuProc.Add(new string[] { PosMenuKeyIn, idx.ToString(), sPosName, sPosKey });   //배열저장
                                //idx++;
                                seqIndex = seqIndex + 1;
                                string sDispSeq = Convert.ToString(seqIndex);     // 표시순번
                                m_alMenuProc.Add(new string[] { m_sPosMenuKeyIn, sDispSeq, sPosName, sPosKey });   //배열저장
                            //}
                            

                        }
                    }
                }
                else
                {
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    // 판매기본 기능키 로딩
                    dtFuncKey = m_cDataCommon.SeletFuncKeyGroup(PosMenuKeyIn);
                    if (dtFuncKey == null || dtFuncKey.Rows.Count <= 0)
                    {
                        return;
                    }
                    else
                    {
                        foreach (DataRow dr in dtFuncKey.Rows)
                        {
                            string sDispSeq = CmUtil.GetDataRowStr(dr, PosMst.MST_FUNC_KEY_GRP.DATA.DISP_SEQ);        // 표시순번
                            string sPosName = CmUtil.GetDataRowStr(dr, PosMst.MST_FUNC_KEY_GRP.DATA.FUNC_NM);         // 기능명
                            string sPosKey = CmUtil.GetDataRowStr(dr, PosMst.MST_FUNC_KEY_GRP.DATA.FUNC_ID);          // 기능키

                            m_alMenuProc.Add(new string[] { PosMenuKeyIn, idx.ToString(), sPosName, sPosKey });           //배열저장
                            idx++;
                        }
                    }
                }
                
            }
            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


        private void SetMenuBtn()
        {
            try
            {
                #region 메뉴버튼
                // 메뉴버튼 
                m_htMenuControls = new Hashtable();
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu01);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu02);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu03);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu04);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu05);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu06);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu07);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu08);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu09);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu10);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu11);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu12);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu13);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu14);
                m_htMenuControls.Add(m_htMenuControls.Count, btnMenu15);

                btnExit.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.BTN_CLOSE);

                #endregion
            }
            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 폼 컨트롤 초기화
        /// <summary>
        /// 폼 컨트롤 초기화
        /// </summary>
        private void InitControl()
        {
            try
            {
                // 이미지 로딩 처리
                picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_SIZE_S);

                if (PosMenuKeyIn == PosKey.MENU_KEY.SALE_LOSS || PosMenuKeyIn == PosKey.MENU_KEY.TOP_MENU_WEB)
                {
                    FormManager.MovePopUpForm(this, false, m_cPosStatus.Sale.ScreenSizeUser);
                }
                else
                {
                    FormManager.MovePopUpForm(this, true, m_cPosStatus.Sale.ScreenSizeUser);
                }
                FormManager.SetFormAllControlFont(this, m_cPosStatus.Base.FONT);

                // 메뉴 타이틀 출력
                foreach (string[] aMenu in m_alMenuHead)
                {
                    if (aMenu[0] == PosMenuKeyIn)
                    {
                        lblTitle.Text = aMenu[1];
                        break;
                    }
                }

                m_nTopPos = 0;
                MenuDisplay();      //화면에 메뉴 표시
            }
            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 MenuDisplay()
        {
            try 
            {
                int nMax = 0;

                // 메뉴 상세 초기화
                foreach (object obj in m_htMenuControls.Values)
                {
                    Cosmos.UI.CsmLabel btnCtl = (Cosmos.UI.CsmLabel)obj;
                    btnCtl.Text = "";
                    btnCtl.Tag = "";
                    btnCtl.BackColor = Color.FromArgb(236, 241, 249);
                    // btnCtl.Visible = false;
                }

                //0:메뉴항목, 1:위치, 2:표시텍스트, 3:기능키값
                foreach (string[] aMenu in m_alMenuProc)
                {
                    if (aMenu[0] == PosMenuKeyIn)
                    {
                        nMax = m_htMenuControls.Count;
                        
                        int nPos = int.Parse(aMenu[1]);      // 표시위치
                        if (nPos > m_nTopPos && nPos <= m_nTopPos + nMax)
                        {
                            Cosmos.UI.CsmLabel btnCtl = (Cosmos.UI.CsmLabel)m_htMenuControls[(nPos - 1) % nMax];

                            btnCtl.Text = aMenu[2];
                            btnCtl.Tag = aMenu[3];
                            //btnCtl.Visible = true;
                            btnCtl.BackColor = Color.FromArgb(255, 255, 255);
                        }
                    }
                }

                SetNavigationBtn(m_alMenuProc.Count);

            }
            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 버튼 입력 처리
        /// <summary>
        /// 메뉴 버튼 입력 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProc_Click(object sender, EventArgs e)
        {
            try
            {
                if (((Cosmos.UI.CsmLabel)sender).Tag.ToString() != "")
                {   
                    UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ((Cosmos.UI.CsmLabel)sender).Text.ToString());

                    PosMenuKeyOut = ((Cosmos.UI.CsmLabel)sender).Tag.ToString();

                    if (PosMenuKeyIn == PosKey.MENU_KEY.SALE_LOSS)
                    {
                        string sLossTitle = ((Cosmos.UI.CsmLabel)sender).Text;

                        PosMenuKeyOut += "~^(" + sLossTitle;
                    }
                    
                    this.DialogResult = DialogResult.OK;
                    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);
            }
        }

        /// <summary>
        /// 기능 버튼 입력 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFuncProc_Click(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 + "()", ((Cosmos.UI.CsmLabel)sender).Text.ToString());

                if (((Cosmos.UI.CsmLabel)sender) == btnExit)
                {
                    PosMenuKeyOut = "";
                    //m_cTrnStatus.Head.LossTitle = "";
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
                if (((Cosmos.UI.CsmLabel)sender) == btnUp)
                {
                    int nMax = m_htMenuControls.Count;
                    if (m_nTopPos > 0)
                    {
                        m_nTopPos -= nMax;
                        MenuDisplay();
                    }
                }
                if (((Cosmos.UI.CsmLabel)sender) == btnDown)
                {
                    int nMax = m_htMenuControls.Count;

                    foreach (string[] aMenu in m_alMenuProc)
                    {
                        if (aMenu[0] == PosMenuKeyIn)
                        {
                            if (int.Parse(aMenu[1]) > m_nTopPos + nMax)
                            {
                                m_nTopPos += nMax;
                                MenuDisplay();
                                break;
                            }
                        }
                    }
                }
            }
            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>
        /// <param name="nMax"></param>
        private void SetNavigationBtn(int nMax)
        {
            try
            {
                if (m_nTopPos == 0)
                {
                    btnUp.Text = "■";
                    //btnUp.ForeColor = Color.FromArgb(255, 255, 255);
                }
                else
                {
                    btnUp.Text = "◀";
                    //btnUp.ForeColor = Color.FromArgb(163, 171, 178); 
                }

                if (m_nTopPos + 15 < nMax)
                {
                    btnDown.Text = "▶";
                    //btnDown.ForeColor = Color.FromArgb(163, 171, 178); 
                }
                else
                {
                    btnDown.Text = "■";
                    //btnDown.ForeColor = Color.FromArgb(255, 255, 255);
                }
            }
            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

    }
}
