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;
using static DevExpress.XtraPrinting.Export.Pdf.PdfImageCache;

namespace LFP_Manager.Forms
{
    public delegate void CalibUpdateEvent(object sendor);
    public delegate void CalibCmdEvent(int sId, int mode, int index, 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, CommConfig aConfig, DeviceParamData aParam, DeviceCalibration aCalib)
        {
            InitializeComponent();

            InitComponent();

            ucCalibrationConfig.UpdateConfig(aConfig);

            SystemId = sId;
            Param = aParam;
            Calib = aCalib;

            tmrDisplay.Enabled = true;
        }

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

        #endregion

        #region COMMAND EVENT

        private void OnCmdEvent(int mode, int index, int flag, ref DeviceParamData aParam, ref DeviceCalibration aCalib)
        {
            OnCommand?.Invoke(SystemId, mode, index, flag, ref aParam, ref aCalib);
        }

        #endregion

        #region BUTTON EVENT
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion

        #region TIMER EVENT

        private void tmrDisplay_Tick(object sender, EventArgs e)
        {

            DisplayValue();
            if (OnUpdate != null)
            {
                OnUpdate(this);
            }
        }

        #endregion

        #region DISPLAY DATA

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

        #endregion

        #region PUBLIC FUCTIONS

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

        #endregion
    }
}