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 System.Drawing.Imaging;

using SmartX;

using ITC81DB_2.Forms;
using ITC81DB_2.DialogForms;
using ITC81DB_2_ImageDll;
using System.Threading;

namespace ITC81DB_2.Controls
{
    public partial class ControlCenterEquipEngineer : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
         #endregion

        #region Constructor
        public ControlCenterEquipEngineer(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();
        }
        #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();

            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);
            
            if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
            {
                this.labelTitleCommunicationLog.Text = "Log";

                this.labelTitleEquipmentType.Text = "Equipment Type";
                this.labelTitleOptionBoard.Text = "Option Board";
                this.buttonCapture.Text = "Capture";

                this.comboBoxEquipmentType.SelectedIndexChanged -= new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);
                this.comboBoxEquipmentType.Items.Clear();
                this.comboBoxEquipmentType.Items.Add("Conveyor");
                this.comboBoxEquipmentType.Items.Add("Updown");
                this.comboBoxEquipmentType.SelectedIndexChanged += new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);

                this.comboBoxOptionBoard.SelectedIndexChanged -= new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);
                this.comboBoxOptionBoard.Items.Clear();
                this.comboBoxOptionBoard.Items.Add("None");
                this.comboBoxOptionBoard.Items.Add("OPT1");
                //this.comboBoxOptionBoard.Items.Add("OPT2");
                this.comboBoxOptionBoard.SelectedIndexChanged += new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);

                //this.buttonCommunicationLog.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engON));
                //this.buttonCommunicationLog.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engOFF));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.labelTitleCommunicationLog.Text = "日志";

                this.labelTitleEquipmentType.Text = "设备类型";
                this.labelTitleOptionBoard.Text = "选项板";
                this.buttonCapture.Text = "捕获";

                this.comboBoxEquipmentType.SelectedIndexChanged -= new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);
                this.comboBoxEquipmentType.Items.Clear();
                this.comboBoxEquipmentType.Items.Add("输送带");
                this.comboBoxEquipmentType.Items.Add("上下操作");
                this.comboBoxEquipmentType.SelectedIndexChanged += new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);

                this.comboBoxOptionBoard.SelectedIndexChanged -= new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);
                this.comboBoxOptionBoard.Items.Clear();
                this.comboBoxOptionBoard.Items.Add("不使用");
                this.comboBoxOptionBoard.Items.Add("OPT1");
                //this.comboBoxOptionBoard.Items.Add("OPT2");
                this.comboBoxOptionBoard.SelectedIndexChanged += new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);

                //this.buttonCommunicationLog.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnON));
                //this.buttonCommunicationLog.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnOFF));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.labelTitleCommunicationLog.Text = "Log";

                this.labelTitleEquipmentType.Text = "Tipo de equipo";
                this.labelTitleOptionBoard.Text = "Tablero opcional";
                this.buttonCapture.Text = "Capture";

                this.comboBoxEquipmentType.SelectedIndexChanged -= new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);
                this.comboBoxEquipmentType.Items.Clear();
                this.comboBoxEquipmentType.Items.Add("Transportador");
                this.comboBoxEquipmentType.Items.Add("Arriba abajo");
                this.comboBoxEquipmentType.SelectedIndexChanged += new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);

                this.comboBoxOptionBoard.SelectedIndexChanged -= new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);
                this.comboBoxOptionBoard.Items.Clear();
                this.comboBoxOptionBoard.Items.Add("Ninguna");
                this.comboBoxOptionBoard.Items.Add("OPT1");
                //this.comboBoxOptionBoard.Items.Add("OPT2");
                this.comboBoxOptionBoard.SelectedIndexChanged += new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);

                //this.buttonCommunicationLog.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnON));
                //this.buttonCommunicationLog.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnOFF));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Russian)
            {
                this.labelTitleCommunicationLog.Text = "Журнал обмена";

                this.labelTitleEquipmentType.Text = "Тип оборудования";
                this.labelTitleOptionBoard.Text = "Дополнительная плата";
                this.buttonCapture.Text = "Заголовок";

                this.comboBoxEquipmentType.SelectedIndexChanged -= new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);
                this.comboBoxEquipmentType.Items.Clear();
                this.comboBoxEquipmentType.Items.Add("Конвейер");
                this.comboBoxEquipmentType.Items.Add("Вверх-вниз");
                this.comboBoxEquipmentType.SelectedIndexChanged += new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);

                this.comboBoxOptionBoard.SelectedIndexChanged -= new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);
                this.comboBoxOptionBoard.Items.Clear();
                this.comboBoxOptionBoard.Items.Add("Нет");
                this.comboBoxOptionBoard.Items.Add("OPT1");
                //this.comboBoxOptionBoard.Items.Add("OPT2");
                this.comboBoxOptionBoard.SelectedIndexChanged += new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);

                //this.buttonCommunicationLog.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusON));
                //this.buttonCommunicationLog.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusOFF));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.German)
            {
                this.labelTitleCommunicationLog.Text = "Protokoll";

                this.labelTitleEquipmentType.Text = "Waagentyp";
                this.labelTitleOptionBoard.Text = "Optionsplatine";
                this.buttonCapture.Text = "Fang";

                this.comboBoxEquipmentType.SelectedIndexChanged -= new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);
                this.comboBoxEquipmentType.Items.Clear();
                this.comboBoxEquipmentType.Items.Add("Förderer");
                this.comboBoxEquipmentType.Items.Add("Obenunten");
                this.comboBoxEquipmentType.SelectedIndexChanged += new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);

                this.comboBoxOptionBoard.SelectedIndexChanged -= new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);
                this.comboBoxOptionBoard.Items.Clear();
                this.comboBoxOptionBoard.Items.Add("Keiner");
                this.comboBoxOptionBoard.Items.Add("OPT1");
                //this.comboBoxOptionBoard.Items.Add("OPT2");
                this.comboBoxOptionBoard.SelectedIndexChanged += new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);

                //this.buttonCommunicationLog.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerON));
                //this.buttonCommunicationLog.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerOFF));
            }
            else
            {
                this.labelTitleCommunicationLog.Text = "통신로그";

                this.labelTitleEquipmentType.Text = "장비타입";
                this.labelTitleOptionBoard.Text = "옵션보드";
                this.buttonCapture.Text = "Capture";

                this.comboBoxEquipmentType.SelectedIndexChanged -= new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);
                this.comboBoxEquipmentType.Items.Clear();
                this.comboBoxEquipmentType.Items.Add("컨베어");
                this.comboBoxEquipmentType.Items.Add("업다운");
                this.comboBoxEquipmentType.SelectedIndexChanged += new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);

                this.comboBoxOptionBoard.SelectedIndexChanged -= new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);
                this.comboBoxOptionBoard.Items.Clear();
                this.comboBoxOptionBoard.Items.Add("None");
                this.comboBoxOptionBoard.Items.Add("OPT1");
                //this.comboBoxOptionBoard.Items.Add("OPT2");
                this.comboBoxOptionBoard.SelectedIndexChanged += new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);

                //this.buttonCommunicationLog.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engON));
                //this.buttonCommunicationLog.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engOFF));
            }
        }
        private void DefaultSetting()
        {

        }

        private void UpdateParameterDisplay(SystemConfigurationItem item, SystemParameter9507 system)
        {
            int index = 0;
            string value = "";

            // 장비타입
            this.comboBoxEquipmentType.SelectedIndexChanged -= new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);
            switch (item.EquipmentType)
            {
                case 0: // 컨베어
                    this.comboBoxEquipmentType.SelectedIndex = 0;
                    break;
                case 4: // 업다운
                    this.comboBoxEquipmentType.SelectedIndex = 1;
                    break;
                default:
                    this.comboBoxEquipmentType.SelectedIndex = 0;
                    break;
            }
            this.comboBoxEquipmentType.SelectedIndexChanged += new EventHandler(this.comboBoxEquipmentType_SelectedIndexChanged);

            // 옵션보드
            this.comboBoxOptionBoard.SelectedIndexChanged -= new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);
            switch (int.Parse(system.OptionBoard))
            {
                case 1: // OPT1
                    this.comboBoxOptionBoard.SelectedIndex = 1;
                    break;
                default:
                    this.comboBoxOptionBoard.SelectedIndex = 0;
                    break;
            }
            this.comboBoxOptionBoard.SelectedIndexChanged += new EventHandler(this.comboBoxOptionBoard_SelectedIndexChanged);

            if (this.ParentForm.ParentForm.IsCommunicationLogOpen == true)
                this.buttonCommunicationLog.ButtonDown();
            else
                this.buttonCommunicationLog.ButtonUp();
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_DisplayStore.EquipEngineer;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.UpdateParameterDisplay(this.ParentForm.ParentForm.SystemConfig, this.ParentForm.ParentForm.CurrentSystemParameter9507);
        }
        #endregion

        #region Event Handler
        private void comboBoxEquipmentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = "";

            if (this.ParentForm.ParentForm.SystemConfig.EquipmentType == 8 || this.comboBoxEquipmentType.SelectedIndex == 2)
            {
                DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.Language, 30);
                if (myDlg.ShowDialog() == DialogResult.Yes)
                {
                    switch (this.comboBoxEquipmentType.SelectedIndex)
                    {
                        case 0: // 컨베어
                            this.ParentForm.ParentForm.SystemConfig.EquipmentType = 0;
                            this.ParentForm.CenterSystemSorterSetting.UpdateControlEnable(true);
                            this.ParentForm.ParentForm.ChildFormMainDisplay.UpdateControlVisible(false);
                            break;
                        case 1: // 업다운
                            this.ParentForm.ParentForm.SystemConfig.EquipmentType = 4;
                            this.ParentForm.CenterSystemSorterSetting.UpdateControlEnable(false);
                            this.ParentForm.ParentForm.ChildFormMainDisplay.UpdateControlVisible(true);
                            break;
                        case 2: // 알루스캔
                            // 알루스캔 사용 시 재부팅 필요
                            this.ParentForm.ParentForm.SystemConfig.SerialCOM3BaudRate = 3;
                            this.ParentForm.ParentForm.SystemConfig.SerialCOM3Mode = 0;

                            this.ParentForm.ParentForm.SystemConfig.EquipmentType = 8;

                            this.ParentForm.CenterSystemSorterSetting.UpdateControlEnable(true);
                            this.ParentForm.ParentForm.ChildFormMainDisplay.UpdateControlVisible(false);
                            break;
                        default:
                            this.ParentForm.ParentForm.SystemConfig.EquipmentType = 0;
                            this.ParentForm.CenterSystemSorterSetting.UpdateControlEnable(true);
                            this.ParentForm.ParentForm.ChildFormMainDisplay.UpdateControlVisible(false);
                            break;
                    }
                }

                value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.SystemConfig.EquipmentType.ToString());
                this.ParentForm.CenterSystemJudgmentSetting.ChangeScreen();
                this.ParentForm.ParentForm.SaveSystemConfigurationFile1(this.ParentForm.ParentForm.SystemConfig);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._1002_EquipmentType, value);

                DialogFormMessage msg = new DialogFormMessage(18, this.ParentForm.ParentForm.SystemConfig.Language);
                msg.ShowDialog();
            }
            else
            {
                switch (this.comboBoxEquipmentType.SelectedIndex)
                {
                    case 0: // 컨베어
                        this.ParentForm.ParentForm.SystemConfig.EquipmentType = 0;
                        this.ParentForm.CenterSystemSorterSetting.UpdateControlEnable(true);
                        this.ParentForm.ParentForm.ChildFormMainDisplay.UpdateControlVisible(false);
                        break;
                    case 1: // 업다운
                        this.ParentForm.ParentForm.SystemConfig.EquipmentType = 4;
                        this.ParentForm.CenterSystemSorterSetting.UpdateControlEnable(false);
                        this.ParentForm.ParentForm.ChildFormMainDisplay.UpdateControlVisible(true);
                        break;
                    case 2: // 알루스캔
                        // 알루스캔 사용 시 재부팅 필요
                        this.ParentForm.ParentForm.SystemConfig.SerialCOM3BaudRate = 4;
                        this.ParentForm.ParentForm.SystemConfig.SerialCOM3Mode = 0;

                        this.ParentForm.ParentForm.SystemConfig.EquipmentType = 8;

                        this.ParentForm.CenterSystemSorterSetting.UpdateControlEnable(true);
                        this.ParentForm.ParentForm.ChildFormMainDisplay.UpdateControlVisible(false);
                        break;
                    default:
                        this.ParentForm.ParentForm.SystemConfig.EquipmentType = 0;
                        this.ParentForm.CenterSystemSorterSetting.UpdateControlEnable(true);
                        this.ParentForm.ParentForm.ChildFormMainDisplay.UpdateControlVisible(false);
                        break;
                }

                value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.SystemConfig.EquipmentType.ToString());
                this.ParentForm.CenterSystemJudgmentSetting.ChangeScreen();
                this.ParentForm.ParentForm.SaveSystemConfigurationFile1(this.ParentForm.ParentForm.SystemConfig);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._1002_EquipmentType, value);
            }
        }
        private void comboBoxOptionBoard_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = "";

            value = Helper.StringZeroFillDigits4(this.comboBoxOptionBoard.SelectedIndex.ToString());
            this.ParentForm.ParentForm.CurrentSystemParameter9507.OptionBoard = value;
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._1402_OptionBoard, value);

            this.ParentForm.ParentForm.ChildFormMenu.CenterConfiOptionBoard.InitializeDesign();

            this.ParentForm.ParentForm.ChildFormMenu.CenterConfiHelp.OptionBoardVisible();
            this.ParentForm.ParentForm.ChildFormMenu.BottomConfiguration.OptionBoardVisible();

            this.ParentForm.ParentForm.ChildFormMainDisplay.MainWeightScreen.UpdateDisplay();
        }

        private void buttonCapture_Click(object sender, EventArgs e)
        {
            string filePath = "";
            Bitmap bitMap = new Bitmap(800, 480);

            bitMap = IntechGraphics.CopyFromScreen();
            filePath = string.Format("{0}{1}_EngineerSetting.jpg",
                this.ParentForm.ParentForm.PathDataBackupFolder, this.ParentForm.ParentForm.SystemConfig.SerialNumber);
            bitMap.Save(filePath, ImageFormat.Jpeg);
        }
        private void buttonCommunicationLog_Click(object sender, EventArgs e)
        {
            if (this.buttonCommunicationLog.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
            {
                this.ParentForm.ParentForm.smartFileCommunicationLog.Close();
                this.ParentForm.ParentForm.smartFileCommunicationLog.Open(this.ParentForm.ParentForm.BufferSmartUart);
                this.ParentForm.ParentForm.IsCommunicationLogOpen = true;
            }
            else
            {
                this.ParentForm.ParentForm.smartFileCommunicationLog.Close();
                this.ParentForm.ParentForm.IsCommunicationLogOpen = false;
            }
        }
        #endregion
    }
}
