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 INT63DC_ImageDll;

namespace INT63DC_2C.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;
        }
        #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();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    break;
                case DataStore.E_LanguageID.English:
                    #region English
                    this.labelTitle.Text = "Time settings";
                    this.labelStaticCurrentTime.Text = "Current time";
                    this.labelStaticTimeSetting.Text = "Setting time";

                    this.buttonSave.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engSaveDisable));
                    this.buttonSave.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engSaveDown));
                    this.buttonSave.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engSaveUp));

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackUp)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Chinese:
                    #region Chinese
                    this.labelTitle.Text = "时间设定";
                    this.labelStaticCurrentTime.Text = "当前时间";
                    this.labelStaticTimeSetting.Text = "设置";

                    this.buttonSave.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnSaveDisable));
                    this.buttonSave.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnSaveDown));
                    this.buttonSave.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnSaveUp));

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackUp)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Czech:
                    #region Czech
                    this.labelTitle.Text = "Nastavení času";
                    this.labelStaticCurrentTime.Text = "Aktuální čas";
                    this.labelStaticTimeSetting.Text = "Nastavení";

                    this.buttonSave.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeSaveDisable));
                    this.buttonSave.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeSaveDown));
                    this.buttonSave.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeSaveUp));

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackUp)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.German:
                    #region German
                    this.labelTitle.Text = "Zeiteinstellung";
                    this.labelStaticCurrentTime.Text = "Aktuelle Uhrzeit";
                    this.labelStaticTimeSetting.Text = "Einstellung";

                    this.buttonSave.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerSaveDisable));
                    this.buttonSave.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerSaveDown));
                    this.buttonSave.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerSaveUp));

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackUp)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Japanese:
                    #region Japanese
                    this.labelTitle.Text = "時間設定";
                    this.labelStaticCurrentTime.Text = "現在時刻";
                    this.labelStaticTimeSetting.Text = "設定";

                    this.buttonSave.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnSaveDisable));
                    this.buttonSave.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnSaveDown));
                    this.buttonSave.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnSaveUp));

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackUp));
                    #endregion
                    break;
                default:
                    break;
            }
        }

        public void DisplayRefresh()
        {
            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 Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.InitializeDesign();
        }
        #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 != "" && before != after)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.ChangeTime, "", before, after);
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            if (this.TimerForTimeSetting.Enabled != true)
                this.TimerForTimeSetting.Enabled = true;

            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }
        #endregion
    }
}