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;

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

        private int mID = 0;
        private CommConfig Config;
        private CsDeviceData.DeviceModuleData ModuleData;
        private CsDeviceData.DeviceModuleData.DeviceParamData ParamData;
        private CsDeviceData.DeviceModuleData.DeviceCalibration CalibData;

        public event CommandEvent OnCommand = null;
        
        #endregion

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

        public ucModuleMainB(CommConfig aConfig, int aID, ref CsDeviceData.DeviceModuleData mData)
        {
            InitializeComponent();

            Config = aConfig;
            mID = aID;
            ModuleData = mData;

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

            tmrDisplay.Start();
        }
        #endregion

        #region PUBLIC FUNCTION

        public void UpdateData(ref CsDeviceData.DeviceModuleData mData)
        {
            ModuleData = mData;
        }

        #endregion

        #region TIMER EVENT
        private void tmrDisplay_Tick(object sender, EventArgs e)
        {
            DisplayValue();
            DisplayMainStatus();
            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 (ModuleData.ShelfCommFail == false)
            {
                lbMdNo.BackColor = Color.Green;
            }
            else
            {
                lbMdNo.BackColor = Color.Orange;
            }
        }

        private void DisplayValue()
        {
            string sn = ModuleData.Information.HwSerialNumber;
            if (sn.Length > 9)
                sn = sn.Substring(sn.Length - 9, 9);

            LbMdSN.Text = sn;
            gaugeVoltage.Text = String.Format("{0:0.0}", (double)ModuleData.ValueData.voltage / 10);
            gaugeCurrent.Text = String.Format("{0:0.0}", (double)ModuleData.ValueData.current / 10);
            gaugeSOC.Text = String.Format("{0:0.0}", (double)ModuleData.ValueData.SOC / 10);
            gaugeSOH.Text = String.Format("{0:0.0}", (double)ModuleData.ValueData.SOH / 10);
            gaugeTempMax.Text = String.Format("{0:0.0}", (double)ModuleData.AvgData.maxTemp / 10);
            gaugeGapV.Text = String.Format("{0:0.000}", (double)ModuleData.AvgData.diffCellVoltage / 1000);
        }

        private void DisplayStatusAndAlarm()
        {
            if (ModuleData.ShelfCommFail == false)
            {
                switch (ModuleData.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 (ModuleData.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.Orange;
                        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})", ModuleData.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;
            }
        }

        #endregion
    }
}
