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

using INT69DB_2A_ImageDll;

namespace INT69DB_2A.Forms
{
    public partial class FormTimeSetting : Form
    {
        #region Field
        private FormMain m_ParentForm;

        private DateTime CurrentTime;
        #endregion

        #region Constructor
        public FormTimeSetting(FormMain parent)
        {
            InitializeComponent();

            this.ParentForm = parent;
            this.TimerForTimeSetting.Enabled = true;

            this.InitializeDesign();
        }
        #endregion

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

        #region Method
        private void InitializeDesign()
        {
            ImageDll images = new ImageDll();

            if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.labelTitle.Text = "Time";
                this.groupBoxPresentTime.Text = "Present time";
                this.groupBoxTimeChange.Text = "Time change";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "时间设定";
                this.groupBoxPresentTime.Text = "当前时间";
                this.groupBoxTimeChange.Text = "设置";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitle.Text = "時間";
                this.groupBoxPresentTime.Text = "現在時刻";
                this.groupBoxTimeChange.Text = "時間の変更";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {

            }
            else
            {

            }
        }

        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormTimeSetting;
            this.CurrentTime = DateTime.Now;

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

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

            this.buttonSave.Visible = false;
        }
        #endregion

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

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

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

            this.ParentForm.ChildFormMainDisplay.UpdateDisplayTime();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            string before = "", after = "";

            before = DateTime.Now.ToString("yyyy-MM-dd HH:mm");

            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);
                after = setDateTime.ToString("yyyy-MM-dd HH:mm");
            }
            catch
            {
                this.DisplayRefresh();
                return;
            }

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

            if (this.ParentForm.SystemConfig.IsPart11 == true)
                if (after != "")
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.ChangeTime, "", before, after);
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }
        #endregion
    }
}