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

using System.Runtime.InteropServices;
using System.IO;
using System.IO.Ports;

using LFP_Manager.DataStructure;
using LFP_Manager.Function;

namespace LFP_Manager.Controls
{
    public delegate void CloseEvent(object aConfig);

    public partial class ucCommConfig : DevExpress.XtraEditors.XtraUserControl
    {
        #region VARIABLES

        CommConfig Config;
        public event CloseEvent OnClose = null;

        DataTable dtComport = new DataTable();

        #endregion

        #region CONSTRUCTORS

        public ucCommConfig()
        {
            InitializeComponent();

            CanDeviceLoad();
            UartPortLoad();
            IniLoad();
        }

        #endregion

        #region DATA LOAD AND SAVE

        private void CanDeviceLoad()
        {
            cbCanDevice.Properties.Items.Clear();
            foreach (string d_name in csCanConstData.CanDeviceInfo.DeviceNames)
            {
                cbCanDevice.Properties.Items.Add(d_name);
            }
        }

        private void InitComportDataTable()
        {
            dtComport = new DataTable();
            dtComport.Columns.Add("name", typeof(string));
            dtComport.Columns.Add("name2", typeof(string));
        }

        private void UartPortLoad()
        {
            InitComportDataTable();
            foreach (string comport in SerialPort.GetPortNames())
            {
                DataRow aRow = dtComport.NewRow();
                aRow["name"] = comport;
                dtComport.Rows.Add(aRow);
            }

            cbUartPort.Properties.Items.Clear();
            for (int i = 0; i < dtComport.Rows.Count; i++)
            {
                cbUartPort.Properties.Items.Add(dtComport.Rows[i]["name"].ToString());
            }

            cbUartSpeed.Properties.Items.Clear();
            for (int i = 0; i < csConstData.CommType.UartCAN_Speed.Length; i++)
            {
                cbUartSpeed.Properties.Items.Add(csConstData.CommType.UartCAN_Speed[i]);
            }
        }

        private void IniLoad()
        {
            Config = csIniControlFunction.IniLoad(Application.ExecutablePath, Config);

            cbTargetModel.Properties.Items.Clear();
            foreach (string d_name in csConstData.CommType.CAN_MODEL)
            {
                cbTargetModel.Properties.Items.Add(d_name);
            }

            cbCanDevice.SelectedIndex = Config.CanDevice;
            cbCanIndex.SelectedIndex = Config.CanIndex;
            cbCanNo.SelectedIndex = Config.CanNo;
            cbCanBaudrate.SelectedIndex = Config.CanBaudrate;
            cbTargetModel.SelectedIndex = Config.TargetModelIndex;

            cbUartPort.Text = Config.UartPort;
            cbUartSpeed.SelectedIndex = Config.UartBaudrate;
            
            switch (Config.CommType)
            {
                case csConstData.CommType.COMM_SNMP:
                    break;
                case csConstData.CommType.COMM_USBCAN:
                    rbtnUSBCAN.Checked = true;
                    break;
                case csConstData.CommType.COMM_UARTCAN:
                    rbtnUARTCAN.Checked = true;

                    bool found = false;
                    int ComId = 0;

                    if (Config.UartPort != "")
                    {
                        for (int i = 0; i < cbUartPort.Properties.Items.Count; i++)
                        {
                            if (dtComport.Rows[i]["name"].ToString() == Config.UartPort)
                            {
                                found = true;
                                ComId = i;
                                break;
                            }
                        }
                    }

                    if (found)
                    {
                        cbUartPort.SelectedIndex = ComId;
                        Config.UartPort = cbUartPort.Properties.Items[ComId].ToString();
                    }
                    else
                    {
                        if (cbUartPort.Properties.Items.Count > 0)
                            cbUartPort.SelectedIndex = 0;
                    }
                    break;
            }
            cbDbLogPeriod.Text = Config.DbLogPeriod.ToString();
        }

        private void IniSave()
        {
            if (rbtnUARTCAN.Checked)
                Config.CommType = csConstData.CommType.COMM_UARTCAN;
            else if (rbtnUSBCAN.Checked)
                Config.CommType = csConstData.CommType.COMM_USBCAN;

            Config.CanDevice = cbCanDevice.SelectedIndex;
            Config.CanIndex = cbCanIndex.SelectedIndex;
            Config.CanNo = cbCanNo.SelectedIndex;
            Config.CanBaudrate = cbCanBaudrate.SelectedIndex;
            Config.TargetModelIndex = cbTargetModel.SelectedIndex;

            Config.UartPort = cbUartPort.Text;
            Config.UartBaudrate = cbUartSpeed.SelectedIndex;

            Config.DbLogPeriod = Convert.ToInt32(cbDbLogPeriod.Text);

            csIniControlFunction.IniSave(Application.ExecutablePath, Config);
        }

        #endregion

        #region EVENT

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (OnClose != null) OnClose(Config);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            IniSave();
        }

        private void cbCanDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            int DeviceID = csCanConstData.CanDeviceInfo.DeviceIds[cbCanDevice.SelectedIndex];

            if (DeviceID == csCanConstData.CanDeviceInfo.VCI_VALUE_CAN4_1)
            {
                cbCanIndex.Enabled = false;
                cbCanNo.Enabled = false;
            }
            else
            {
                cbCanIndex.Enabled = true;
                cbCanNo.Enabled = true;
            }
        }

        private void rbtnUSBCAN_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnUSBCAN.Checked)
            {
                cbCanDevice.Enabled = true;
                cbCanIndex.Enabled = true;
                cbCanNo.Enabled = true;
                cbCanBaudrate.Enabled = true;

                cbUartPort.Enabled = false;
                cbUartSpeed.Enabled = false;
            }
            else if (rbtnUARTCAN.Checked)
            {
                cbCanDevice.Enabled = false;
                cbCanIndex.Enabled = false;
                cbCanNo.Enabled = false;
                cbCanBaudrate.Enabled = false;

                cbUartPort.Enabled = true;
                cbUartSpeed.Enabled = true;
            }
        }

        private void rbtnUARTCAN_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnUSBCAN.Checked)
            {
                cbCanDevice.Enabled = true;
                cbCanIndex.Enabled = true;
                cbCanNo.Enabled = true;
                cbCanBaudrate.Enabled = true;

                cbUartPort.Enabled = false;
                cbUartSpeed.Enabled = false;
            }
            else if (rbtnUARTCAN.Checked)
            {
                cbCanDevice.Enabled = false;
                cbCanIndex.Enabled = false;
                cbCanNo.Enabled = false;
                cbCanBaudrate.Enabled = false;

                cbUartPort.Enabled = true;
                cbUartSpeed.Enabled = true;
            }
        }

        #endregion
    }
}
