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

using INT51DB.Forms;
using INT51DB_ImageDll;

namespace INT51DB.Controls
{
    public partial class ControlCenterConfiCommunication : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        #endregion

        #region Constructor
        public ControlCenterConfiCommunication(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.comboBoxSerial2BaudRate.Items.Clear();
            this.comboBoxSerial2BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial2_SelectedIndexChanged);
            this.comboBoxSerial2BaudRate.Items.Add(9600);
            this.comboBoxSerial2BaudRate.Items.Add(19200);
            this.comboBoxSerial2BaudRate.Items.Add(38400);
            this.comboBoxSerial2BaudRate.Items.Add(115200);
            this.comboBoxSerial2BaudRate.SelectedIndex = 0;
            this.comboBoxSerial2BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial2_SelectedIndexChanged);

            this.comboBoxSerial2Mode.Items.Clear();
            this.comboBoxSerial2Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial2_SelectedIndexChanged);
            this.comboBoxSerial2Mode.Items.Add("None");
            this.comboBoxSerial2Mode.Items.Add("STD1");
            this.comboBoxSerial2Mode.SelectedIndex = 0;
            this.comboBoxSerial2Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial2_SelectedIndexChanged);

            this.comboBoxSerial3BaudRate.Items.Clear();
            this.comboBoxSerial3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
            this.comboBoxSerial3BaudRate.Items.Add(9600);
            this.comboBoxSerial3BaudRate.Items.Add(19200);
            this.comboBoxSerial3BaudRate.Items.Add(38400);
            this.comboBoxSerial3BaudRate.Items.Add(115200);
            this.comboBoxSerial3BaudRate.SelectedIndex = 0;
            this.comboBoxSerial3BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);

            this.comboBoxSerial3Mode.Items.Clear();
            this.comboBoxSerial3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
            this.comboBoxSerial3Mode.Items.Add("None");
            this.comboBoxSerial3Mode.Items.Add("STD1");
            this.comboBoxSerial3Mode.SelectedIndex = 0;
            this.comboBoxSerial3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
        }

        private void UpdateSerialParameterDisplay(SystemConfigurationItem item, SystemParameter1 parameter)
        {
            int index = 0;
            string value = "";

            // CH2. BaudRate 
            if (this.comboBoxSerial2BaudRate.SelectedIndex != item.COM3BaudRate)
            {
                this.comboBoxSerial2BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial2_SelectedIndexChanged);
                this.comboBoxSerial2BaudRate.SelectedIndex = item.COM3BaudRate;
                this.comboBoxSerial2BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial2_SelectedIndexChanged);
            }
            // CH2. Mode
            if (this.comboBoxSerial2Mode.SelectedIndex != item.COM3Mode)
            {
                this.comboBoxSerial2Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial2_SelectedIndexChanged);
                this.comboBoxSerial2Mode.SelectedIndex = item.COM3Mode;
                this.comboBoxSerial2Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial2_SelectedIndexChanged);
            }

            // CH3. BaudRate
            if (this.comboBoxSerial3BaudRate.SelectedIndex != item.COM4BaudRate)
            {
                this.comboBoxSerial3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
                this.comboBoxSerial3BaudRate.SelectedIndex = item.COM4BaudRate;
                this.comboBoxSerial3BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
            }
            // CH3. Mode
            if (this.comboBoxSerial3Mode.SelectedIndex != item.COM4Mode)
            {
                this.comboBoxSerial3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
                this.comboBoxSerial3Mode.SelectedIndex = item.COM4Mode;
                this.comboBoxSerial3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
            }
        }

        public void DisplayRefresh(DataStore.UserGroup group)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.ConfiCommunication;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.UpdateSerialParameterDisplay(this.ParentForm.ParentForm.SystemConfig, this.ParentForm.ParentForm.CurrentSystemParameter1);

            this.buttonSave2.Enabled = false;
            this.buttonSave3.Enabled = false;
        }
        #endregion

        #region Event Handler
        private void comboBoxSerial2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.buttonSave2.Enabled = true;
        }
        private void comboBoxSerial3_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.buttonSave3.Enabled = true;
        }

        private void buttonSave2_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.SystemConfig.COM3BaudRate = this.comboBoxSerial2BaudRate.SelectedIndex;
            this.ParentForm.ParentForm.SystemConfig.COM3Mode = this.comboBoxSerial2Mode.SelectedIndex;

            this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);

            this.ParentForm.ParentForm.OpenSerialCOM3();

            this.buttonSave2.Enabled = false;
        }
        private void buttonSave3_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.SystemConfig.COM4BaudRate = this.comboBoxSerial3BaudRate.SelectedIndex;
            this.ParentForm.ParentForm.SystemConfig.COM4Mode = this.comboBoxSerial3Mode.SelectedIndex;

            this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);

            this.ParentForm.ParentForm.OpenSerialCOM4();

            this.buttonSave3.Enabled = false;
        }
        #endregion
    }
}
