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 SmartX;
using ITC81DB.Forms;
using ITC81DB.DialogForms;
using ITC81DB_ImageDll;

namespace ITC81DB.Controls
{
    public partial class ControlCenterSystemIOTest : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;

        private Color ColorIOStatusOn;
        private Color ColorIOStatusOff;

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

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

            this.ParentForm = parent;

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

            if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.English)
            {
                this.groupBoxOutput.Text = "Output";
                this.groupBoxInput.Text = "Input";

                this.buttonOutput1.Text = "Buzzer";
                this.buttonOutput2.Text = "GRN Lamp";
                this.buttonOutput3.Text = "RED Lamp";
                this.buttonOutput4.Text = "YEL Lamp";
                this.buttonOutput5.Text = "OPT";
                this.buttonOutput6.Text = "SORT A";
                this.buttonOutput7.Text = "SORT B";
                this.buttonOutput8.Text = "Motor";
                this.buttonOutput9.Text = "DRY 1";
                this.buttonOutput10.Text = "DRY 2";
                this.buttonOutput11.Text = "DRY 3";
                this.buttonOutput12.Text = "DRY 4";
                this.buttonOutput13.Text = "DRY 5";
                this.buttonOutput14.Text = "DRY 6";
                this.buttonOutput15.Text = "DRY 7";
                this.buttonOutput16.Text = "DRY 8";

                this.labelInputName1.Text = "PI8";
                this.labelInputName2.Text = "PI7";
                this.labelInputName3.Text = "PI6";
                this.labelInputName4.Text = "PI5";
                this.labelInputName5.Text = "PI4";
                this.labelInputName6.Text = "PI3";
                this.labelInputName7.Text = "PhotoB";
                this.labelInputName8.Text = "PhotoA";

            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.groupBoxOutput.Text = "输出";
                this.groupBoxInput.Text = "输入";

                this.buttonOutput1.Text = "蜂鸣器";
                this.buttonOutput2.Text = "绿灯";
                this.buttonOutput3.Text = "红灯";
                this.buttonOutput4.Text = "黄灯";
                this.buttonOutput5.Text = "OPT";
                this.buttonOutput6.Text = "分类机A";
                this.buttonOutput7.Text = "分类机B";
                this.buttonOutput8.Text = "马达";
                this.buttonOutput9.Text = "无电压1";
                this.buttonOutput10.Text = "无电压2";
                this.buttonOutput11.Text = "无电压3";
                this.buttonOutput12.Text = "无电压4";
                this.buttonOutput13.Text = "无电压5";
                this.buttonOutput14.Text = "无电压6";
                this.buttonOutput15.Text = "无电压7";
                this.buttonOutput16.Text = "无电压8";

                this.labelInputName1.Text = "PI8";
                this.labelInputName2.Text = "PI7";
                this.labelInputName3.Text = "PI6";
                this.labelInputName4.Text = "PI5";
                this.labelInputName5.Text = "PI4";
                this.labelInputName6.Text = "PI3";
                this.labelInputName7.Text = "PhotoB";
                this.labelInputName8.Text = "PhotoA";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.groupBoxOutput.Text = "Выход";
                this.groupBoxInput.Text = "Вход";

                this.buttonOutput1.Text = "Сигнал";
                this.buttonOutput2.Text = "Зеленая";
                this.buttonOutput3.Text = "Красная";
                this.buttonOutput4.Text = "Желтая";
                this.buttonOutput5.Text = "[OPT]";
                this.buttonOutput6.Text = "[SORT A]";
                this.buttonOutput7.Text = "[SORT B]";
                this.buttonOutput8.Text = "[Motor]";
                this.buttonOutput9.Text = "[DRY 1]";
                this.buttonOutput10.Text = "[DRT 2]";
                this.buttonOutput11.Text = "[DRY 3]";
                this.buttonOutput12.Text = "[DRY 4]";
                this.buttonOutput13.Text = "[DRY 5]";
                this.buttonOutput14.Text = "[DRY 6]";
                this.buttonOutput15.Text = "[DRY 7]";
                this.buttonOutput16.Text = "[DRY 8]";

                this.labelInputName1.Text = "[PI8]";
                this.labelInputName2.Text = "[PI7]";
                this.labelInputName3.Text = "[PI6]";
                this.labelInputName4.Text = "[PI5]";
                this.labelInputName5.Text = "[PI4]";
                this.labelInputName6.Text = "[PI3]";
                this.labelInputName7.Text = "[PhotoB]";
                this.labelInputName8.Text = "[PhotoA]";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.groupBoxOutput.Text = "Ausgabe";
                this.groupBoxInput.Text = "Eingang";

                this.buttonOutput1.Text = "Summer";
                this.buttonOutput2.Text = "Grüne-Amp";
                this.buttonOutput3.Text = "Rote-Amp";
                this.buttonOutput4.Text = "Gelb-Amp";
                this.buttonOutput5.Text = "Möglichkeit";
                this.buttonOutput6.Text = "SORT-A";
                this.buttonOutput7.Text = "SORT-B";
                this.buttonOutput8.Text = "Motor";
                this.buttonOutput9.Text = "Lauf 1";
                this.buttonOutput10.Text = "Lauf 2";
                this.buttonOutput11.Text = "Lauf 3";
                this.buttonOutput12.Text = "Lauf 4";
                this.buttonOutput13.Text = "Lauf 5";
                this.buttonOutput14.Text = "Lauf 6";
                this.buttonOutput15.Text = "Lauf 7";
                this.buttonOutput16.Text = "Lauf 8";

                this.labelInputName1.Text = "PI8";
                this.labelInputName2.Text = "PI7";
                this.labelInputName3.Text = "PI6";
                this.labelInputName4.Text = "PI5";
                this.labelInputName5.Text = "PI4";
                this.labelInputName6.Text = "PI3";
                this.labelInputName7.Text = "PhotoB";
                this.labelInputName8.Text = "PhotoA";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.groupBoxOutput.Text = "Producción";
                this.groupBoxInput.Text = "Aporte";

                this.buttonOutput1.Text = "Zumbador";
                this.buttonOutput2.Text = "Lámpara verde";
                this.buttonOutput3.Text = "Lámpara roja";
                this.buttonOutput4.Text = "Lámpara amarilla";
                this.buttonOutput5.Text = "Opción";
                this.buttonOutput6.Text = "Clasificador A";
                this.buttonOutput7.Text = "Clasificador B";
                this.buttonOutput8.Text = "Motor";
                this.buttonOutput9.Text = "Seco 1";
                this.buttonOutput10.Text = "Seco 2";
                this.buttonOutput11.Text = "Seco 3";
                this.buttonOutput12.Text = "Seco 4";
                this.buttonOutput13.Text = "Seco 5";
                this.buttonOutput14.Text = "Seco 6";
                this.buttonOutput15.Text = "Seco 7";
                this.buttonOutput16.Text = "Seco 8";

                this.labelInputName1.Text = "PI8";
                this.labelInputName2.Text = "PI7";
                this.labelInputName3.Text = "PI6";
                this.labelInputName4.Text = "PI5";
                this.labelInputName5.Text = "PI4";
                this.labelInputName6.Text = "PI3";
                this.labelInputName7.Text = "PhotoB";
                this.labelInputName8.Text = "PhotoA";
            }
            else
            {
                this.groupBoxOutput.Text = "Output";
                this.groupBoxInput.Text = "Input";

                this.buttonOutput1.Text = "Buzzer";
                this.buttonOutput2.Text = "GRN Lamp";
                this.buttonOutput3.Text = "RED Lamp";
                this.buttonOutput4.Text = "YEL Lamp";
                this.buttonOutput5.Text = "OPT";
                this.buttonOutput6.Text = "SORT A";
                this.buttonOutput7.Text = "SORT B";
                this.buttonOutput8.Text = "Motor";
                this.buttonOutput9.Text = "DRY 1";
                this.buttonOutput10.Text = "DRT 2";
                this.buttonOutput11.Text = "DRY 3";
                this.buttonOutput12.Text = "DRY 4";
                this.buttonOutput13.Text = "DRY 5";
                this.buttonOutput14.Text = "DRY 6";
                this.buttonOutput15.Text = "DRY 7";
                this.buttonOutput16.Text = "DRY 8";

                this.labelInputName1.Text = "PI8";
                this.labelInputName2.Text = "PI7";
                this.labelInputName3.Text = "PI6";
                this.labelInputName4.Text = "PI5";
                this.labelInputName5.Text = "PI4";
                this.labelInputName6.Text = "PI3";
                this.labelInputName7.Text = "PhotoB";
                this.labelInputName8.Text = "PhotoA";
            }
        }
        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.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.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);

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

            this.DrawInitialize();
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
            if (status == DataStore.EquipmentStatus.Start)
            {
                this.groupBoxOutput.Enabled = false;

                foreach (System.Windows.Forms.Label label in this.CollectionInputLabel)
                    label.Visible = false;
            }
            else
            {
                this.groupBoxOutput.Enabled = true;

                foreach (System.Windows.Forms.Label label in this.CollectionInputLabel)
                    label.Visible = true;
            }
        }
        private void DrawInitialize()
        {
            Class1 images = new Class1();
            int channel = 8;

            Bitmap backImage = new Bitmap(images.GetImage(Class1.ButtonImages.BackgroundIOTestGraph));
            this.smartDraw.BackDraw.ImageDraw(backImage, 0, 0);

            SmartX.SmartDraw.CHARTREFRESH charRefreshType = SmartX.SmartDraw.CHARTREFRESH.LEFTSCROLL;
            SmartX.SmartDraw.CHARTPENSTYLE[] charPenStyle;
            charPenStyle = new SmartX.SmartDraw.CHARTPENSTYLE[channel];
            for (int i = 0; i < channel; i++)
            {
                charPenStyle[i].m_iPenWidth = 1;
                charPenStyle[i].m_chColor = Color.Yellow;
            }

            this.smartDraw.SetChartCfg(10, 297, 500, 298, 1, charRefreshType, 8);
            this.smartDraw.ChartChannelPenStyle = charPenStyle;
            this.smartDraw.ChartDrawStep = 1;
        }
        public void UpdateGraphDataDisplay(Collection<string> values)
        {
            string sValue = "";
            int[] array = new int[8];
            int height = 37;
            int temp = 0;

            this.smartDraw.PutDataAllClear();

            try
            {
                for (int i = 0; i < values.Count; i++)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        if (int.Parse(values[i].Substring(j, 1)) == 1)
                            temp = 0;
                        else
                            temp = 1;

                        array[j] = temp * 27 + height * j;
                    }
                    //array[j] = reference - int.Parse(values[i].Substring(j, 1)) * 27 - height * j;

                    this.smartDraw.PutData(array[7], array[6], array[5], array[4], array[3], array[2], array[1], array[0]);
                }
            }
            catch
            {
                DialogFormMessage msg = new DialogFormMessage(6, this.ParentForm.ParentForm.SystemConfig1.Language);
                msg.ShowDialog();
                return;
            }
        }

        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(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.SystemIOTest;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.IOTest);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.smartDraw.PutDataAllClear();

            foreach (SmartButton button in this.CollectionOutputButton)
                button.ButtonUp();
        }
        #endregion
      
        #region Event Handler
        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 buttonOutput_Click(object sender, EventArgs e)
        {
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

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

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