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 LFP_Manager.Threads;

namespace LFP_Manager.Forms
{
    public delegate void AntiTheftUpdateEvent(object sendor);
    public delegate void AntiTheftCmdEvent(int sId, int mode, int index, int flag, 
        ref CsDeviceData.DeviceModuleData.DeviceParamData aParam, 
        ref CsDeviceData.DeviceModuleData.DeviceCalibration aCalib);

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

        int SystemId = 0;
        CsDeviceData.DeviceModuleData SystemData;
        CsDeviceData.DeviceModuleData.DeviceParamData Param;
        CsDeviceData.DeviceModuleData.DeviceCalibration Calib;

        public event AntiTheftUpdateEvent OnUpdate = null;
        public event AntiTheftCmdEvent OnCommand = null;
        public event SendDataUartEvent OnSendUartData = null;

        #endregion

        #region CONSTRUCTORS

        public fmxAntiTheft(int sId, CsDeviceData.DeviceModuleData.DeviceCalibration aCalib)
        {
            InitializeComponent();

            InitComponent();

            SystemId = sId;
            Calib = aCalib;

            AntiTheftCmd.UpdateData(SystemId, Calib);
            AntiTheftCmd.OnSendUartData += SetWriteData;

            tmrDisplay.Enabled = true;
        }

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

        #endregion

        #region COMMAND EVENT
        private void SetWriteData(ushort WriteAddr, byte[] WriteData, bool RelyFlag, int type)
        {
            OnSendUartData?.Invoke(WriteAddr, WriteData, RelyFlag, type);
        }

        private void OnCmdEvent(int mode, int index, int flag, 
            ref CsDeviceData.DeviceModuleData.DeviceParamData aParam, 
            ref CsDeviceData.DeviceModuleData.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
            AntiTheftCmd.UpdateData(SystemId, Calib);
        }

        #endregion

        #region PUBLIC FUCTIONS

        public void UpdateData(CsDeviceData.DeviceModuleData.DeviceParamData aParam, 
            CsDeviceData.DeviceModuleData.DeviceCalibration aCalib, 
            CsDeviceData.DeviceModuleData mData)
        {
            SystemData = mData;
            Param = aParam;
            Calib = aCalib;
        }

        #endregion

        private void AntiTheftCmd_Load(object sender, EventArgs e)
        {

        }
    }
}