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 CalibUpdateEvent(object sendor);
    public delegate void CalibCmdEvent(int sId, int mode, int flag, ref DeviceParamData aParam, ref DeviceCalibration aCalib);

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

        int SystemId = 0;
        DeviceSystemData SystemData;
        DeviceParamData Param;
        DeviceCalibration Calib;

        public event CalibUpdateEvent OnUpdate = null;
        public event CalibCmdEvent OnCommand = null;

        #endregion

        #region CONSTRUCTORS

        public fmxCalibration()
        {
            InitializeComponent();
        }

        public fmxCalibration(int sId, DeviceCalibration aCalib)
        {
            InitializeComponent();

            InitComponent();

            SystemId = sId;
            Calib = aCalib;

            tmrDisplay.Enabled = true;
        }

        public void InitComponent()
        {
            // Cell Voltage Calibration
            ucCalibrationConfig.OnCommand += OnCmdEvent;
            ucCalibrationConfig.OnGetBattData += OnGetCellDataEvent;
        }

        #endregion

        #region COMMAND EVENT

        private void OnCmdEvent(int mode, int flag, ref DeviceCalibration aCalib, ref DeviceSystemTotalData aTotal)
        {
            OnCommand?.Invoke(SystemId, mode, flag, ref Param, ref aCalib);
        }

        private Int32 OnGetCellDataEvent(int item, int cno)
        {
            Int32 result = 0;

            switch (item)
            {
                case 0:     // Cell Voltagae Value
                    if ((cno > 0) && (cno <= csConstData.SystemInfo.MAX_MODULE_CELL_SIZE)) result = (Int32)SystemData.ValueData.CellVoltage[cno - 1];
                    break;
                case 1:
                    result = (Int32)SystemData.ValueData.voltageOfPack;
                    break;
                case 2:
                    result = (Int32)SystemData.ValueData.current;
                    break;
            }

            return result;
        }

        #endregion

        #region BUTTON EVENT

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void btnResetAll_Click(object sender, EventArgs e)
        {
            OnCommand?.Invoke(0xFF, 25, 1, ref Param, ref Calib);
        }

        #endregion

        #region TIMER EVENT

        private void tmrDisplay_Tick(object sender, EventArgs e)
        {

            DisplayValue();
            OnUpdate?.Invoke(this);
        }

        #endregion

        #region DISPLAY DATA

        private void DisplayValue()
        {
            // Cell Voltage Parameter
            ucCalibrationConfig.UpdateData(Calib);
        }

        #endregion

        #region PUBLIC FUCTIONS

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

        #endregion

    }
}