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 INT99DC_ImageDll;
using INT99DC_7C.Controls;

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

        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;
        }
        #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";

                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackUp));
                this.buttonDisplay.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engScreenDisable));
                this.buttonDisplay.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engScreenDown));
                this.buttonDisplay.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engScreenUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "I/O测试";

                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackUp));
                this.buttonDisplay.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnScreenDisable));
                this.buttonDisplay.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnScreenDown));
                this.buttonDisplay.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnScreenUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                this.labelTitle.Text = "Test I/O";

                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackUp));
                this.buttonDisplay.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeScreenDisable));
                this.buttonDisplay.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeScreenDown));
                this.buttonDisplay.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeScreenUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.labelTitle.Text = "I/O Test";

                this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDisable));
                this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackUp));
                this.buttonDisplay.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerScreenDisable));
                this.buttonDisplay.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerScreenDown));
                this.buttonDisplay.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerScreenUp));
            }
            else
            {

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

            foreach (SmartButton button in this.CollectionOutputButton)
                button.ButtonUp();
        }
        private void DefaultSetting()
        {
            this.ColorIOStatusOn = Color.Lime;
            this.ColorIOStatusOff = Color.Gray;

            this.ChildControlIOTest = new ControlIOTest(this);
            this.Controls.Add(this.ChildControlIOTest);
            this.ChildControlIOTest.Location = new Point(0, 75);
            this.ChildControlIOTest.BringToFront();
            this.ChildControlIOTest.Visible = false;

            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);

            this.buttonOutput9.Text = "SM_OUT9";
            this.buttonOutput10.Text = "SM_OUT10";
            this.buttonOutput11.Text = "SM_OUT11";
            this.buttonOutput12.Text = "SM_OUT12";
            this.buttonOutput13.Text = "SM_OUT13";
            this.buttonOutput14.Text = "SM_OUT14";
            this.buttonOutput15.Text = "SM_OUT15";
            this.buttonOutput16.Text = "SM_OUT16";

            this.labelInputName1.Text = "SM_INT1";
            this.labelInputName2.Text = "SM_INT2";
            this.labelInputName3.Text = "SM_INT3";
            this.labelInputName4.Text = "SM_INT4";
            this.labelInputName5.Text = "SM_INT5";
            this.labelInputName6.Text = "SM_INT6";
            this.labelInputName7.Text = "SM_INT7";
            this.labelInputName8.Text = "SM_INT8";
        }

        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 Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.InitializeDesign();
            this.DefaultSetting();
            this.InitializeControl();
        }
        #endregion

        #region Event Handler
        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.InitializeControl();
            this.ChildControlIOTest.Visible = false;

            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }
        private void buttonDisplay_Click(object sender, EventArgs e)
        {
            if (this.ChildControlIOTest.Visible == false)
            {
                this.ChildControlIOTest.BringToFront();
                this.ChildControlIOTest.DisplayRefresh();
                this.ChildControlIOTest.Visible = true;
            }
            else
                this.ChildControlIOTest.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"));
            }
        }
        #endregion
    }
}