using System;
using System.Linq;
using System.Collections.ObjectModel;
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 ITC81DB_0H.DialogForms;
using ITC81DB_0H.Forms;

namespace ITC81DB_0H
{
    public partial class ControlCenterInforSystem3 : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        #endregion

        #region Constructor
        public ControlCenterInforSystem3(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()
        {
            if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Korean)
            {
                this.groupBoxEquipment.Text = "기능설정";
                this.groupBoxSerial.Text = "시리얼통신";
                this.groupBoxEthernet.Text = "이더넷통신";

                this.labelTitleEquipmentID.Text = "장비 ID";
                this.labelTitleBuzzerOn.Text = "부저 ON";
                this.labelTitleChattering.Text = "채터링";
                this.labelTitleRelay.Text = "릴레이동작";
                this.labelTitleLanguage.Text = "언어설정";
                this.labelTitleOptionBoard.Text = "옵션보드";
                this.labelTitleExtension.Text = "확장자";
                this.labelTitleFormat.Text = "포맷";
                this.labelTitleLogin.Text = "로그인";
                this.labelTitleBarcode.Text = "바코드";

                this.labelTitleSampleNumber.Text = "샘플개수";
                this.labelTitleDelayNumber.Text = "지연개수";
                this.labelTitlePulseWidth.Text = "펄스폭";
                this.labelTitleUsingOPT1.Text = "사용여부";
                this.labelTitleFeedbackOver.Text = "피드백상한";
                this.labelTitleFeedbackUnder.Text = "피드백하한";

                this.labelTitlePort.Text = "출력포트";
                this.labelTitleDelayTime1.Text = "지연시간1";
                this.labelTitleDelayTime2.Text = "지연시간2";

                this.labelTitleModeSerial.Text = "모드";

                this.labelTitleOperatingMode.Text = "동작모드";
                this.labelTitleCommunicationMode.Text = "통신모드";
            }
            else
            {
                this.groupBoxEquipment.Text = "Equipment";
                this.groupBoxSerial.Text = "Serial";
                this.groupBoxEthernet.Text = "Ethernet";

                this.labelTitleEquipmentID.Text = "Equip ID";
                this.labelTitleBuzzerOn.Text = "Buzz ON";
                this.labelTitleChattering.Text = "Chattering";
                this.labelTitleRelay.Text = "Relay";
                this.labelTitleLanguage.Text = "Language";
                this.labelTitleOptionBoard.Text = "Op.Board";
                this.labelTitleExtension.Text = "Extension";
                this.labelTitleFormat.Text = "Format";
                this.labelTitleLogin.Text = "Login";
                this.labelTitleBarcode.Text = "Barcode";

                this.labelTitleSampleNumber.Text = "Sample Num";
                this.labelTitleDelayNumber.Text = "Delay Num";
                this.labelTitlePulseWidth.Text = "Pulse Width";
                this.labelTitleUsingOPT1.Text = "Using";
                this.labelTitleFeedbackOver.Text = "Feedback Over";
                this.labelTitleFeedbackUnder.Text = "Feedback Under";

                this.labelTitlePort.Text = "Port";
                this.labelTitleDelayTime1.Text = "Delay Time 1";
                this.labelTitleDelayTime2.Text = "Delay Time 2";

                this.labelTitleModeSerial.Text = "Mode";

                this.labelTitleOperatingMode.Text = "Op-Mode";
                this.labelTitleCommunicationMode.Text = "Com-Mode";
            }
        }
        private void DefaultSetting()
        {

        }

        private string ReturnBaudrateName(int num)
        {
            switch (num)
            {
                case 0:
                    return "9600";
                case 1:
                    return "19200";
                case 2:
                    return "38400";
                case 3:
                    return "57600";
                case 4:
                    return "115200";
                default:
                    return "9600";
            }
        }
        private string ReturnCommunicationModeName(int num)
        {
            if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                switch (num)
                {
                    case 0:
                        return "Keiner";
                    case 1:
                        return "STD1";
                    case 2:
                        return "Fernzugriff";
                    case 3:
                        return "Drucker";
                    case 4:
                        return "imaje9410-1";
                    case 5:
                        return "imaje9028-1";
                    case 6:
                        return "OPT0";
                    case 7:
                        return "imaje9410-2";
                    case 8:
                        return "imaje9028-2";
                    case 9:
                        return "Hitachi";
                    case 10:
                        return "MACSA";
                    case 11:
                        return "Markoprint";
                    case 12:
                        return "alphaJET";
                    case 13:
                        return "VJ1510";
                    case 14:
                        return "OPT1";
                    case 15:
                        return "OPT2";
                    case 16:
                        return "OPC";
                    case 17:
                        return "OPT3";
                    case 18:
                        return "HP-200";
                    default:
                        return "Keiner";
                }
            }
            else
            {
                switch (num)
                {
                    case 0:
                        return "None";
                    case 1:
                        return "STD1";
                    case 2:
                        return "Remote";
                    case 3:
                        return "Printer";
                    case 4:
                        return "imaje9410-1";
                    case 5:
                        return "imaje9028-1";
                    case 6:
                        return "OPT0";
                    case 7:
                        return "imaje9410-2";
                    case 8:
                        return "imaje9028-2";
                    case 9:
                        return "Hitachi";
                    case 10:
                        return "MACSA";
                    case 11:
                        return "Markoprint";
                    case 12:
                        return "alphaJET";
                    case 13:
                        return "VJ1510";
                    case 14:
                        return "OPT1";
                    case 15:
                        return "OPT2";
                    case 16:
                        return "OPC";
                    case 17:
                        return "OPT3";
                    case 18:
                        return "HP-200";
                    default:
                        return "None";
                }
            }
        }
        private void InitialLabel()
        {
            #region Initial Label
            this.labelOPT1SampleNumber.Text = "-";
            this.labelOPT1DelayNumber.Text = "-";
            this.labelOPT1PulseWidth.Text = "-";
            this.labelOPT1Using.Text = "-";
            this.labelOPT1FeedbackOver.Text = "-";
            this.labelOPT1FeedbackUnder.Text = "-";

            this.labelOPT2Port.Text = "-";
            this.labelOPT2DelayTime1.Text = "-";
            this.labelOPT2DelayTime2.Text = "-";

            this.labelBuzzerOn.Text = "-";
            this.labelChattering.Text = "-";
            this.labelRelay.Text = "-";
            this.labelOptionBoard.Text = "-";
            this.labelPIN6.Text = "-";
            this.labelPIN5.Text = "-";
            this.labelPIN4.Text = "-";
            this.labelPIN3.Text = "-";
            this.labelPhotoB.Text = "-";

            this.labelEquipmentID.Text = "-";
            this.labelLanguage.Text = "-";
            this.labelExtension.Text = "-";
            this.labelFormat.Text = "-";
            this.labelLogin.Text = "-";
            this.labelBarcode.Text = "-";

            this.labelCOM1Baudrate.Text = "-";
            this.labelCOM1Mode.Text = "-";
            this.labelCOM3Baudrate.Text = "-";
            this.labelCOM3Mode.Text = "-";
            this.labelCOM4Baudrate.Text = "-";
            this.labelCOM4Mode.Text = "-";
            this.labelOperatingMode.Text = "-";
            this.labelCommunicationMode.Text = "-";
            this.labelIPConfiguration.Text = "-";
            this.labelIPAddress.Text = "-";
            this.labelSubnetMask.Text = "-";
            this.labelGateWay.Text = "-";
            this.labelServerPort.Text = "-";
            this.labelClientServerIP.Text = "-";
            this.labelClientServerPort.Text = "-"; 
            #endregion

            #region ForeColor
            this.labelOPT1SampleNumber.ForeColor = Color.White;
            this.labelOPT1DelayNumber.ForeColor = Color.White;
            this.labelOPT1PulseWidth.ForeColor = Color.White;
            this.labelOPT1Using.ForeColor = Color.White;
            this.labelOPT1FeedbackOver.ForeColor = Color.White;
            this.labelOPT1FeedbackUnder.ForeColor = Color.White;

            this.labelOPT2Port.ForeColor = Color.White;
            this.labelOPT2DelayTime1.ForeColor = Color.White;
            this.labelOPT2DelayTime2.ForeColor = Color.White;

            this.labelBuzzerOn.ForeColor = Color.White;
            this.labelChattering.ForeColor = Color.White;
            this.labelRelay.ForeColor = Color.White;
            this.labelOptionBoard.ForeColor = Color.White;
            this.labelPIN6.ForeColor = Color.White;
            this.labelPIN5.ForeColor = Color.White;
            this.labelPIN4.ForeColor = Color.White;
            this.labelPIN3.ForeColor = Color.White;
            this.labelPhotoB.ForeColor = Color.White;

            this.labelEquipmentID.ForeColor = Color.White;
            this.labelLanguage.ForeColor = Color.White;
            this.labelExtension.ForeColor = Color.White;
            this.labelFormat.ForeColor = Color.White;
            this.labelLogin.ForeColor = Color.White;
            this.labelBarcode.ForeColor = Color.White;

            this.labelCOM1Baudrate.ForeColor = Color.White;
            this.labelCOM1Mode.ForeColor = Color.White;
            this.labelCOM3Baudrate.ForeColor = Color.White;
            this.labelCOM3Mode.ForeColor = Color.White;
            this.labelCOM4Baudrate.ForeColor = Color.White;
            this.labelCOM4Mode.ForeColor = Color.White;
            this.labelOperatingMode.ForeColor = Color.White;
            this.labelCommunicationMode.ForeColor = Color.White;
            this.labelIPConfiguration.ForeColor = Color.White;
            this.labelIPAddress.ForeColor = Color.White;
            this.labelSubnetMask.ForeColor = Color.White;
            this.labelGateWay.ForeColor = Color.White;
            this.labelServerPort.ForeColor = Color.White;
            this.labelClientServerIP.ForeColor = Color.White;
            this.labelClientServerPort.ForeColor = Color.White;
            #endregion
        }
        private void CheckCompareBoardLCDValue(SystemInformation3 item)
        {
            Collection<ProductItem> productItem = new Collection<ProductItem>(); 
            for (int i = 0; i < 5; i++)
                productItem.Add(new ProductItem());

            if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1SamplingCount != item.OPT1SampleNumber)
                this.labelOPT1SampleNumber.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1DelayCount != item.OPT1DelayNumber)
                this.labelOPT1DelayNumber.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1PulseWidth != item.OPT1PulseWidth)
                this.labelOPT1PulseWidth.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1IsWeightSetting != item.OPT1Using)
                this.labelOPT1Using.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1OverRange != item.OPT1OverRange)
                this.labelOPT1FeedbackOver.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1UnderRange != item.OPT1UnderRange)
                this.labelOPT1FeedbackUnder.ForeColor = Color.Red;

            if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2Port != item.OPT2Port)
                this.labelOPT2Port.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2DelayTime1 != item.OPT2Delay1)
                this.labelOPT2DelayTime1.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2DelayTime2 != item.OPT2Delay2)
                this.labelOPT2DelayTime2.ForeColor = Color.Red;

            if (this.ParentForm.ParentForm.CurrentSystemParameter1.BuzzerOnTime != item.BuzzerONTime)
                this.labelBuzzerOn.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter1.Chattering != item.Chattering)
                this.labelChattering.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter1.RelayOnTime != item.Relay)
                this.labelRelay.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter1.OptionBoard != item.OptionBoard)
                this.labelOptionBoard.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter1.PI6 != int.Parse(item.PI6))
                this.labelPIN6.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter1.PI5 != int.Parse(item.PI5))
                this.labelPIN5.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter1.PI4 != int.Parse(item.PI4))
                this.labelPIN4.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter1.PI3 != int.Parse(item.PI3))
                this.labelPIN3.ForeColor = Color.Red;
            if (this.ParentForm.ParentForm.CurrentSystemParameter1.PI2 != int.Parse(item.PhotoB))
                this.labelPhotoB.ForeColor = Color.Red;
        }
        public void UpdateParameter2Display(SystemInformation3 item)
        {
            int temp = 0;

            #region OPT1
            this.labelOPT1SampleNumber.Text = item.OPT1SampleNumber;
            this.labelOPT1DelayNumber.Text = item.OPT1DelayNumber;
            temp = int.Parse(item.OPT1PulseWidth);
            switch (temp)
            {
                case 0:
                    this.labelOPT1PulseWidth.Text = "2";
                    break;
                case 1:
                    this.labelOPT1PulseWidth.Text = "4";
                    break;
                case 2:
                    this.labelOPT1PulseWidth.Text = "6";
                    break;
                case 3:
                    this.labelOPT1PulseWidth.Text = "8";
                    break;
                case 4:
                    this.labelOPT1PulseWidth.Text = "10";
                    break;
                default:
                    this.labelOPT1PulseWidth.Text = "6";
                    break;
            }

            temp = int.Parse(item.OPT1Using);
            if(temp == 1)
                this.labelOPT1Using.Text = "ON";
            else
                this.labelOPT1Using.Text = "OFF";
            this.labelOPT1FeedbackOver.Text = Helper.StringToDecimalPlaces(item.OPT1OverRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            this.labelOPT1FeedbackUnder.Text = Helper.StringToDecimalPlaces(item.OPT1UnderRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            #endregion

            #region OPT2
            temp = int.Parse(item.OPT2Port);
            if(temp == 1)
                this.labelOPT2Port.Text = "5, 6";
            else if(temp == 2)
                this.labelOPT2Port.Text = "7, 8";
            else
                this.labelOPT2Port.Text = "None";
            this.labelOPT2DelayTime1.Text = item.OPT2Delay1;
            this.labelOPT2DelayTime2.Text = item.OPT2Delay2;
            #endregion

            #region Equipment setting
            this.labelBuzzerOn.Text = item.BuzzerONTime;
            this.labelChattering.Text = item.Chattering;
            this.labelRelay.Text = item.Relay;

            temp = int.Parse(item.OptionBoard);
            if(temp == 1)
                this.labelOptionBoard.Text = "OPT1";
            else if(temp == 2)
                this.labelOptionBoard.Text = "OPT2";
            else
                this.labelOptionBoard.Text = "None";

            temp = int.Parse(item.PI6);
            if (temp == 1)
                this.labelPIN6.Text = "OPT2-En2";
            else
                this.labelPIN6.Text = "None";

            temp = int.Parse(item.PI5);
            if (temp == 1)
                this.labelPIN5.Text = "Stack up";
            else if (temp == 2)
                this.labelPIN5.Text = "Ex.Run";
            else
                this.labelPIN5.Text = "None";

            temp = int.Parse(item.PI4);
            if (temp == 1)
                this.labelPIN4.Text = "Air";
            else if (temp == 2)
                this.labelPIN4.Text = "START";
            else
                this.labelPIN4.Text = "None";

            temp = int.Parse(item.PI3);
            if (temp == 1)
                this.labelPIN3.Text = "STOP";
            else
                this.labelPIN3.Text = "None";

            temp = int.Parse(item.PhotoB);
            if(temp == 1)
                this.labelPhotoB.Text = "Door";
            else
                this.labelPhotoB.Text = "None";
            #endregion

            this.CheckCompareBoardLCDValue(item);
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.InforSystem3;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.Menu);

            this.InitialLabel();
        }
        #endregion

        #region Event Handler
        private void buttonPrevious_Click(object sender, EventArgs e)
        {
            this.ParentForm.CenterInforSystem2.DisplayRefresh(this.ParentForm.ParentForm.CurrentSystemStatus);
            this.ParentForm.CenterInforSystem2.BringToFront();
        }

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

            bitMap = IntechGraphics.CopyFromScreen();
            filePath = string.Format("{0}{1}_SystemInfo2.jpg",
                this.ParentForm.ParentForm.PathDataBackupFolder, this.ParentForm.ParentForm.SystemConfig1.SerialNumber);
            bitMap.Save(filePath, ImageFormat.Jpeg);
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.SystemInformationRead3, "");

            #region Equipment setting
            this.labelEquipmentID.Text = this.ParentForm.ParentForm.SystemConfig1.EquipmentID.ToString();
            this.labelLanguage.Text = this.ParentForm.ParentForm.SystemConfig1.Language.ToString();
            if(this.ParentForm.ParentForm.SystemConfig2.FileNameExtension == 1)
                this.labelExtension.Text = "TXT";
            else
                this.labelExtension.Text = "CSV";
            if (this.ParentForm.ParentForm.SystemConfig1.DatabackupFormat == 1)
                this.labelFormat.Text = "OPT1"; // 종근당 리젝데이터 있는 포맷
            else
                this.labelFormat.Text = "STD1";
            if (this.ParentForm.ParentForm.SystemConfig1.IsLogin == true)
                this.labelLogin.Text = "ON";
            else
                this.labelLogin.Text = "OFF";
            if (this.ParentForm.ParentForm.SystemConfig1.IsBarcodeEnable == true)
                this.labelBarcode.Text = "ON";
            else
                this.labelBarcode.Text = "OFF";

            #endregion

            #region Serial
            this.labelCOM1Baudrate.Text = this.ReturnBaudrateName(this.ParentForm.ParentForm.SystemConfig1.SerialCOM1BaudRate);
            this.labelCOM1Mode.Text = this.ReturnCommunicationModeName(this.ParentForm.ParentForm.SystemConfig1.SerialCOM1Mode);
            this.labelCOM3Baudrate.Text = this.ReturnBaudrateName(this.ParentForm.ParentForm.SystemConfig1.SerialCOM3BaudRate);
            this.labelCOM3Mode.Text = this.ReturnCommunicationModeName(this.ParentForm.ParentForm.SystemConfig1.SerialCOM3Mode);
            this.labelCOM4Baudrate.Text = this.ReturnBaudrateName(this.ParentForm.ParentForm.SystemConfig1.SerialCOM4BaudRate);
            this.labelCOM4Mode.Text = this.ReturnCommunicationModeName(this.ParentForm.ParentForm.SystemConfig1.SerialCOM4Mode);
            #endregion

            #region Ethernet
            if(this.ParentForm.ParentForm.SystemConfig2.EthernetOperationMode == 1)
                this.labelOperatingMode.Text = "Server";
            else if (this.ParentForm.ParentForm.SystemConfig2.EthernetOperationMode == 2)
                this.labelOperatingMode.Text = "Client";
            else
                this.labelOperatingMode.Text = "None";
            this.labelCommunicationMode.Text = this.ReturnCommunicationModeName(this.ParentForm.ParentForm.SystemConfig2.EthernetCommMode);
            if (this.ParentForm.ParentForm.smartConfigs.IPSettings.DHCPEnable == 0)
                this.labelIPConfiguration.Text = "Static";
            else
                this.labelIPConfiguration.Text = "DHCP";
            this.labelIPAddress.Text = this.ParentForm.ParentForm.smartConfigs.IPSettings.DeviceIP;
            this.labelSubnetMask.Text = this.ParentForm.ParentForm.smartConfigs.IPSettings.SubNetMask;
            this.labelGateWay.Text = this.ParentForm.ParentForm.smartConfigs.IPSettings.GateWay;
            this.labelServerPort.Text = this.ParentForm.ParentForm.SystemConfig2.EthernetLocalPort.ToString();
            this.labelClientServerIP.Text = this.ParentForm.ParentForm.SystemConfig2.EthernetAddress;
            this.labelClientServerPort.Text = this.ParentForm.ParentForm.SystemConfig2.EthernetPort.ToString();
            #endregion
        }
        #endregion
    }
}