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 DevExpress.XtraEditors;
using LFP_Manager.DataStructure;
using static LFP_Manager.DataStructure.csDbConstData;

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

        private int mID = 0;
        private CommConfig Config;
        private DeviceSystemData SystemData;
        private DeviceParamData ParamData;
        private DeviceCalibration CalibData;

        public event CommandEvent OnCommand = null;
        
        #endregion

        #region CONSTRUCTORS
        public ucModuleMainB()
        {
            InitializeComponent();
        }

        public ucModuleMainB(CommConfig aConfig, int aID, ref DeviceSystemData mSystemData)
        {
            InitializeComponent();

            Config = aConfig;
            mID = aID;
            SystemData = mSystemData;

            lbMdNo.Text = String.Format("#{0:00}", mID);

            tmrDisplay.Start();
        }
        #endregion

        #region PUBLIC FUNCTION

        public void UpdateData(ref DeviceSystemData mSystemData)
        {
            SystemData = mSystemData;
        }

        #endregion

        #region TIMER EVENT
        private void tmrDisplay_Tick(object sender, EventArgs e)
        {
            DisplayValue();
            DisplayStatusAndAlarm();
        }

        #endregion

        #region BUTTON EVENT
        private void btnModuleDetail_Click(object sender, EventArgs e)
        {
            OnCommand?.Invoke(mID, 2, 0, 1, ref ParamData, ref CalibData);
        }
        #endregion

        #region DISPLAY DATA

        private void DisplayMainStatus()
        {
            if (SystemData.CommFail == false)
            {

            }
            else
            {
                lbMdNo.BackColor = Color.Orange;
            }

        }

        private void DisplayValue()
        {
            lbMdFwVer.Text = SystemData.Information.SwProductRev;
            gaugeVoltage.Text = String.Format("{0:0.0}", (double)SystemData.ValueData.voltageOfPack / 10);
            gaugeCurrent.Text = String.Format("{0:0.0}", (double)SystemData.ValueData.current / 10);
            gaugeSOC.Text = String.Format("{0:0.0}", (double)SystemData.ValueData.rSOC / 10);
            gaugeSOH.Text = String.Format("{0:0.0}", (double)SystemData.ValueData.stateOfHealth / 10);
            gaugeTempMax.Text = String.Format("{0:0.0}", (double)SystemData.AvgData.maxTemp / 10);
            gaugeGapV.Text = String.Format("{0:0.000}", (double)SystemData.AvgData.diffCellVoltage / 1000);
        }

        private void DisplayStatusAndAlarm()
        {
            if (SystemData.CommFail == false)
            {
                if (SystemData.ShelfCommFail == false)
                {
                    switch (SystemData.StatusData.status)
                    {
                        case 0x0000:                // Stand by
                            slbOpStatus.Text = "STANDBY";
                            slbOpStatus.AppearanceItemCaption.ForeColor = System.Drawing.Color.Black;
                            break;
                        case 0x0001:                // Charging
                            slbOpStatus.Text = "CHARGING";
                            slbOpStatus.AppearanceItemCaption.ForeColor = System.Drawing.Color.Blue;
                            break;
                        case 0x0002:                // Discharging
                            slbOpStatus.Text = "DISCHARGING";
                            slbOpStatus.AppearanceItemCaption.ForeColor = System.Drawing.Color.Magenta;
                            break;
                        case 0x0003:                // Float Charging
                            slbOpStatus.Text = "FLOATING";
                            slbOpStatus.AppearanceItemCaption.ForeColor = System.Drawing.Color.Black;
                            break;
                        case 0x0004:                // Warming Up
                            slbOpStatus.Text = "WARMING UP";
                            slbOpStatus.AppearanceItemCaption.ForeColor = System.Drawing.Color.Black;
                            break;
                        default:
                            slbOpStatus.Text = "UNKNOWN";
                            slbOpStatus.AppearanceItemCaption.ForeColor = System.Drawing.Color.Black;
                            break;
                    }
                    // Alarm Display
                    switch (SystemData.StatusData.batteryStatus)
                    {
                        case 0:         // NORMAL
                            slbAlarm.Text = "NORMAL";
                            slbAlarm.AppearanceItemCaption.ForeColor = System.Drawing.Color.Green;
                            break;
                        case 1:         // WARNING
                            slbAlarm.Text = "WARNING";
                            slbAlarm.AppearanceItemCaption.ForeColor = System.Drawing.Color.Yellow;
                            break;
                        case 2:         // FAULT
                            slbAlarm.Text = "FAULT";
                            slbAlarm.AppearanceItemCaption.ForeColor = System.Drawing.Color.Red;
                            break;
                        case 3:         // WARMING UP
                            slbAlarm.Text = "WARMING UP";
                            slbAlarm.AppearanceItemCaption.ForeColor = System.Drawing.Color.Black;
                            break;
                        case 4:         // FAULT (Anti-Theft Comm.)
                            slbAlarm.Text = "FAULT1";
                            slbAlarm.AppearanceItemCaption.ForeColor = System.Drawing.Color.Red;
                            break;
                        case 5:         // FAULT (Anti-Theft Gyro-Scope)
                            slbAlarm.Text = "FAULT2";
                            slbAlarm.AppearanceItemCaption.ForeColor = System.Drawing.Color.Red;
                            break;
                        default:
                            slbAlarm.Text = string.Format("UNKNOWN ({0})", SystemData.StatusData.batteryStatus);
                            slbAlarm.AppearanceItemCaption.ForeColor = System.Drawing.Color.Red;
                            break;
                    }
                }
                else
                {
                    slbOpStatus.Text = "COMM FAIL";
                    slbOpStatus.AppearanceItemCaption.ForeColor = System.Drawing.Color.Red;
                    slbAlarm.Text = "COMM FAIL";
                    slbAlarm.AppearanceItemCaption.ForeColor = System.Drawing.Color.Red;
                    lbMdNo.BackColor = System.Drawing.Color.Orange;
                }
            }
            else
            {
                slbOpStatus.Text = "OFF LINE";
                slbOpStatus.AppearanceItemCaption.ForeColor = System.Drawing.Color.Red;
                slbAlarm.Text = "OFF LINE";
                slbAlarm.AppearanceItemCaption.ForeColor = System.Drawing.Color.Red;
                lbMdNo.BackColor = System.Drawing.Color.Orange;
            }

            // Charge Relay Status Display
        }

        #endregion
    }
}
