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_6CH.DialogForms;
using INT63DC_ImageDll;
using INT63DC_6CH.In_UserManager;

namespace INT63DC_6CH.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()
        {
            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 = "Equipment settings";

                    this.labelStaticLane.Text = "Lane";
                    this.labelStaticOrder.Text = "Order";
                    this.labelStaticIndividualNG.Text = "Individual NG";
                    this.labelStaticLanguage.Text = "Language";
                    this.labelStaticSorter1Location.Text = "Sorter1 Location";
                    this.labelStaticSorter2Location.Text = "Sorter2 Location";
                    this.labelStaticLogin.Text = "Login";
                    this.labelStaticWeightLimitIgnoreCount.Text = "Weight limit - Ignore count";
                    this.labelTitleStoragePeriod.Text = "Storage Period";

                    this.labelInfo1.Text = "※ Sorting NG products by weight";
                    this.labelInfo2.Text = "※ Normal product (selected under quantity)";

                    this.labelWarning1.Text = "※ After changing setting, Turn the power off and back on";
                    this.labelWarning2.Text = "※ Do not change the value except engineer";

                    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.labelStaticLane.Text = "列";
                    this.labelStaticOrder.Text = "重量显示";
                    this.labelStaticIndividualNG.Text = "单独NG";
                    this.labelStaticLanguage.Text = "语言";
                    this.labelStaticSorter1Location.Text = "分类机1 地点";
                    this.labelStaticSorter2Location.Text = "分类机2 地点";
                    this.labelStaticLogin.Text = "登錄";
                    this.labelStaticWeightLimitIgnoreCount.Text = "重量限制 - 忽略计数";
                    this.labelTitleStoragePeriod.Text = "储存期";

                    this.radioButtonWeightViewForward.Text = "正向(1~12)";
                    this.radioButtonWeightViewReverse.Text = "反向(12~1)";

                    this.labelInfo1.Text = "※ 重量不良产品筛选";
                    this.labelInfo2.Text = "※ 正常产品 (选择数量未达标)";

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

                    this.buttonEachNG.DownImage = this.buttonLogin.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnOn_84X25));
                    this.buttonEachNG.UpImage = this.buttonLogin.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnOff_84X25));

                    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í zařízení";

                    this.labelStaticLane.Text = "Řada";
                    this.labelStaticOrder.Text = "Zobrazení hmotnosti";
                    this.labelStaticIndividualNG.Text = "Individuální vyřazování";
                    this.labelStaticLanguage.Text = "Jazyk";
                    this.labelStaticSorter1Location.Text = "Vyřazovač1 umístění";
                    this.labelStaticSorter2Location.Text = "Vyřazovač2 umístění";
                    this.labelStaticLogin.Text = "Přihlásit se";
                    this.labelStaticWeightLimitIgnoreCount.Text = "hmotnostní limit - ignorovat počet";
                    this.labelTitleStoragePeriod.Text = "Doba uložení";

                    this.labelInfo1.Text = "※ Třídění NG produktů podle hmotnosti";
                    this.labelInfo2.Text = "※ Normální produkt (vybrán pod množstvím)";

                    this.labelWarning1.Text = "※ Po změně nastavení vypněte a znovu zapněte napájení";
                    this.labelWarning2.Text = "※ Nezměňujte hodnotu kromě inženýra";

                    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 = "Vorrichtungeinstellung";

                    this.radioButtonWeightViewForward.Text = "Vorwärts(1~12)";
                    this.radioButtonWeightViewReverse.Text = "Rückwärts(1~12)";

                    this.labelStaticLane.Text = "Reihe";
                    this.labelStaticSorter1Location.Text = "Sortierer1 Standort";
                    this.labelStaticSorter2Location.Text = "Sortierer2 Standort";
                    this.labelStaticLogin.Text = "Einloggen";
                    this.labelStaticIndividualNG.Text = "Individuelle Aussortierung";
                    this.labelStaticOrder.Text = "Gewichtsanzeige";
                    this.labelStaticLanguage.Text = "Sprache";
                    this.labelStaticWeightLimitIgnoreCount.Text = "Gewichtsgrenze - Zählung ignorieren";
                    this.labelTitleStoragePeriod.Text = "Speicherdauer";

                    this.labelInfo1.Text = "※ Sortieren von NG-Produkten nach Gewicht";
                    this.labelInfo2.Text = "※ Normales Produkt (ausgewählt unter Menge)";

                    this.labelWarning1.Text = "※ Stellen Sie nach dem Einstellen des Servoantriebs sicher, Mit dem Hauptschalter aus- und einschalten";
                    this.labelWarning2.Text = "※ Werte dürfen nur vom Servicetechniker des Herstellers geändert werden";

                    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.labelStaticLane.Text = "レㅡン";
                    this.labelStaticOrder.Text = "オㅡダㅡ";
                    this.labelStaticIndividualNG.Text = "個別NG";
                    this.labelStaticLanguage.Text = "言語";
                    this.labelStaticSorter1Location.Text = "ソㅡタㅡ 1 の場所";
                    this.labelStaticSorter2Location.Text = "ソㅡタㅡ 2 の場所";
                    this.labelStaticLogin.Text = "ログイン";
                    this.labelStaticWeightLimitIgnoreCount.Text = "重量制限 - 無視数量";
                    this.labelTitleStoragePeriod.Text = "保管期間";
                    
                    this.radioButtonWeightViewForward.Text = "順方向(1~12)";
                    this.radioButtonWeightViewReverse.Text = "逆方向(12~1)";

                    this.labelInfo1.Text = "※ 重量NG製品選別";
                    this.labelInfo2.Text = "※ 正常製品数量未達選別";

                    this.labelWarning1.Text = "※ 設定変更後は電源を入れ直してください";
                    this.labelWarning2.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;
            }
        }
        private void InitializeControl()
        {
            string value = "";

            // 저장 기한
            if (this.ParentForm.SystemConfig.IsPart11 == true)
            {
                this.labelTitleStoragePeriod.Visible = true;
                this.labelStoragePeriod.Visible = true;

                value = this.ParentForm.SystemConfig.StoragePeriod.ToString();
                if (this.labelStoragePeriod.Text != value)
                    this.labelStoragePeriod.Text = value;
            }

            // 장비 열
            this.comboBoxEquipmentColumn.Items.Clear();
            this.comboBoxEquipmentColumn.Items.Add(6);
            this.comboBoxEquipmentColumn.Items.Add(7);
            this.comboBoxEquipmentColumn.Items.Add(8);
            this.comboBoxEquipmentColumn.Items.Add(10);
            this.comboBoxEquipmentColumn.Items.Add(12);
            this.comboBoxEquipmentColumn.SelectedItem = this.ParentForm.SystemConfig.EquipmentColumns;

            // 선별기1 위치
            value = this.ParentForm.SystemConfig.Sorter1Location.ToString();
            if (this.labelSorter1Location.Text != value)
                this.labelSorter1Location.Text = value;

            // 선별기2 위치
            value = this.ParentForm.SystemConfig.Sorter2Location.ToString();
            if (this.labelSorter2Location.Text != value)
                this.labelSorter2Location.Text = value;

            // Weight Limit 무시수량
            value = this.ParentForm.SystemConfig.WeightLimitIgnoreCount.ToString();
            if (this.labelWeightLimitIgnoreCount.Text != value)
                this.labelWeightLimitIgnoreCount.Text = value;

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

            // 개별 NG
            if (this.ParentForm.SystemConfig.IsEachNG == true)
                this.buttonEachNG.ButtonDown();
            else
                this.buttonEachNG.ButtonUp();

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

            // 언어
            if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
                this.radioButtonLanguageChinese.Checked = true;
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
                this.radioButtonLanguageCzech.Checked = true;
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
                this.radioButtonLanguageEnglish.Checked = true;
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
                this.radioButtonLanguageKorean.Checked = true;
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.German)
                this.radioButtonLanguageGerman.Checked = true;
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Japanese)
                this.radioButtonLanguageJap.Checked = true;
            else
                this.radioButtonLanguageKorean.Checked = true;

            this.buttonSave.Visible = false;
        }

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

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

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

            this.InitializeDesign();
        }
        #endregion

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

        private void buttonSave_Click(object sender, EventArgs e)
        {
            // 저장 기한
            this.ParentForm.SystemConfig.StoragePeriod = int.Parse(this.labelStoragePeriod.Text);

            if (this.comboBoxEquipmentColumn.SelectedIndex == 0)
                this.ParentForm.SystemConfig.EquipmentColumns = 6;
            else if (this.comboBoxEquipmentColumn.SelectedIndex == 1)
                this.ParentForm.SystemConfig.EquipmentColumns = 7;
            else if (this.comboBoxEquipmentColumn.SelectedIndex == 2)
                this.ParentForm.SystemConfig.EquipmentColumns = 8;
            else if (this.comboBoxEquipmentColumn.SelectedIndex == 3)
                this.ParentForm.SystemConfig.EquipmentColumns = 10;
            else
                this.ParentForm.SystemConfig.EquipmentColumns = 12;

            // 선별기1 위치
            this.ParentForm.SystemConfig.Sorter1Location = int.Parse(this.labelSorter1Location.Text);

            // 선별기2 위치
            this.ParentForm.SystemConfig.Sorter2Location = int.Parse(this.labelSorter2Location.Text);

            // Weight Limit 무시수량
            this.ParentForm.SystemConfig.WeightLimitIgnoreCount = int.Parse(this.labelWeightLimitIgnoreCount.Text);

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

                    // Delete user list
                    UserManager.UserManager_UserAllDel();
                }
            }

            // 개별 NG
            if (this.buttonEachNG.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.SystemConfig.IsEachNG = true;
            else
                this.ParentForm.SystemConfig.IsEachNG = false;

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

            // 언어
            if (this.radioButtonLanguageKorean.Checked == true)
                this.ParentForm.SystemConfig.Language = DataStore.E_LanguageID.Korean;
            else if (this.radioButtonLanguageEnglish.Checked == true)
                this.ParentForm.SystemConfig.Language = DataStore.E_LanguageID.English;
            else if (this.radioButtonLanguageChinese.Checked == true)
                this.ParentForm.SystemConfig.Language = DataStore.E_LanguageID.Chinese;
            else if (this.radioButtonLanguageCzech.Checked == true)
                this.ParentForm.SystemConfig.Language = DataStore.E_LanguageID.Czech;
            else if (this.radioButtonLanguageGerman.Checked == true)
                this.ParentForm.SystemConfig.Language = DataStore.E_LanguageID.German;
            else if (this.radioButtonLanguageJap.Checked == true)
                this.ParentForm.SystemConfig.Language = DataStore.E_LanguageID.Japanese;
            else
                this.ParentForm.SystemConfig.Language = DataStore.E_LanguageID.Korean;

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

            DialogFormMessage myMsg = new DialogFormMessage(18, this.ParentForm.SystemConfig.Language);
            myMsg.ShowDialog();

            this.groupBoxSpecialMenu.Enabled = false;
            this.buttonSave.Enabled = false;
            this.buttonBack.Visible = false;
        }
        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }

        private void labelSorter1Location_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorter1Location.Text, 4, 0, false, this.ParentForm.SystemConfig.Language);

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

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void labelSorter2Location_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorter2Location.Text, 4, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.StringValue == this.labelSorter1Location.Text)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelSorter2Location.Text = myKeyPad.StringValue;

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void labelWeightLimitIgnoreCount_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelWeightLimitIgnoreCount.Text, 2, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > 99)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelWeightLimitIgnoreCount.Text = myKeyPad.StringValue;

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void labelStoragePeriod_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelStoragePeriod.Text, 2, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    if (this.ParentForm.SystemConfig.IsPart11 == true)
                    {
                        if (myKeyPad.doubleValue > 24)
                            this.labelStoragePeriod.Text = "24";
                        else
                            this.labelStoragePeriod.Text = myKeyPad.StringValue;
                    }
                    else
                    {
                        if (myKeyPad.doubleValue > 12)
                            this.labelStoragePeriod.Text = "12";
                        else
                            this.labelStoragePeriod.Text = myKeyPad.StringValue;
                    }

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