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

namespace INT89DB_26.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()
        {
            Class1 images = new Class1();

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

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
            {
                this.labelTitle.Text = "Time setting";
                this.groupBoxPresentTime.Text = "Present time";
                this.groupBoxTimeChange.Text = "Time change";

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

                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
            {
                this.labelTitle.Text = "时间设定";
                this.groupBoxPresentTime.Text = "当前时间";
                this.groupBoxTimeChange.Text = "设置";

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

                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {

            }
            else
            {

            }
        }

        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)
        {
            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();
                return;
            }

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

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