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

using INT89DB_26.DialogForms;
using INT89DB_26_ImageDll;

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

        #endregion

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

            this.ParentForm = parent;
        } 
        #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.English)
            {
                this.labelTitle.Text = "Equipment setting";

                this.labelStaticEquipmentLine.Text = "Line";
                this.labelStaticWeightCheck.Text = "Order";
                this.labelStaticEquipmentID.Text = "Equipment ID";
                this.labelStaticLanguage.Text = "Language";
                this.labelStaticInverterCNT.Text = "Inverter QTY";

                this.labelStaticLogin.Text = "Login";

                this.labelBuadrateWarning.Text = "※ After changing setting, Turn OFF the power and then turn ON the power";
                this.labelBuadrateWarning2.Text = "※ Do not change the value except engineer";

                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.groupBoxSpecialMenu.Text = "设备设置";
                this.labelStaticEquipmentLine.Text = "列";
                this.labelStaticWeightCheck.Text = "重量显示";
                this.labelStaticEquipmentID.Text = "设备编号";
                this.labelStaticLanguage.Text = "语言";
                this.labelStaticInverterCNT.Text = "逆變器數量";

                this.labelStaticLogin.Text = "登錄";
                this.buttonSave.Text = "保存";
                this.buttonBack.Text = "返回";

                this.labelBuadrateWarning.Text = "※ 电源重启后，设置会自动更新";
                this.labelBuadrateWarning2.Text = "※ 除了工程师之外，不要改变这个值";

                this.buttonLogin.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnEnable));
                this.buttonLogin.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnDisable));
                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
            {

            }
        }
        private void InitializeControl()
        {
            // 장비 열
            this.comboBoxEquipmentColumn.Items.Clear();
            this.comboBoxEquipmentColumn.Items.Add(2);
            this.comboBoxEquipmentColumn.Items.Add(3);
            this.comboBoxEquipmentColumn.Items.Add(4);
            this.comboBoxEquipmentColumn.Items.Add(5);
            this.comboBoxEquipmentColumn.Items.Add(6);
            this.comboBoxEquipmentColumn.SelectedItem = this.ParentForm.SystemConfig.EquipmentColumns;

            // 인버터 개수
            this.comboBoxInverterCNT.Items.Clear();
            this.comboBoxInverterCNT.Items.Add(1);
            this.comboBoxInverterCNT.Items.Add(2);
            this.comboBoxInverterCNT.Items.Add(3);
            this.comboBoxInverterCNT.Items.Add(4);
            this.comboBoxInverterCNT.SelectedItem = this.ParentForm.SystemConfig.InverterQTY;

            // 장비 ID
            if (this.labelEquipmentID.Text != this.ParentForm.SystemConfig.EquipmentID.ToString())
                this.labelEquipmentID.Text = this.ParentForm.SystemConfig.EquipmentID.ToString();

            // 언어
            if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
                this.radioButtonKorean.Checked = true;
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
                this.radioButtonEnglish.Checked = true;
            else
                this.radioButtonChinese.Checked = true;

            // 로그인
            if (this.ParentForm.SystemConfig.IsLogin == true)
                this.buttonLogin.ButtonDown();
            else
                this.buttonLogin.ButtonUp();

            this.buttonSave.Visible = false;
        }

        public void DisplayRefresh()
        {
            this.InitializeControl();

            this.ParentForm.SystemConfig.CurrentForm = DataStore.E_FormStore.FormEquipmentSetting;
        }
        #endregion

        #region Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

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

        #region Event Handler
        private void control_Click(object sender, EventArgs e)
        {
            if (this.buttonSave.Visible != true)
                this.buttonSave.Visible = true;
        }

        private void labelEquipmentID_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelEquipmentID.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (this.labelEquipmentID.Text != myKeyPad.StringValue)
                {
                    this.labelEquipmentID.Text = myKeyPad.StringValue;

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            // 장비 타입   0:컨베어, 1:스틱1, 2:스틱2, 3:스틱3, 4:업다운
            this.ParentForm.SystemConfig.EquipmentMode = 0;

            // 장비 열
            if (this.comboBoxEquipmentColumn.SelectedIndex == 0)
                this.ParentForm.SystemConfig.EquipmentColumns = 2;
            else if (this.comboBoxEquipmentColumn.SelectedIndex == 1)
                this.ParentForm.SystemConfig.EquipmentColumns = 3;
            else if (this.comboBoxEquipmentColumn.SelectedIndex == 2)
                this.ParentForm.SystemConfig.EquipmentColumns = 4;
            else if (this.comboBoxEquipmentColumn.SelectedIndex == 3)
                this.ParentForm.SystemConfig.EquipmentColumns = 5;
            else
                this.ParentForm.SystemConfig.EquipmentColumns = 6;

            // 인버터 개수
            this.ParentForm.SystemConfig.InverterQTY = int.Parse(this.comboBoxInverterCNT.SelectedItem.ToString());

            // 중량표시
            if (this.radioButtonWeightViewForward.Checked == true)
                this.ParentForm.SystemConfig.IsWeightViewForward = true;
            else
                this.ParentForm.SystemConfig.IsWeightViewForward = false;

            // 장비 ID
            this.ParentForm.SystemConfig.EquipmentID = int.Parse(this.labelEquipmentID.Text);

            // 언어
            if (this.radioButtonKorean.Checked == true)
                this.ParentForm.SystemConfig.Language = DataStore.E_LanguageID.Korean;
            else if (this.radioButtonEnglish.Checked == true)
                this.ParentForm.SystemConfig.Language = DataStore.E_LanguageID.English;
            else
                this.ParentForm.SystemConfig.Language = DataStore.E_LanguageID.Chinese;

            // 로그인
            if (this.buttonLogin.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.SystemConfig.IsLogin = true;
            else
                this.ParentForm.SystemConfig.IsLogin = false;


            // SystemConfig 파일 저장
            this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);
            Thread.Sleep(100);

            DialogFormMessage myMsg = new DialogFormMessage(18, this.ParentForm.SystemConfig.Language, "");
            myMsg.ShowDialog();
            this.groupBoxSpecialMenu.Enabled = false;
            this.buttonBack.Visible = false;
            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
    }
}