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 INT51DB_ImageDll;
using INT51DB.Forms;
using INT51DB.DialogForms;

namespace INT51DB.Controls
{
    public partial class ControlMainDisplayFunction1 : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion

        #region Constructor
        public ControlMainDisplayFunction1(FormMainDisplay parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();
        } 
        #endregion

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

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

            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else
            {
            }
        }
        private void DefaultSetting()
        {
          
        }

        private void UpdateControl(JudgmentSetItem item)
        {
            string value = "";

            #region Judgment
            value = item.UpDelayTime.ToString();
            if (this.labelUpDelayTime.Text != value)
                this.labelUpDelayTime.Text = value;

            value = item.DownDelayTime.ToString();
            if (this.labelDownDelayTime.Text != value)
                this.labelDownDelayTime.Text = value;
            #endregion
        }

        public void DisplayRefresh(SystemStatus status)
        {
            if (status.Equipment == DataStore.EquipmentStatus.Start)
            {
                this.buttonServoDown.Enabled = false;
                this.buttonServoUp1.Enabled = false;
                this.buttonServoUp2.Enabled = false;
            }
            else
            {
                this.buttonServoDown.Enabled = true;
                this.buttonServoUp1.Enabled = true;
                this.buttonServoUp2.Enabled = true;
            }

            this.UpdateControl(this.ParentForm.ParentForm.CurrentJudgmentSetItem);
        }
        #endregion

        #region Event Handler
        private void buttonServoUp1_Click(object sender, EventArgs e)
        {
            DialogFormYesNo msg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.Language, 14);
            if (msg.ShowDialog() == DialogResult.Yes)
            {
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorUp, CommunicationID.MainBoard);
                this.ParentForm.VisibleMenuButton(true);
            }
        }
        private void buttonServoUp2_Click(object sender, EventArgs e)
        {
            DialogFormYesNo msg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.Language, 15);
            if (msg.ShowDialog() == DialogResult.Yes)
            {
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorUp2, CommunicationID.MainBoard);
                this.ParentForm.VisibleMenuButton(false);
            }
        }
        private void buttonServoDown_Click(object sender, EventArgs e)
        {
            DialogFormYesNo msg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.Language, 13);
            if (msg.ShowDialog() == DialogResult.Yes)
            {
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorDown, CommunicationID.MainBoard);
                this.ParentForm.VisibleMenuButton(true);
            }
        }
        private void buttonServoSetup_Click(object sender, EventArgs e)
        {
            DataStore.UserGroup group = DataStore.UserGroup.LogOff;

            DialogFormPasswordKeyPad password = new DialogFormPasswordKeyPad(this.ParentForm.ParentForm.SystemConfig.Language, this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUser);
            if (password.ShowDialog() == DialogResult.OK)
            {
                group = password.Group;

                if (group == DataStore.UserGroup.Level3Manager || group == DataStore.UserGroup.Level4Developer)
                {
                    this.ParentForm.MainDisplayFunction2.BringToFront();
                    this.ParentForm.MainDisplayFunction2.DisplayRefresh(this.ParentForm.ParentForm.CurrentSystemStatus);
                    this.ParentForm.MainDisplayFunction2.Visible = true;
                }
            }
        }

        private void labelUpDelayTime_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelUpDelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    message = "0~9999";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelUpDelayTime.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.UpDelayTime = myKeyPad.IntValue;

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.UpDelayTime, value);

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);
                }
            }
        }
        private void labelDownDelayTime_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDownDelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    message = "0~9999";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelDownDelayTime.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.DownDelayTime = myKeyPad.IntValue;

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.DownDelayTime, value);

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);
                }
            }
        }
        #endregion
    }
}
