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 INT51DB.Forms;
using INT51DB_ImageDll;

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

            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else
            {
            }
        }
        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);

            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.buttonOutput1.Text = "RED";
            this.buttonOutput2.Text = "GRN";
            this.buttonOutput3.Text = "None";
            this.buttonOutput4.Text = "Buzzer";
            this.buttonOutput5.Text = "OUT.A1";
            this.buttonOutput6.Text = "OUT.A2";
            this.buttonOutput7.Text = "OUT.A3";
            this.buttonOutput8.Text = "OUT.A4";
            this.buttonOutput9.Text = "OUT.B1";
            this.buttonOutput10.Text = "OUT.B2";
            this.buttonOutput11.Text = "OUT.B3";
            this.buttonOutput12.Text = "OUT.B4";
            this.buttonOutput13.Text = "OUT.C1";
            this.buttonOutput14.Text = "OUT.C2";
            this.buttonOutput15.Text = "OUT.C3";
            this.buttonOutput16.Text = "OUT.C4";

            this.labelInputName1.Text = "Input1";
            this.labelInputName2.Text = "Input2";
            this.labelInputName3.Text = "Input3";
            this.labelInputName4.Text = "Input4";
            this.labelInputName5.Text = "S1.Alarm";
            this.labelInputName6.Text = "S2.Alarm";
            this.labelInputName7.Text = "None";
            this.labelInputName8.Text = "SVON";
            this.labelInputName9.Text = "None";
            this.labelInputName10.Text = "None";
            this.labelInputName11.Text = "None";
            this.labelInputName12.Text = "None";
            this.labelInputName13.Text = "None";
            this.labelInputName14.Text = "None";
            this.labelInputName15.Text = "None";
            this.labelInputName16.Text = "None";
        }

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

            this.ParentForm.ParentForm.TransferData(CommunicationCommand.ModeIOTest, CommunicationID.MainBoard);

            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
    }
}
