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 INT63DC_ImageDll;
using INT63DC_2C.Controls;

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

        private Color ColorIOStatusOn;
        private Color ColorIOStatusOff;

        private string Text_Out;
        private string Text_In;

        private Collection<Label> CollectionInputLabel;
        private Collection<Label> CollectionInputLabel1;
        private Collection<SmartLabel> CollectionInputLabelName;
        private Collection<SmartButton> CollectionOutputButton;

        // OUT 1 : RED LAMP
        // OUT 2 : GRN LAMP
        // OUT 3 : BUZZER
        // OUT 4 : OPT_OUT
        // OUT 5 : OPER
        // OUT 6 : ??
        // OUT 7 : GATE
        // OUT 8 : STOPPER
        
        // OUT 9~16 : SERVO

        // OUT 17 : REJ 1
        // OUT 18 : REJ 2
        // OUT 19 : REJ 3
        // OUT 20 : REJ 4
        // OUT 21 : REJ 5
        // OUT 22 : REJ 6
        // OUT 23 : REJ 7
        // OUT 24 : REJ 8
        // OUT 25 : REJ 9
        // OUT 26 : REJ 10
        // OUT 27 : REJ 11
        // OUT 28 : REJ 12
        // OUT 29 : NONE
        // OUT 30 : N REJ
        // OUT 31 : S REJ
        // OUT 32 : NONE
        #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();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    this.Text_Out = "OUT";
                    this.Text_In = "IN";
                    break;
                case DataStore.E_LanguageID.English:
                    #region English
                    this.labelTitle.Text = "I/O Test";

                    this.Text_Out = "OUT";
                    this.Text_In = "IN";

                    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)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Chinese:
                    #region Chinese
                    this.labelTitle.Text = "I/O测试";

                    this.Text_Out = "OUT";
                    this.Text_In = "IN";

                    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)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Czech:
                    #region Czech
                    this.labelTitle.Text = "Test I/O";

                    this.Text_Out = "OUT";
                    this.Text_In = "IN";

                    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)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.German:
                    #region German
                    this.labelTitle.Text = "I/O Test";

                    this.Text_Out = "OUT";
                    this.Text_In = "IN";

                    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)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Japanese:
                    #region Japanese
                    this.labelTitle.Text = "I/O テスト";

                    this.smartGroupBox1.Text = "出力";
                    this.smartGroupBox2.Text = "入力";

                    this.Text_Out = "出力";
                    this.Text_In = "入力";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackUp)); 
                    #endregion
                    break;
                default:
                    break;
            }
        }
        private void InitializeControl()
        {
            foreach (Label label in this.CollectionInputLabel)
                this.SetlabelOnOff(label, false);

            foreach (Label label in this.CollectionInputLabel1)
                this.SetlabelOnOff(label, false);

            foreach (SmartButton button in this.CollectionOutputButton)
                button.ButtonUp();
        }
        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.CollectionInputLabel1 = new Collection<Label>();
            this.CollectionInputLabel1.Clear();
            this.CollectionInputLabel1.Add(this.labelInput17);
            this.CollectionInputLabel1.Add(this.labelInput18);
            this.CollectionInputLabel1.Add(this.labelInput19);
            this.CollectionInputLabel1.Add(this.labelInput20);
            this.CollectionInputLabel1.Add(this.labelInput21);
            this.CollectionInputLabel1.Add(this.labelInput22);
            this.CollectionInputLabel1.Add(this.labelInput23);
            this.CollectionInputLabel1.Add(this.labelInput24);
            this.CollectionInputLabel1.Add(this.labelInput25);
            this.CollectionInputLabel1.Add(this.labelInput26);
            this.CollectionInputLabel1.Add(this.labelInput27);
            this.CollectionInputLabel1.Add(this.labelInput28);
            this.CollectionInputLabel1.Add(this.labelInput29);
            this.CollectionInputLabel1.Add(this.labelInput30);
            this.CollectionInputLabel1.Add(this.labelInput31);
            this.CollectionInputLabel1.Add(this.labelInput32);

            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.CollectionInputLabelName.Add(this.labelInputName17);
            this.CollectionInputLabelName.Add(this.labelInputName18);
            this.CollectionInputLabelName.Add(this.labelInputName19);
            this.CollectionInputLabelName.Add(this.labelInputName20);
            this.CollectionInputLabelName.Add(this.labelInputName21);
            this.CollectionInputLabelName.Add(this.labelInputName22);
            this.CollectionInputLabelName.Add(this.labelInputName23);
            this.CollectionInputLabelName.Add(this.labelInputName24);
            this.CollectionInputLabelName.Add(this.labelInputName25);
            this.CollectionInputLabelName.Add(this.labelInputName26);
            this.CollectionInputLabelName.Add(this.labelInputName27);
            this.CollectionInputLabelName.Add(this.labelInputName28);
            this.CollectionInputLabelName.Add(this.labelInputName29);
            this.CollectionInputLabelName.Add(this.labelInputName30);
            this.CollectionInputLabelName.Add(this.labelInputName31);
            this.CollectionInputLabelName.Add(this.labelInputName32);

            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);
            this.CollectionOutputButton.Add(this.buttonOutput33);
            this.CollectionOutputButton.Add(this.buttonOutput34);
            this.CollectionOutputButton.Add(this.buttonOutput35);
            this.CollectionOutputButton.Add(this.buttonOutput36);
            this.CollectionOutputButton.Add(this.buttonOutput37);
            this.CollectionOutputButton.Add(this.buttonOutput38);
            this.CollectionOutputButton.Add(this.buttonOutput39);
            this.CollectionOutputButton.Add(this.buttonOutput40);

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

            if (this.ParentForm.SystemConfig.EquipmentColumns <= 12)
            {
                for (int i = 32; i < 40; i++)
                    this.CollectionOutputButton[i].Visible = false;

                for (int i = 0; i < 32; i++)
                {
                    this.CollectionOutputButton[i].Location =
                        new Point(this.CollectionOutputButton[i].Location.X + 50, this.CollectionOutputButton[i].Location.Y);
                }
            }
        }

        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 UpdateInputRead1(Collection<string> values)
        {
            if (values.Count == 0 || values == null)
                return;

            for (int i = 0; i < values.Count; i++)
                this.SetlabelOnOff(this.CollectionInputLabel1[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.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }
        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
    }
}