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.Collections;

using System.Net; 
using System.IO;

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

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 달력
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Win
{
    public partial class frmCalendar : 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_htDaysCtl;                                              // 일자 컨트롤 정보

        private string m_sChoiceDate = "";
        /// <summary>
        /// 선택 일자
        /// </summary>
        public string ChoiceDate { get { return this.m_sChoiceDate; } set { this.m_sChoiceDate = value; } }
        #endregion 변수 선언

        #region 생성자 & 소멸자
        public frmCalendar()
        {
            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 frmCalendar_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();  // 콘트롤 초기화 처리
            DisplayCalendar(DateTime.Now);
        }

        private void frmCalendar_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 InitControl()
        {
            try
            {
                //picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_FULL_SIZE);

                FormManager.SetFormAllControlFont(this, m_cPosStatus.Base.FONT);

                lblSaleDate.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0516);
                btnToDay.Text = m_cPosStatus.Global.DateToCulture(DateTime.Now.ToString("yyyyMMdd"));

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

                m_sChoiceDate = "";

                m_htDaysCtl = new Hashtable();
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays01);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays02);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays03);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays04);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays05);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays06);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays07);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays08);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays09);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays10);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays11);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays12);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays13);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays14);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays15);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays16);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays17);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays18);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays19);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays20);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays21);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays22);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays23);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays24);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays25);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays26);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays27);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays28);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays29);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays30);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays31);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays32);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays33);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays34);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays35);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays36);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays37);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays38);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays39);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays40);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays41);
                m_htDaysCtl.Add(m_htDaysCtl.Count, lblDays42);
            }
            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

        /// <summary>
        /// 버튼 입력 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFunc_Click(object sender, EventArgs e)
        {
            try
            {
                if (((Cosmos.UI.CsmButton)sender) == btnExit)
                {
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
                else if (((Cosmos.UI.CsmButton)sender) == btnUp)
                {
                    string sDate = lblTitle.Text.Substring(0, 4) + lblTitle.Text.Substring(5, 2) + "01";
                    DisplayCalendar(DateTime.ParseExact(sDate, "yyyyMMdd", null).AddMonths(1));
                }
                else if (((Cosmos.UI.CsmButton)sender) == btnDown)
                {
                    string sDate = lblTitle.Text.Substring(0, 4) + lblTitle.Text.Substring(5, 2) + "01";
                    DisplayCalendar(DateTime.ParseExact(sDate, "yyyyMMdd", null).AddMonths(-1));
                }
                else if (((Cosmos.UI.CsmButton)sender) == btnToDay)
                {
                    m_sChoiceDate = DateTime.Now.ToString("yyyyMMdd");
                    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 lblDays_Click(object sender, EventArgs e)
        {
            try
            {
                if (((Cosmos.UI.CsmLabel)sender).Text == "") return;

                m_sChoiceDate = lblTitle.Text.Substring(0, 4) + lblTitle.Text.Substring(5, 2) + string.Format("{0:00}", CmUtil.IntParse(((Cosmos.UI.CsmLabel)sender).Text));

                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="dtDate"></param>
        private void DisplayCalendar(DateTime dtDate)
        {
            try
            {
                string sDate = dtDate.ToString("yyyyMM");
                lblTitle.Text = sDate.Substring(0, 4) + "-" + sDate.Substring(4, 2);

                // 초기화
                foreach (object obj in m_htDaysCtl.Values)
                {
                    Cosmos.UI.CsmLabel btnCtl = (Cosmos.UI.CsmLabel)obj;
                    btnCtl.Text = "";
                    btnCtl.BackColor = Color.White;
                }

                // 해당월의 최대 일자 획득
                int nTotalDay = DateTime.DaysInMonth(dtDate.Year, dtDate.Month);

                // 해당월의 시작 요일 획득
                DateTime dtStart = new DateTime(dtDate.Year, dtDate.Month, 1);
                int nCtl = (int)dtStart.DayOfWeek;

                string sNowDate = DateTime.Now.ToString("yyyyMMdd");

                for (int nDay = 1; nDay <= nTotalDay; nDay++, nCtl++)
                {
                    Cosmos.UI.CsmLabel btnCtl = (Cosmos.UI.CsmLabel)m_htDaysCtl[nCtl];
                    btnCtl.Text = nDay.ToString();

                    if (sNowDate == sDate + string.Format("{0:D2}", nDay)) btnCtl.BackColor = Color.FromArgb(255, 251, 211); // Color.Yellow;
                }
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }   
    }
}
