using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

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

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 해피버즈 공통
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Win
{
    public partial class frmHappyBuzz : Form
    {
        #region Variable

        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 IDataServiceUs m_cWebBiz = null;                                    // 웹정보 서비스
                        
        private string m_sPosMenuKeyIn;
        public string PosMenuKeyIn { set { m_sPosMenuKeyIn = value; } get { return m_sPosMenuKeyIn; } }

        private string m_sCardNo;
        public string CardNoIn { set { m_sCardNo = value; } get { return m_sCardNo; } }
        
        private string m_sPosMenuKeyOut;
        public string PosMenuKeyOut { set { m_sPosMenuKeyOut = value; } get { return m_sPosMenuKeyOut; } }

        private string m_sPagerNo = "";

        private ArrayList alPagerNo = null;

        #endregion
        
        #region 생성자 & 소멸자
        /// <summary>
        /// 생성자
        /// </summary>
        public frmHappyBuzz()
        {
            InitializeComponent();

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

            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cTrnStatus = (TranStatus)StateObject.TRAN;    // POS 거래정보
            m_cWebBiz = (IDataServiceUs)sManager.InitServiceInstance(ServiceLists.BSV_BASIC.DLL, ServiceLists.BSV_BASIC.WEB_POS);
        }

        /// <summary>
        /// 폼로드
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmHappyBuzz_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();

            tmrReqst.Enabled = true;
        }

        /// <summary>
        /// 폼클로즈
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmHappyBuzz_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);
        }

        /// <summary>
        /// 폼엑티브
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmHappyBuzz_Activated(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// 폼디엑티브
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmHappyBuzz_Deactivate(object sender, EventArgs e)
        {
            
        }
        #endregion 생성자 & 소멸자
        
        #region 폼 컨트롤 초기화
        /// <summary>
        /// 폼 컨트롤 초기화
        /// </summary>
        private void InitControl()
        {
            


            try
            {
                btnAdd.Visible = false;
                btnCan.Visible = false;
                wbCtrl.Visible = false;
                
                if (m_sPosMenuKeyIn == PosKey.MENU_KEY.HAPPY_BUZ_REG)
                {

                    this.Size = new Size(788, 402);
                    //this.Location = new Point(216, 173);
                    FormManager.MovePopUpForm(this, true, m_cPosStatus.Sale.ScreenSizeUser);

                    //if (m_cPosStatus.Sale.ScreenSizeUser == 1024)
                    //{
                    //    this.Location = new Point((1024 - this.Size.Width) / 2, (768 - this.Size.Height) / 2);
                    //}
                    //else
                    //{
                    //    this.Location = new Point((800 - this.Size.Width) / 2, (600 - this.Size.Height) / 2);
                    //}

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

                    FormManager.SetControlSize(lblTitle, 16, 1, 708, 50);
                    FormManager.SetControlSize(btnExit, 730, 1, 58, 50);
                    FormManager.SetControlSize(picBack, 0, 0, 788, 402);
                    FormManager.SetControlSize(lblDspMsg, 22, 66, 744, 255);

                    FormManager.SetControlSize(btnAdd, 288, 333, 100, 50);
                    FormManager.SetControlSize(btnCan, 398, 333, 100, 50);
                    
                    //wbCtrl.Visible = false;
                    lblDspMsg.Visible = true;

                    lblDspMsg.Text = MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0530);
                    lblTitle.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0841);
                    btnAdd.Text  = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0843);
                    btnCan.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0844);

                    btnCan.BackColor = CmUtil.GetColorToString(m_cPosStatus.ScnMst.ThemeColor);
                }
                else
                {
                    this.Size = new Size(800, 600);
                    FormManager.MovePopUpForm(this, false, m_cPosStatus.Sale.ScreenSizeUser);
                    //if (m_cPosStatus.Sale.ScreenSizeUser == 1024)
                    //{
                    //    this.Location = new Point((1024 - this.Size.Width) / 2, (768 - this.Size.Height) / 2);
                    //}
                    //else
                    //{
                    //    this.Location = new Point((800 - this.Size.Width) / 2, (600 - this.Size.Height) / 2);
                    //}
                    picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_SIZE_800X600);

                    //wbCtrl.Visible = true;
                    lblDspMsg.Visible = false;

                    FormManager.SetControlSize(lblTitle, 16, 1, 718, 50);
                    FormManager.SetControlSize(btnExit, 740, 1, 58, 50);
                    FormManager.SetControlSize(wbCtrl, 8, 52, 784, 540);
                    FormManager.SetControlSize(picBack, 0, 0, 800, 600);

                    lblTitle.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0842);
                    
                }

                //m_sPagerNo = "";

                alPagerNo = new ArrayList();


                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);
                btnExit.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.BTN_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);
            }
        }

        #endregion

        #region Control Event
        /// <summary>
        /// 기능키 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProc_Click(object sender, EventArgs e)
        {
            try
            {
                if ((UI.CsmButton)sender == btnAdd)
                {
                    //추가지급
                    ProcessRun(PosConst.HAPPYBUZZ_BIZ_TYPE.SET);
                }
                else if ((UI.CsmButton)sender == btnCan)
                {

                    if (alPagerNo != null && alPagerNo.Count > 0)
                    {
                        ProcessRun(PosConst.HAPPYBUZZ_BIZ_TYPE.CAN);    //등록취소
                    }
                    else
                    {
                        WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0677);    //시연회 이후 디비 등록 해야함! - 임시
                    }



                    //if (m_sPagerNo == "")
                    //    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0677);    //시연회 이후 디비 등록 해야함! - 임시
                    //else
                    //    ProcessRun(PosConst.HAPPYBUZZ_BIZ_TYPE.CAN);    //등록취소
                }


            }
            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 btnExit_Click(object sender, EventArgs e)
        {

            if (alPagerNo != null && alPagerNo.Count > 0)
            {
                int iRow = alPagerNo.Count - 1;
                m_sPosMenuKeyOut = string.Format("{0}{1}", "B", alPagerNo[iRow].ToString());
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }



            //if (m_sPagerNo != "")
            //{
            //    m_sPosMenuKeyOut = "B" + m_sPagerNo;
            //    this.DialogResult = DialogResult.OK;
            //    this.Close();
            //}
            //else
            //{
            //    this.DialogResult = DialogResult.Cancel;
            //    this.Close();
            //}


        }
        /// <summary>
        /// 실행타이머
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tmrReqst_Tick(object sender, EventArgs e)
        {
            tmrReqst.Enabled = false;

            try
            {
                if (m_sPosMenuKeyIn == PosKey.MENU_KEY.HAPPY_BUZ_REG)
                {
                    ProcessRun(PosConst.HAPPYBUZZ_BIZ_TYPE.SET);
                }
                else
                {
                    ProcessRun(PosConst.HAPPYBUZZ_BIZ_TYPE.WEB);
                }
            }
            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 User Method
        /// <summary>
        /// 업무 실행
        /// </summary>
        /// <param name="sProcessType"></param>
        private void ProcessRun(string sProcessType)
        {
            try
            {

                if (sProcessType == PosConst.HAPPYBUZZ_BIZ_TYPE.SET)
                {
                    //등록 처리
                    IHappyBuzz m_cHappyBuzz = (IHappyBuzz)sManager.InitServiceInstance(ServiceLists.BSV_SALE.DLL, ServiceLists.BSV_SALE.HAPPYBUZZ);

                    string[] aStr = new string[3];

                    int iRet = m_cHappyBuzz.SetHappyBuzz(new string[] { m_sCardNo }, ref aStr);

                    if (iRet == UserCom.OK)
                    {
                        //m_sPagerNo = aStr[2].ToString();
                        //lblDspMsg.Text = string.Format(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0531), m_sPagerNo);
                        alPagerNo.Add(aStr[2].ToString());
                        lblDspMsg.Text = string.Format(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0531), aStr[2].ToString());
                        btnAdd.Visible = true;
                        btnCan.Visible = true;
                    }
                    else if (iRet == UserCom.NG1)
                    {
                        lblDspMsg.Text = MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0532) + "\r\n" + MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0676);
                    }
                    else
                    {
                        lblDspMsg.Text = MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0532);
                    }
                }
                else if (sProcessType == PosConst.HAPPYBUZZ_BIZ_TYPE.CAN)
                {
                    //취소 처리
                    IHappyBuzz m_cHappyBuzz = (IHappyBuzz)sManager.InitServiceInstance(ServiceLists.BSV_SALE.DLL, ServiceLists.BSV_SALE.HAPPYBUZZ);

                    string[] aStr = new string[4];

                    int iRow = alPagerNo.Count - 1;
                    
                    int iRet = m_cHappyBuzz.CanHappyBuzz(new string[] { alPagerNo[iRow].ToString() }, ref aStr);

                    if (iRet == UserCom.OK)
                    {
                        lblDspMsg.Text = string.Format(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0533), alPagerNo[iRow].ToString());
                        alPagerNo.RemoveAt(iRow);
                        //m_sPagerNo = "";
                    }
                    else if (iRet == UserCom.NG1)
                    {
                        lblDspMsg.Text = MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0532) + "\r\n" + MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0676);
                    }
                    else
                    {
                        lblDspMsg.Text = MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0534);
                    }
                }
                else if (sProcessType == PosConst.HAPPYBUZZ_BIZ_TYPE.WEB)
                {

                    wbCtrl.Refresh();

                    //string sUrl = "http://buzz.happypointcard.co.kr/view/pos/list?storeCd={0}&subCd=0";
                    //wbCtrl.Navigate(string.Format(sUrl, "99931"));

                    string sUrl = "";
                    string stmp = (string)m_cWebBiz.GetData(new string[] { PosKey.MENU_KEY.HAPPY_BUZ_SEARCH });

                    if (string.IsNullOrEmpty(stmp) == true)
                    {
                        WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0676);
                    }
                    else
                    {
                        sUrl = CmUtil.MidH(stmp, 31, 100).Trim() + CmUtil.MidH(stmp, 131, 100).Trim();
                        wbCtrl.Navigate(sUrl);
                        wbCtrl.Refresh();
                        wbCtrl.Visible = true;
                    }
                }
            }
            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
        
    }
}
