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_2H.Forms;
using ITC81DB_2H_ImageDll;
using ITC81DB_2H.Datastore;

namespace ITC81DB_2H.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.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundTime));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundTime));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundTime));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundTime));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundTime));
            }
        }
        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)
        {
            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(this.ParentForm.ParentForm.CurrentSystemStatus);
                return;
            }

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

            if (before != after)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.ChangeTime, "", before, after);
        }

        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
    }
}
