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

using SmartX;
using INT69DB_2A_ImageDll;

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

        private Color ColorIOStatusOn;
        private Color ColorIOStatusOff;

        private Collection<Label> CollectionInputLabel;
        private Collection<SmartLabel> CollectionInputLabelName;
        private Collection<SmartButton> CollectionOutputButton;
        #endregion

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

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();
            this.InitializeControl();
        } 
        #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)
            {
                this.labelTitle.Text = "I/O Test";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "I/O测试";
                this.buttonInputRead.Text = "读";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitle.Text = "I/Oテスト";
                this.buttonInputRead.Text = "読む";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {

            }
            else
            {

            }
        }
        private void InitializeControl()
        {
            foreach (Label label in this.CollectionInputLabel)
                this.SetlabelOnOff(label, false);

            foreach (SmartButton button in this.CollectionOutputButton)
                button.ButtonUp();

            this.buttonInputRead.ButtonUp();
            this.timer.Enabled = false;

            this.groupBoxPLC.Location = new Point(16, 108);
            this.groupBoxPLC.Visible = false;
        }
        private void DefaultSetting()
        {
            this.ColorIOStatusOn = Color.Lime;
            this.ColorIOStatusOff = Color.Gray;

            this.CollectionInputLabel = new Collection<Label>();
            this.CollectionInputLabel.Clear();
            this.CollectionInputLabel.Add(this.labelInput1);
            this.CollectionInputLabel.Add(this.labelInput2);
            this.CollectionInputLabel.Add(this.labelInput3);
            this.CollectionInputLabel.Add(this.labelInput4);
            this.CollectionInputLabel.Add(this.labelInput5);
            this.CollectionInputLabel.Add(this.labelInput6);
            this.CollectionInputLabel.Add(this.labelInput7);
            this.CollectionInputLabel.Add(this.labelInput8);
            this.CollectionInputLabel.Add(this.labelInput9);
            this.CollectionInputLabel.Add(this.labelInput10);
            this.CollectionInputLabel.Add(this.labelInput11);
            this.CollectionInputLabel.Add(this.labelInput12);
            this.CollectionInputLabel.Add(this.labelInput13);
            this.CollectionInputLabel.Add(this.labelInput14);
            this.CollectionInputLabel.Add(this.labelInput15);
            this.CollectionInputLabel.Add(this.labelInput16);

            this.CollectionInputLabelName = new Collection<SmartLabel>();
            this.CollectionInputLabelName.Clear();
            this.CollectionInputLabelName.Add(this.labelInputName1);
            this.CollectionInputLabelName.Add(this.labelInputName2);
            this.CollectionInputLabelName.Add(this.labelInputName3);
            this.CollectionInputLabelName.Add(this.labelInputName4);
            this.CollectionInputLabelName.Add(this.labelInputName5);
            this.CollectionInputLabelName.Add(this.labelInputName6);
            this.CollectionInputLabelName.Add(this.labelInputName7);
            this.CollectionInputLabelName.Add(this.labelInputName8);
            this.CollectionInputLabelName.Add(this.labelInputName9);
            this.CollectionInputLabelName.Add(this.labelInputName10);
            this.CollectionInputLabelName.Add(this.labelInputName11);
            this.CollectionInputLabelName.Add(this.labelInputName12);
            this.CollectionInputLabelName.Add(this.labelInputName13);
            this.CollectionInputLabelName.Add(this.labelInputName14);
            this.CollectionInputLabelName.Add(this.labelInputName15);
            this.CollectionInputLabelName.Add(this.labelInputName16);

            this.CollectionOutputButton = new Collection<SmartButton>();
            this.CollectionOutputButton.Clear();
            this.CollectionOutputButton.Add(this.buttonOutput1);
            this.CollectionOutputButton.Add(this.buttonOutput2);
            this.CollectionOutputButton.Add(this.buttonOutput3);
            this.CollectionOutputButton.Add(this.buttonOutput4);
            this.CollectionOutputButton.Add(this.buttonOutput5);
            this.CollectionOutputButton.Add(this.buttonOutput6);
            this.CollectionOutputButton.Add(this.buttonOutput7);
            this.CollectionOutputButton.Add(this.buttonOutput8);
            this.CollectionOutputButton.Add(this.buttonOutput9);
            this.CollectionOutputButton.Add(this.buttonOutput10);
            this.CollectionOutputButton.Add(this.buttonOutput11);
            this.CollectionOutputButton.Add(this.buttonOutput12);
            this.CollectionOutputButton.Add(this.buttonOutput13);
            this.CollectionOutputButton.Add(this.buttonOutput14);
            this.CollectionOutputButton.Add(this.buttonOutput15);
            this.CollectionOutputButton.Add(this.buttonOutput16);
            this.CollectionOutputButton.Add(this.buttonOutput17);
            this.CollectionOutputButton.Add(this.buttonOutput18);
            this.CollectionOutputButton.Add(this.buttonOutput19);
            this.CollectionOutputButton.Add(this.buttonOutput20);
            this.CollectionOutputButton.Add(this.buttonOutput21);
            this.CollectionOutputButton.Add(this.buttonOutput22);
            this.CollectionOutputButton.Add(this.buttonOutput23);
            this.CollectionOutputButton.Add(this.buttonOutput24);
            this.CollectionOutputButton.Add(this.buttonOutput25);
            this.CollectionOutputButton.Add(this.buttonOutput26);
            this.CollectionOutputButton.Add(this.buttonOutput27);
            this.CollectionOutputButton.Add(this.buttonOutput28);
            this.CollectionOutputButton.Add(this.buttonOutput29);
            this.CollectionOutputButton.Add(this.buttonOutput30);
            this.CollectionOutputButton.Add(this.buttonOutput31);
            this.CollectionOutputButton.Add(this.buttonOutput32);

            for (int i = 0; i < this.CollectionInputLabelName.Count; i++)
                this.CollectionInputLabelName[i].Text = string.Format("IN{0}", i + 1);

            for (int i = 0; i < this.CollectionOutputButton.Count; i++)
                this.CollectionOutputButton[i].Text = string.Format("OUT{0}", i + 1);

            //if (this.ParentForm.SystemConfig.EquipmentMode == 0)
            //{
            //    if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            //    {
            //        this.buttonOutput1.Text = "红灯";
            //        this.buttonOutput2.Text = "绿灯";
            //        this.buttonOutput3.Text = "蜂鸣器";
            //        this.buttonOutput4.Text = "运转(DRY)";
            //        this.buttonOutput5.Text = "输送机1";
            //        this.buttonOutput6.Text = "输送机2";
            //        this.buttonOutput7.Text = "输送机3";
            //        this.buttonOutput8.Text = "输送机4";
            //        this.buttonOutput9.Text = "输送机5";
            //        this.buttonOutput10.Text = "输送机6";
            //        this.buttonOutput11.Text = "选别部1";
            //        this.buttonOutput12.Text = "选别部2";
            //        this.buttonOutput13.Text = "选别部3";
            //        this.buttonOutput14.Text = "选别部4";
            //        this.buttonOutput15.Text = "选别部5";
            //        this.buttonOutput16.Text = "选别部6";

            //        this.labelInputName1.Text = "开启开关";
            //        this.labelInputName2.Text = "停止开关";
            //        this.labelInputName3.Text = "";
            //        this.labelInputName4.Text = "";
            //        this.labelInputName5.Text = "";
            //        this.labelInputName6.Text = "";
            //        this.labelInputName7.Text = "";
            //        this.labelInputName8.Text = "";
            //        this.labelInputName9.Text = "输入传感器1";
            //        this.labelInputName10.Text = "输入传感器2";
            //        this.labelInputName11.Text = "输入传感器3";
            //        this.labelInputName12.Text = "输入传感器4";
            //        this.labelInputName13.Text = "输入传感器5";
            //        this.labelInputName14.Text = "输入传感器6";
            //        this.labelInputName15.Text = "";
            //        this.labelInputName16.Text = "";
            //    }
            //    else
            //    {
            //        this.buttonOutput1.Text = "Red Lamp";
            //        this.buttonOutput2.Text = "GreenLamp";
            //        this.buttonOutput3.Text = "Buzzer";
            //        this.buttonOutput4.Text = "Run(DRY)";
            //        this.buttonOutput5.Text = "Conveyor1";
            //        this.buttonOutput6.Text = "Conveyor2";
            //        this.buttonOutput7.Text = "Conveyor3";
            //        this.buttonOutput8.Text = "Conveyor4";
            //        this.buttonOutput9.Text = "Conveyor5";
            //        this.buttonOutput10.Text = "Conveyor6";
            //        this.buttonOutput11.Text = "Reject1";
            //        this.buttonOutput12.Text = "Reject2";
            //        this.buttonOutput13.Text = "Reject3";
            //        this.buttonOutput14.Text = "Reject4";
            //        this.buttonOutput15.Text = "Reject5";
            //        this.buttonOutput16.Text = "Reject6";

            //        this.labelInputName1.Text = "Run S/W";
            //        this.labelInputName2.Text = "Stop S/W";
            //        this.labelInputName3.Text = "";
            //        this.labelInputName4.Text = "";
            //        this.labelInputName5.Text = "";
            //        this.labelInputName6.Text = "";
            //        this.labelInputName7.Text = "";
            //        this.labelInputName8.Text = "";
            //        this.labelInputName9.Text = "InSensor1";
            //        this.labelInputName10.Text = "InSensor2";
            //        this.labelInputName11.Text = "InSensor3";
            //        this.labelInputName12.Text = "InSensor4";
            //        this.labelInputName13.Text = "InSensor5";
            //        this.labelInputName14.Text = "InSensor6";
            //        this.labelInputName15.Text = "";
            //        this.labelInputName16.Text = "";
            //    }
               
            //}
        }

        private void SetlabelOnOff(Label control, bool isOn)
        {
            if (isOn == true)
            {
                if (control.BackColor != this.ColorIOStatusOn)
                    control.BackColor = this.ColorIOStatusOn;
            }
            else
            {
                if (control.BackColor != this.ColorIOStatusOff)
                    control.BackColor = this.ColorIOStatusOff;
            }
        }

        public void UpdateInputRead(Collection<string> values)
        {
            if (values.Count == 0 || values == null)
                return;

            for (int i = 0; i < values.Count; i++)
                this.SetlabelOnOff(this.CollectionInputLabel[i], values[i] == "1" ? true : false);
        }

        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormIOTest;
            this.ParentForm.TransferData(CommunicationCommand.ModeIOTest, CommunicationID.MainBoard);
        }
        #endregion

        #region Event Handler
        private void buttonPlcOut_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            SmartButton button = sender as SmartButton;

            if (button == null)
                return;


            sb.Append((char)0x02);

            if (button == this.buttonPlcOut1)
                sb.Append((char)0x01);
            else if (button == this.buttonPlcOut2)
                sb.Append((char)0x02);
            else if (button == this.buttonPlcOut3)
                sb.Append((char)0x03);
            else if (button == this.buttonPlcOut4)
                sb.Append((char)0x04);
            else if (button == this.buttonPlcOut5)
                sb.Append((char)0x05);
            else if (button == this.buttonPlcOut6)
                sb.Append((char)0x06);
            else if (button == this.buttonPlcOut7)
                sb.Append((char)0x07);
            else if (button == this.buttonPlcOut8)
                sb.Append((char)0x08);
            else if (button == this.buttonPlcOut9)
                sb.Append((char)0x09);
            else if (button == this.buttonPlcOut10)
                sb.Append((char)0x0A);
            else if (button == this.buttonPlcOut11)
                sb.Append((char)0x0B);
            else if (button == this.buttonPlcOut12)
                sb.Append((char)0x0C);
            else if (button == this.buttonPlcOut13)
                sb.Append((char)0x0D);
            else if (button == this.buttonPlcOut14)
                sb.Append((char)0x0E);
            else if (button == this.buttonPlcOut15)
                sb.Append((char)0x0F);
            else if (button == this.buttonPlcOut16)
                sb.Append((char)0x10);
            else if (button == this.buttonPlcOut17)
                sb.Append((char)0x11);
            else if (button == this.buttonPlcOut18)
                sb.Append((char)0x12);
            else if (button == this.buttonPlcOut19)
                sb.Append((char)0x13);
            else if (button == this.buttonPlcOut20)
                sb.Append((char)0x14);

            sb.Append((char)0x03);

            if (this.ParentForm.ChildFormMotorSetting.serialPort1.IsOpen == true)
                this.ParentForm.ChildFormMotorSetting.serialPort1.Write(sb.ToString());
        }

        private void buttonPlcClose_Click(object sender, EventArgs e)
        {
            this.groupBoxPLC.Visible = false;
        }

        private void buttonPLC1_Click(object sender, EventArgs e)
        {
            if (this.groupBoxPLC.Visible == false)
                this.groupBoxPLC.Visible = true;
            else
                this.groupBoxPLC.Visible = false;
        }

        private void buttonOutput_Click(object sender, EventArgs e)
        {
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
            {
                this.ParentForm.TransferDataStream(CommunicationCommand.IOTest, CommunicationID.MainBoard, Helper.StringZeroFillDigits4(button.Tag.ToString()),
                    Helper.StringZeroFillDigits4("1"));
            }
            else
            {
                this.ParentForm.TransferDataStream(CommunicationCommand.IOTest, CommunicationID.MainBoard, Helper.StringZeroFillDigits4(button.Tag.ToString()),
                    Helper.StringZeroFillDigits4("0"));
            }
        }

        private void buttonInputRead_Click(object sender, EventArgs e)
        {
            if (this.buttonInputRead.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                this.timer.Enabled = true;
            else
                this.timer.Enabled = false;
        }

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

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.InitializeControl();

            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }
        #endregion
    }
}