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

using LFP_Manager.DataStructure;
using LFP_Manager.Function;
using LFP_Manager.Utils;

namespace LFP_Manager.Controls
{
    public delegate void setUpdate(object sender);
    public delegate void setCommand(int mode, int flag, ParamData aParam);

    public class ParamData
    {
        public short Fault;
        public short Warning;
        public short Release;
        public short Time;

        public ParamData()
        {
            Fault = 0;
            Warning = 0;
            Release = 0;
            Time = 0;
        }

        public ParamData DeepCopy()
        {
            ParamData newCopy = new ParamData
            {
                Fault = Fault,
                Warning = Warning,
                Release = Release,
                Time = Time,
            };
            return newCopy;
        }
    }

    public partial class ucParamSet : UserControl
    {
        #region VARIABLES

        int id = 0;
        ParamData rParam;
        ParamData wParam;

        bool wFlag = false;

        public event setCommand OnCommand = null;

        #endregion

        #region CONSTRUCTORS

        public ucParamSet()
        {
            InitializeComponent();

            rParam = new ParamData();
            wParam = new ParamData();
        }

        #endregion

        #region EXT EVENT FUNCTION

        private void OnCommnadEvent(int mode, int flag, ParamData aParam)
        {
            OnCommand?.Invoke(mode, flag, aParam);
        }

        #endregion

        #region BUTTON EVENT

        private void btnRead_Click(object sender, EventArgs e)
        {
            OnCommnadEvent(id, 0, rParam);
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            if ((edTripNew.Text == "") || (edWarningNew.Text == "") || (edReleaseNew.Text == ""))
            {
                return;
            }
            wParam = rParam.DeepCopy();
            OnCommnadEvent(id, 1, MakeNewParam(id));
        }

        private ParamData MakeNewParam(int aId)
        {
            switch (aId)
            {
                case 0:                 // Cell Over Voltage
                case 1:                 // Cell Under Voltage
                    wParam.Fault = Convert.ToInt16(edTripNew.Text);
                    wParam.Warning = Convert.ToInt16(edWarningNew.Text);
                    wParam.Release = Convert.ToInt16(edReleaseNew.Text);
                    break;
                case 2:                 // Module Over Voltage
                case 3:                 // Module Under Voltage
                    wParam.Fault = (short)(Convert.ToDouble(edTripNew.Text) * 100);
                    wParam.Warning = (short)(Convert.ToDouble(edWarningNew.Text) * 100);
                    wParam.Release = (short)(Convert.ToDouble(edReleaseNew.Text) * 100);
                    break;
                case 4:                 // Charge High Temperature
                case 5:                 // Charge Loq Temperature
                case 6:                 // Discharge High Temperature
                case 7:                 // Discharge Low Temperature
                case 10:                // Low SOC
                case 11:                // Low SOC
                    wParam.Fault = Convert.ToInt16(edTripNew.Text);
                    wParam.Warning = Convert.ToInt16(edWarningNew.Text);
                    wParam.Release = Convert.ToInt16(edReleaseNew.Text);
                    break;
                case 8:                 // Charge Over Current
                case 9:                 // Discharge Over Current
                    wParam.Fault = (short)(Convert.ToDouble(edTripNew.Text) * 10);
                    wParam.Warning = (short)(Convert.ToDouble(edWarningNew.Text) * 10);
                    wParam.Release = (short)(Convert.ToDouble(edReleaseNew.Text) * 10);
                    break;
            }
            return wParam;
        }

        #endregion

        #region PUBLIC FUCTIONS

        public void SetId(int aId)
        {
            id = aId;
        }

        public void UpdateData(ParamData aParam)
        {
            rParam = aParam;
            DisplayParam();
            UpdateNewParam();
        }

        public void EnableItem(int item, bool flag)
        {
            switch (item)
            {
                case 0:
                    edTrip.Enabled = flag;
                    edTripNew.Enabled = flag;
                    break;
                case 1:
                    edWarning.Enabled = flag;
                    edWarningNew.Enabled = flag;
                    break;
                case 2:
                    edRelease.Enabled = flag;
                    edReleaseNew.Enabled = flag;
                    break;
            }
        }

        public void SetParamName(int no, string nm)
        {
            switch (no)
            {
                case 0: gbParamSet.Text = nm; break;
                case 1:
                    lbTripUnit.Text = nm;
                    lbWarningUnit.Text = nm;
                    lbReleaseUnit.Text = nm;
                    break;
            }
        }

        #endregion

        #region DISPLAY

        private void DisplayParam()
        {
            ParamData Param = new ParamData();
            Param = rParam;

            switch (id)
            {
                case 0:
                case 1:
                    edTrip.Text = String.Format("{0}", Param.Fault);
                    edWarning.Text = String.Format("{0}", Param.Warning);
                    edRelease.Text = String.Format("{0}", Param.Release);
                    break;
                case 2:
                case 3:
                    edTrip.Text = String.Format("{0:#0.0}", (float)Param.Fault / 100);
                    edWarning.Text = String.Format("{0:#0.0}", (float)Param.Warning / 100);
                    edRelease.Text = String.Format("{0:#0.0}", (float)Param.Release / 100);
                    break;
                case 4:
                case 5:
                case 6:
                case 7:
                case 10:
                case 11:
                    edTrip.Text = String.Format("{0}", Param.Fault);
                    edWarning.Text = String.Format("{0}", Param.Warning);
                    edRelease.Text = String.Format("{0}", Param.Release);
                    break;
                case 8:
                case 9:
                    edTrip.Text = String.Format("{0:#0.0}", (float)Param.Fault / 10);
                    edWarning.Text = String.Format("{0:#0.0}", (float)Param.Warning / 10);
                    edRelease.Text = String.Format("{0:#0.0}", (float)Param.Release / 10);
                    break;
            }
        }

        private void UpdateNewParam()
        {
            if (wFlag == false)
            {
                ParamData Param = new ParamData();
                Param = rParam;

                switch (id)
                {
                    case 0:
                    case 1:
                        edTripNew.Text = String.Format("{0}", Param.Fault);
                        edWarningNew.Text = String.Format("{0}", Param.Warning);
                        edReleaseNew.Text = String.Format("{0}", Param.Release);
                        break;
                    case 2:
                    case 3:
                        edTripNew.Text = String.Format("{0:#0.0}", (float)Param.Fault / 100);
                        edWarningNew.Text = String.Format("{0:#0.0}", (float)Param.Warning / 100);
                        edReleaseNew.Text = String.Format("{0:#0.0}", (float)Param.Release / 100);
                        break;
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 10:
                    case 11:
                        edTripNew.Text = String.Format("{0}", Param.Fault);
                        edWarningNew.Text = String.Format("{0}", Param.Warning);
                        edReleaseNew.Text = String.Format("{0}", Param.Release);
                        break;
                    case 8:
                    case 9:
                        edTripNew.Text = String.Format("{0:#0.0}", (float)Param.Fault / 10);
                        edWarningNew.Text = String.Format("{0:#0.0}", (float)Param.Warning / 10);
                        edReleaseNew.Text = String.Format("{0:#0.0}", (float)Param.Release / 10);
                        break;
                }
                wFlag = true;
            }
        }

        #endregion

        private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            csUtils.TypingOnlyNumber(sender, e, true, true);
        }
    }
}
