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

using LFP_Manager.Forms;
using LFP_Manager.DataStructure;
using LFP_Manager.Utils;

namespace LFP_Manager.Controls
{
    public partial class ucModuleIdSet : DevExpress.XtraEditors.XtraUserControl
    {
        #region VARIABLES

        int mID = 0;

        DeviceCalibration rCalib;
        DeviceCalibration wCalib;

        DeviceParamData rParam;

        int maxModule = 1;
        //UInt32 cValue = 0;

        //bool wFlag = false;

        public event CalibCmdEvent OnCommand = null;

        #endregion

        public ucModuleIdSet()
        {
            InitializeComponent();
        }

        #region EXT EVENT FUNCTION

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

        #endregion

        #region PUBLIC FUCTIONS

        public void UpdateData(int sId, DeviceCalibration aCalib)
        {
            mID = sId;
            rCalib = aCalib;
            DisplayCalib();
        }

        public void SetMaxModule(int aMaxModule)
        {
            maxModule = aMaxModule;
        }

        #endregion

        #region DISPLAY DATA

        private void DisplayCalib()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate ()
                {
                    // Device Address
                    edSystemAddr.Text = String.Format("{0:0}", rCalib.SystemInfo.devAddr);
                }));
            }
            else
            {
                // Device Address
                edSystemAddr.Text = String.Format("{0:0}", rCalib.SystemInfo.devAddr);
            }
        }

        #endregion

        private void edModuleNo_EditValueChanged(object sender, EventArgs e)
        {
            if (edModuleNo.Text != "")
            {
                int mdID;
                int mdNo;
                string strMdNo = edModuleNo.Text;

                try
                {
                    mdNo = Convert.ToInt32(strMdNo);

                    mdID = (mdNo - 1) % maxModule + 1;

                    edSystemAddrNew.Text = mdID.ToString();
                }
                catch (Exception ex)
                {
                    edModuleNo.Text = "";
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        #region BUTTON EVENT

        private void btnBmsAddrGet_Click(object sender, EventArgs e)
        {
            OnCommnadEvent(mID, 9, 0, ref rParam, ref wCalib);
        }

        private void btnBmsAddrSet_Click(object sender, EventArgs e)
        {
            if (edSystemAddrNew.Text == string.Empty) return;
            wCalib = rCalib;
            wCalib.SystemInfo.devAddr = (UInt16)(Convert.ToInt32(edSystemAddrNew.Text));

            OnCommnadEvent(mID, 9, 1, ref rParam, ref wCalib);
        }

        #endregion
    }
}
