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

using ITC81DB_2.Forms;
using ITC81DB_2_ImageDll;

namespace ITC81DB_2.Controls
{
    public partial class ControlCenterBasicTime : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        #endregion
        
        #region Constructor
        public ControlCenterBasicTime(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();

            this.timer.Enabled = true;
        } 
        #endregion

        #region Property
        public FormMenu ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        } 
        #endregion

        #region Method
        public void InitializeDesign()
        {
            Class1 images = new Class1();

            if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
            {
                this.labelTitleCurrentTime.Text = "Current time";
                this.labelTitleChangeTime.Text = "Change";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.labelTitleCurrentTime.Text = "当前时间";
                this.labelTitleChangeTime.Text = "时间改变";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.labelTitleCurrentTime.Text = "Tiempo actual";
                this.labelTitleChangeTime.Text = "Cambiar";
            }
            else
            {
                this.labelTitleCurrentTime.Text = "현재시간";
                this.labelTitleChangeTime.Text = "시간변경";
            }
        }
        private void DefaultSetting()
        {

        }

        public void DisplayRefresh(SystemStatus status)
        {
            DateTime currentTime = DateTime.Now;

            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_DisplayStore.BasicTime;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.labelCurrentTime.Text = currentTime.ToString("yyyy-MM-dd HH:mm:ss");

            this.upDownYear.Value = currentTime.Year;
            this.upDownMonth.Value = currentTime.Month;
            this.upDownDate.Value = currentTime.Day;
            this.upDownHour.Value = currentTime.Hour;
            this.upDownMinute.Value = currentTime.Minute;
            this.timer.Enabled = true;

            this.buttonSave.Visible = false;
        }
        #endregion

        #region Event Handler
        private void buttonTimeSetting_Click(object sender, EventArgs e)
        {
            this.timer.Enabled = false;
            this.buttonSave.Visible = true;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            DateTime setDateTime;
            try
            {
                setDateTime = new DateTime(int.Parse(this.upDownYear.TxtValue), int.Parse(this.upDownMonth.TxtValue),
                    int.Parse(this.upDownDate.TxtValue), int.Parse(this.upDownHour.TxtValue), int.Parse(this.upDownMinute.TxtValue), 00);
            }
            catch
            {
                this.DisplayRefresh(this.ParentForm.ParentForm.CurrentSystemStatus);
                return;
            }

            this.ParentForm.ParentForm.smartConfigs.ControlPanel.SetSystemDateTime(setDateTime);
            this.timer.Enabled = true;
            this.buttonSave.Visible = false;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            DateTime currentTime = DateTime.Now;

            this.labelCurrentTime.Text = currentTime.ToString("yyyy-MM-dd HH:mm:ss");
            if (this.upDownMinute.TxtValue != Helper.StringZeroFillDigits2(currentTime.Minute.ToString()))
            {
                this.upDownHour.Value = currentTime.Hour;
                this.upDownMinute.Value = currentTime.Minute;
            }

            this.ParentForm.ParentForm.ChildFormMainDisplay.UpdateTimeDisplay();
        }
        #endregion
    }
}
