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

using LFP_Manager.DataStructure;
using LFP_Manager.Function;
using LFP_Manager.Controls;

namespace LFP_Manager.Forms
{
    public delegate void ParamUpdateEvent(object sendor);
    public delegate void ParamCmdEvent(int sId, int mode, int flag, ref DeviceParamData aParam, ref DeviceCalibration aCalib);

    public partial class fmxParamConfig : DevExpress.XtraEditors.XtraForm
    {
        #region VARIABLES

        int SystemId = 0;
        DeviceParamData Param;
        DeviceCalibration Calib;

        public event ParamUpdateEvent OnUpdate = null;
        public event ParamCmdEvent OnCommand = null;

        #endregion

        #region CONSTRUCTORS

        public fmxParamConfig()
        {
            InitializeComponent();
        }

        public fmxParamConfig(int sId, DeviceParamData aParam)
        {
            InitializeComponent();

            SystemId = sId;
            Param = aParam;

            InitComponent();

            tmrDisplay.Enabled = true;
        }

        public void InitComponent()
        {
            // Cell Voltage Parameter
            ucCOVParam.SetParamName(0, "Cell Over Voltage");
            ucCOVParam.SetParamName(1, "mV");
            ucCOVParam.SetId(0);
            ucCOVParam.OnCommand += OnCmdEvent;

            ucCUVParam.SetParamName(0, "Cell Under Voltage");
            ucCUVParam.SetParamName(1, "mV");
            ucCUVParam.SetId(1);
            ucCUVParam.OnCommand += OnCmdEvent;
            //ucCUVParam.EnableItem(0, false);

            // System Voltage Parameter
            ucSOVParam.SetParamName(0, "System Over Voltage");
            ucSOVParam.SetParamName(1, "V");
            ucSOVParam.SetId(2);
            ucSOVParam.OnCommand += OnCmdEvent;

            ucSUVParam.SetParamName(0, "System Under Voltage");
            ucSUVParam.SetParamName(1, "V");
            ucSUVParam.SetId(3);
            ucSUVParam.OnCommand += OnCmdEvent;
            //ucSUVParam.EnableItem(0, false);

            // Charge Temperature Parameter
            ucCHTParam.SetParamName(0, "Charge High Temp");
            ucCHTParam.SetParamName(1, "C");
            ucCHTParam.SetId(4);
            ucCHTParam.OnCommand += OnCmdEvent;

            ucCLTParam.SetParamName(0, "Charge Low Temp");
            ucCLTParam.SetParamName(1, "C");
            ucCLTParam.SetId(5);
            ucCLTParam.OnCommand += OnCmdEvent;

            // Discharge Temperature Parameter
            ucDHTParam.SetParamName(0, "Discharge High Temp");
            ucDHTParam.SetParamName(1, "C");
            ucDHTParam.SetId(6);
            ucDHTParam.OnCommand += OnCmdEvent;
            //ucDHTParam.EnableItem(0, false);

            ucDLTParam.SetParamName(0, "Discharge Low Temp");
            ucDLTParam.SetParamName(1, "C");
            ucDLTParam.SetId(7);
            ucDLTParam.OnCommand += OnCmdEvent;
            //ucDLTParam.EnableItem(0, false);

            // Discharge Temperature Parameter
            ucCOCParam.SetParamName(0, "Charge Over Current");
            ucCOCParam.SetParamName(1, "A");
            ucCOCParam.SetId(8);
            ucCOCParam.OnCommand += OnCmdEvent;

            ucDOCParam.SetParamName(0, "Discharge Over Current");
            ucDOCParam.SetParamName(1, "A");
            ucDOCParam.SetId(9);
            ucDOCParam.OnCommand += OnCmdEvent;
            //ucDOCParam.EnableItem(0, false);

            // SOC Parameter
            ucLowSocParam.SetParamName(0, "Low SOC");
            ucLowSocParam.SetParamName(1, "%");
            ucLowSocParam.SetId(10);
            ucLowSocParam.OnCommand += OnCmdEvent;
            ucLowSocParam.EnableItem(0, false);

            // SOC Parameter
            ucCvDiffParam.SetParamName(0, "Cell Voltage Diff");
            ucCvDiffParam.SetParamName(1, "mV");
            ucCvDiffParam.SetId(11);
            ucCvDiffParam.OnCommand += OnCmdEvent;
            //ucCvDiffParam.EnableItem(0, false);
        }

        #endregion

        #region COMMAND EVENT

        private void OnCmdEvent(int mode, int flag, ParamData aParam)
        {
            int cmd = 0;
            DeviceParamData wParam = Param;

            switch (mode)
            {
                case 0:                 // Cell Over Voltage
                    cmd = 5;
                    wParam.CellOverVoltageTrip = aParam.Fault;
                    wParam.CellOverVoltageWarning = aParam.Warning;
                    wParam.CellOverVoltageRelease = aParam.Release;
                    break;
                case 1:                 // Cell Under Voltage
                    cmd = 5;
                    wParam.CellUnderVoltageTrip = aParam.Fault;
                    wParam.CellUnderVoltageWarning = aParam.Warning;
                    wParam.CellUnderVoltageRelease = aParam.Release;
                    break;
                case 2:                 // System Over Voltage
                    cmd = 7;
                    wParam.SysOverVoltageTrip = aParam.Fault;
                    wParam.SysOverVoltageWarning = aParam.Warning;
                    wParam.SysOverVoltageRelease = aParam.Release;
                    break;
                case 3:                 // System Under Voltage
                    cmd = 7;
                    wParam.SysUnderVoltageTrip = aParam.Fault;
                    wParam.SysUnderVoltageWarning = aParam.Warning;
                    wParam.SysUnderVoltageRelease = aParam.Release;
                    break;
                case 4:                 // Charge High Temperature
                    cmd = 6;
                    wParam.ChaHighTempTrip = aParam.Fault;
                    wParam.ChaHighTempWarning = aParam.Warning;
                    wParam.ChaHighTempRelease = aParam.Release;
                    break;
                case 5:                 // Charge Low Temperature
                    cmd = 6;
                    wParam.ChaLowTempTrip = aParam.Fault;
                    wParam.ChaLowTempWarning = aParam.Warning;
                    wParam.ChaLowTempRelease = aParam.Release;
                    break;
                case 6:                 // Discharge High Temperature
                    cmd = 6;
                    wParam.DchHighTempTrip = aParam.Fault;
                    wParam.DchHighTempWarning = aParam.Warning;
                    wParam.DchHighTempRelease = aParam.Release;
                    break;
                case 7:                 // Discharge Low Temperature
                    cmd = 6;
                    wParam.DchLowTempTrip = aParam.Fault;
                    wParam.DchLowTempWarning = aParam.Warning;
                    wParam.DchLowTempRelease = aParam.Release;
                    break;
                case 8:                 // Charge Over Current
                    cmd = 22;
                    wParam.ChaOverCurrentTrip = aParam.Fault;
                    wParam.ChaOverCurrentWarning = aParam.Warning;
                    wParam.ChaOverCurrentRelease = aParam.Release;
                    break;
                case 9:                 // Discharge Over Current
                    cmd = 22;
                    wParam.DchOverCurrentTrip = aParam.Fault;
                    wParam.DchOverCurrentWarning = aParam.Warning;
                    wParam.DchOverCurrentRelease = aParam.Release;
                    break;
                case 10:                // Low SOC
                    cmd = 14;
                    wParam.LowSocWarning = aParam.Warning;
                    wParam.LowSocRelease = aParam.Release;
                    break;
                case 11:                // Cell Voltage Difference
                    cmd = 2;
                    wParam.CellVoltageDifferenceTrip = aParam.Fault;
                    wParam.CellVoltageDifferenceWarning = aParam.Warning;
                    wParam.CellVoltageDifferenceRelease = aParam.Release;
                    break;
                case 16:                // Default Parameter
                    cmd = 16;
                    wParam.DefalutParamOption = aParam.Fault;
                    wParam.DefalutParamAll = aParam.Release;
                    break;
                case 99:
                    cmd = 99;           // All parameter read
                    break;
            }
            OnCommand?.Invoke(SystemId, cmd, flag, ref wParam, ref Calib);
        }

        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void UpdateData(DeviceParamData aParam, DeviceCalibration aCalib)
        {
            Param = aParam;
            Calib = aCalib;
        }

        private void tmrDisplay_Tick(object sender, EventArgs e)
        {
            DisplayValue();

            OnUpdate?.Invoke(this);
        }

        private void DisplayValue()
        {
            ParamData pData = new ParamData();

            // Cell Voltage Parameter
            pData.Fault = Param.CellOverVoltageTrip;
            pData.Warning = Param.CellOverVoltageWarning;
            pData.Release = Param.CellOverVoltageRelease;

            ucCOVParam.UpdateData(pData);

            pData.Fault = Param.CellUnderVoltageTrip;
            pData.Warning = Param.CellUnderVoltageWarning;
            pData.Release = Param.CellUnderVoltageRelease;

            ucCUVParam.UpdateData(pData);

            // System Voltage Parameter
            pData.Fault = Param.SysOverVoltageTrip;
            pData.Warning = Param.SysOverVoltageWarning;
            pData.Release = Param.SysOverVoltageRelease;

            ucSOVParam.UpdateData(pData);

            pData.Fault = Param.SysUnderVoltageTrip;
            pData.Warning = Param.SysUnderVoltageWarning;
            pData.Release = Param.SysUnderVoltageRelease;

            ucSUVParam.UpdateData(pData);

            // Charge Temp Parameter
            pData.Fault = Param.ChaHighTempTrip;
            pData.Warning = Param.ChaHighTempWarning;
            pData.Release = Param.ChaHighTempRelease;

            ucCHTParam.UpdateData(pData);

            pData.Fault = Param.ChaLowTempTrip;
            pData.Warning = Param.ChaLowTempWarning;
            pData.Release = Param.ChaLowTempRelease;

            ucCLTParam.UpdateData(pData);

            // Discharge Temp Parameter
            pData.Fault = Param.DchHighTempTrip;
            pData.Warning = Param.DchHighTempWarning;
            pData.Release = Param.DchHighTempRelease;

            ucDHTParam.UpdateData(pData);

            pData.Fault = Param.DchLowTempTrip;
            pData.Warning = Param.DchLowTempWarning;
            pData.Release = Param.DchLowTempRelease;

            ucDLTParam.UpdateData(pData);

            // Charge Over Current
            pData.Fault = Param.ChaOverCurrentTrip;
            pData.Warning = Param.ChaOverCurrentWarning;
            pData.Release = Param.ChaOverCurrentRelease;

            ucCOCParam.UpdateData(pData);

            // Discharge Over Current
            pData.Fault = Param.DchOverCurrentTrip;
            pData.Warning = Param.DchOverCurrentWarning;
            pData.Release = Param.DchOverCurrentRelease;

            ucDOCParam.UpdateData(pData);

            // Low SOC
            pData.Fault = Param.LowSocTrip;
            pData.Warning = Param.LowSocWarning;
            pData.Release = Param.LowSocRelease;

            ucLowSocParam.UpdateData(pData);

            // Cell Votage Difference
            pData.Fault = Param.CellVoltageDifferenceTrip;
            pData.Warning = Param.CellVoltageDifferenceWarning;
            pData.Release = Param.CellVoltageDifferenceRelease;

            ucCvDiffParam.UpdateData(pData);
        }

        private void btnGetAll_Click(object sender, EventArgs e)
        {
            ParamData aa = new ParamData();
            OnCmdEvent(99, 0, aa);
        }

        private void btnDefaultSet_Click(object sender, EventArgs e)
        {
            ParamData aa = new ParamData();

            if (cbFactoryDefault.Checked)
                aa.Fault = 1;
            else
                aa.Fault = 0;

            if (chbAll.Checked)
                aa.Release = 1;
            else
                aa.Release = 0;

            OnCmdEvent(16, 1, aa);
        }
    }
}