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 INT99DC_7C.DialogForms;
using INT99DC_ImageDll;
using INT99DC_7C.Part11_UserManager;

namespace INT99DC_7C.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();

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

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                #region English
                this.labelTitle.Text = "Equipment settings";

                this.labelStaticType.Text = "Type";
                this.labelStaticLine.Text = "Line";
                this.labelStaticOrder.Text = "Order";
                this.labelStaticIndividualNG.Text = "Individual NG";
                this.labelStaticRejectCV.Text = "Reject conveyor";
                this.labelStaticRejectCVRotation.Text = "Reject conveyor rotation";
                this.labelStaticFeedingCV.Text = "Feeding conveyor";
                this.labelStaticEquipmentID.Text = "Equipment ID";
                this.labelStaticLanguage.Text = "Language";
                this.labelStaticInputSensorSelect.Text = "InputSensor select";
                this.labelStaticFeedingConveyorRun.Text = "Feeding conveyor select";
                this.labelStaticSorterMode.Text = "Sorter Mode";
                this.labelStaticEmergencyStopEntrySensorError.Text = "Emergency stop due to entry sensor error";
                this.labelStaticLogin.Text = "Login";
                this.labelStaticDataStoragePeriod.Text = "Data storage period";

                this.radioButtonEquipmentModeTube.Text = "Tube";

                this.labelBuadrateWarning.Text = "※ After changing setting, Turn the power off and back on";
                this.labelBuadrateWarning2.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
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                #region Chinese
                this.labelTitle.Text = "设备设置";

                this.labelStaticType.Text = "设备类型";
                this.labelStaticLine.Text = "列";
                this.labelStaticOrder.Text = "重量显示";
                this.labelStaticIndividualNG.Text = "单独NG";
                this.labelStaticRejectCV.Text = "筛选输送带";
                this.labelStaticRejectCVRotation.Text = "排放输送带旋转方向";
                this.labelStaticFeedingCV.Text = "进料输送带";
                this.labelStaticEquipmentID.Text = "设备编号";
                this.labelStaticLanguage.Text = "语言";
                this.labelStaticInputSensorSelect.Text = "选择输入传感器";
                this.labelStaticFeedingConveyorRun.Text = "进料输送带驱动";
                this.labelStaticSorterMode.Text = "检测仪模式";
                this.labelStaticEmergencyStopEntrySensorError.Text = "由于进入传感器错误导致紧急停止";
                this.labelStaticLogin.Text = "登錄";
                this.labelStaticDataStoragePeriod.Text = "数据保存期限";

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

                this.radioButtonEquipmentModeTube.Text = "Tube";

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

                this.buttonEachNG.DownImage = this.buttonLogin.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnEnable));
                this.buttonEachNG.UpImage = this.buttonLogin.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnDisable));
                this.buttonDischargeConveyor.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnEnable));
                this.buttonDischargeConveyor.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnDisable));
                this.buttonEmergencyStopEntrySensorError.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnEnable));
                this.buttonEmergencyStopEntrySensorError.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnDisable));
                this.buttonFeedingConveyor.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnEnable));
                this.buttonFeedingConveyor.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnDisable));

                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
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                #region Czech
                this.labelTitle.Text = "Nastavení zařízení";

                this.labelStaticType.Text = "Typ";
                this.labelStaticLine.Text = "Řada";
                this.labelStaticOrder.Text = "Zobrazení hmotnosti";
                this.labelStaticIndividualNG.Text = "Individuální vyřazování";
                this.labelStaticRejectCV.Text = "Vyřazovací dopravník";
                this.labelStaticRejectCVRotation.Text = "Směr otáčení";
                this.labelStaticFeedingCV.Text = "Přísunový dopravník";
                this.labelStaticEquipmentID.Text = "ID zařízení";
                this.labelStaticLanguage.Text = "Jazyk";
                this.labelStaticInputSensorSelect.Text = "Vstupní senzor";
                this.labelStaticFeedingConveyorRun.Text = "Chod";
                this.labelStaticSorterMode.Text = "Vyřazování Režim";
                this.labelStaticEmergencyStopEntrySensorError.Text = "Nouzové zastavení z důvodu chyby snímače";
                this.labelStaticLogin.Text = "Přihlásit se";
                this.labelStaticDataStoragePeriod.Text = "Doba uložení dat";

                this.radioButtonEquipmentModeTube.Text = "Tube";

                this.labelBuadrateWarning.Text = "※ Po změně nastavení vypněte a znovu zapněte napájení";
                this.labelBuadrateWarning2.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
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                #region German
                this.labelTitle.Text = "Vorrichtungeinstellung";

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

                this.radioButtonFeedingConveyorRunPass.Text = this.radioButtonSorterModePass.Text = "Bestehen";
                this.radioButtonFeedingConveyorRunNg.Text = this.radioButtonSorterModeNg.Text = "Leer";

                this.labelStaticType.Text = "Waagentyp";
                this.labelStaticLine.Text = "Reihe";
                this.labelStaticOrder.Text = "Gewichtsanzeige";
                this.labelStaticIndividualNG.Text = "Individuelle Aussortierung";
                this.labelStaticRejectCV.Text = "Aussortierförderer";
                this.labelStaticRejectCVRotation.Text = "Drehrichtung";
                this.labelStaticFeedingCV.Text = "Zuführförderer";
                this.labelStaticEquipmentID.Text = "ID-Vorrichtung";
                this.labelStaticLanguage.Text = "Sprache";
                this.labelStaticInputSensorSelect.Text = "Schnitt - Eingang";
                this.labelStaticFeedingConveyorRun.Text = "Zuführförderer auswählen";
                this.labelStaticSorterMode.Text = "Sortiermodus";
                this.labelStaticEmergencyStopEntrySensorError.Text = "Nothalt wegen Sensorfehler";
                this.labelStaticLogin.Text = "Anmelden";
                this.labelStaticDataStoragePeriod.Text = "Dauer der Datenspeicherung";

                this.radioButtonEquipmentModeTube.Text = "Tube";

                this.labelBuadrateWarning.Text = "※ Stellen Sie nach dem Einstellen des Servoantriebs sicher, Mit dem Hauptschalter aus- und einschalten";
                this.labelBuadrateWarning2.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
            }
            else
            {

            }
        }
        private void InitializeControl()
        {
            // 장비타입
            this.radioButtonEquipmentModeTube.Checked = true;

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

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

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

            // 입력센서 선택
            this.comboBoxInputSensorSelect.Items.Clear();
            this.comboBoxInputSensorSelect.Items.Add(1);
            this.comboBoxInputSensorSelect.Items.Add(2);
            this.comboBoxInputSensorSelect.Items.Add(3);
            this.comboBoxInputSensorSelect.Items.Add(4);
            this.comboBoxInputSensorSelect.Items.Add(5);
            this.comboBoxInputSensorSelect.Items.Add(6);
            this.comboBoxInputSensorSelect.Items.Add(7);
            this.comboBoxInputSensorSelect.Items.Add(8);
            this.comboBoxInputSensorSelect.Items.Add(9);
            this.comboBoxInputSensorSelect.Items.Add(10);
            this.comboBoxInputSensorSelect.Items.Add(11);
            this.comboBoxInputSensorSelect.Items.Add(12);
            this.comboBoxInputSensorSelect.SelectedItem = this.ParentForm.SystemConfig.InputSensorSelect;

            // 입력센서 연속입력 시 정지
            if (this.ParentForm.SystemConfig.IsEmergencyStopEntrySensorError == true)
                this.buttonEmergencyStopEntrySensorError.ButtonDown();
            else
                this.buttonEmergencyStopEntrySensorError.ButtonUp();

            // 배출 컨베어
            if (this.ParentForm.SystemConfig.IsDischargeConveyor == true)
                this.buttonDischargeConveyor.ButtonDown();
            else
                this.buttonDischargeConveyor.ButtonUp();

            // 배출 컨베어 회전방향
            if (this.ParentForm.SystemConfig.IsDischargeConveyorDirectionCW == true)
                this.radioButtonDischargeConveyorDirectionCW.Checked = true;
            else
                this.radioButtonDischargeConveyorDirectionCCW.Checked = true;

            // Feeding 컨베어
            if (this.ParentForm.SystemConfig.IsFeedingConveyor == true)
                this.buttonFeedingConveyor.ButtonDown();
            else
                this.buttonFeedingConveyor.ButtonUp();

            // Feeding 컨베어 구동
            if (this.ParentForm.SystemConfig.IsFeedingConveyorRunPass == true)
                this.radioButtonFeedingConveyorRunPass.Checked = true;
            else
                this.radioButtonFeedingConveyorRunNg.Checked = true;

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

            // 데이터 저장기간
            if (this.labelDataStoragePeriod.Text != this.ParentForm.SystemConfig.DataStoragePeriod.ToString())
                this.labelDataStoragePeriod.Text = this.ParentForm.SystemConfig.DataStoragePeriod.ToString();

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

            // 선별기 모드
            if (this.ParentForm.SystemConfig.IsSorterMode == false)
                this.radioButtonSorterModePass.Checked = true;
            else
                this.radioButtonSorterModeNg.Checked = true;

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

            // Part11 사용중이면 로그인 설정 못하도록 제어
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.buttonLogin.Enabled = false;
            else
                this.buttonLogin.Enabled = 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 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 labelDataStoragePeriod_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDataStoragePeriod.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.labelDataStoragePeriod.Text = myKeyPad.StringValue;

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            this.ParentForm.CloseSmartUartLink();
            this.ParentForm.UartCom3Close();

            // 장비 타입
            this.ParentForm.SystemConfig.EquipmentMode = 5;

            // 장비 열
            if (this.comboBoxEquipmentColumn.SelectedIndex == 0)
                this.ParentForm.SystemConfig.EquipmentColumns = 7;
            else if (this.comboBoxEquipmentColumn.SelectedIndex == 1)
                this.ParentForm.SystemConfig.EquipmentColumns = 8;
            else if (this.comboBoxEquipmentColumn.SelectedIndex == 2)
                this.ParentForm.SystemConfig.EquipmentColumns = 10;
            else if (this.comboBoxEquipmentColumn.SelectedIndex == 3)
                this.ParentForm.SystemConfig.EquipmentColumns = 12;
            else
                this.ParentForm.SystemConfig.EquipmentColumns = 15;

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

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

            // 입력센서 선택
            this.ParentForm.SystemConfig.InputSensorSelect = this.comboBoxInputSensorSelect.SelectedIndex + 1;

            // 입력센서 연속입력 시 정지
            if (this.buttonEmergencyStopEntrySensorError.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.SystemConfig.IsEmergencyStopEntrySensorError = true;
            else
                this.ParentForm.SystemConfig.IsEmergencyStopEntrySensorError = false;

            // 배출 컨베어 
            if (this.buttonDischargeConveyor.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.SystemConfig.IsDischargeConveyor = true;
            else
                this.ParentForm.SystemConfig.IsDischargeConveyor = false;

            // 배출 컨베어 회전방향
            if (this.radioButtonDischargeConveyorDirectionCW.Checked == true)
                this.ParentForm.SystemConfig.IsDischargeConveyorDirectionCW = true;
            else
                this.ParentForm.SystemConfig.IsDischargeConveyorDirectionCW = false;

            // Feeding 컨베어
            if (this.buttonFeedingConveyor.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.SystemConfig.IsFeedingConveyor = true;
            else
                this.ParentForm.SystemConfig.IsFeedingConveyor = false;

            // Feeding 컨베어 구동
            if (this.radioButtonFeedingConveyorRunPass.Checked == true)
                this.ParentForm.SystemConfig.IsFeedingConveyorRunPass = true;
            else
                this.ParentForm.SystemConfig.IsFeedingConveyorRunPass = false;

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

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

            // 선별기 모드
            if (this.radioButtonSorterModePass.Checked == true)
                this.ParentForm.SystemConfig.IsSorterMode = false;
            else
                this.ParentForm.SystemConfig.IsSorterMode = true;

            // 로그인
            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();
                }
            }

            // 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);
        }
        #endregion
    }
}