using LFP_Manager.DataStructure;
using LFP_Manager.Utils;
using System;

namespace LFP_Manager.Function
{
    class csMakeDataFunction124050
    {
        private static DeviceSystemData MakeAlarmTripData(int id, short ndata, DeviceSystemData rSystemData)
        {
            bool[] aData = csUtils.Int16ToBitArray(ndata);
            short bFault = rSystemData.StatusData.protection;
            short bWarning = rSystemData.StatusData.warning;
            int i = 0;

            switch (id)
            {
                case 0:         // Warning
                    if (aData[i++]) bWarning |= (1 << 2);      // Bit 0 : Over voltage warning 
                    else bWarning &= ~(1 << 2);
                    if (aData[i++]) bWarning |= (1 << 4);      // Bit 1 : Cell Over voltage warning 
                    else bWarning &= ~(1 << 4);
                    if (aData[i++]) bWarning |= (1 << 3);      // Bit 2 : Low voltage warning
                    else bWarning &= ~(1 << 3);
                    if (aData[i++]) bWarning |= (1 << 5);      // Bit 3 : Low cell voltage warning
                    else bWarning &= ~(1 << 5);
                    if (aData[i++]) bWarning |= (1 << 6);      // Bit 4 : Charge over current warning
                    else bWarning &= ~(1 << 6);
                    if (aData[i++]) bWarning |= (1 << 7);      // Bit 5 : Discharge over current warning
                    else bWarning &= ~(1 << 7);
                    i++;                                        // 06  Abnormal balancing current
                    i++;                                        // 07  Reserved
                    if ((aData[8]) || (aData[9])) bWarning |= (1 << 0);      // Bit 8 : Charge over temperature warning, Bit 9 : Discharge over temperature warning
                    else bWarning &= ~(1 << 0);
                    i++;                                        // 08   
                    i++;                                        // 09   
                    if ((aData[10]) || (aData[11])) bWarning |= (1 << 1);      // Bit 10 : Charge low temperature warning, Bit 11 : Discharge low temperature warning
                    else bWarning &= ~(1 << 1);
                    i++;                                        // 10   
                    i++;                                        // 11   
                    if (aData[i++]) bWarning |= (1 << 11);       // 12 : Low capacity warning
                    else bWarning &= ~(1 << 11);
                    i++;                                        // 13
                    if (aData[i++]) bWarning |= (1 << 9);       // 14  Cell Voltage Difference Warning
                    else bWarning &= ~(1 << 9);
                    break;
                case 1:         // Status Code 2
                    if (aData[i++]) bFault |= (1 << 2);      // Bit 0 : Over voltage warning 
                    else bFault &= ~(1 << 2);
                    if (aData[i++]) bFault |= (1 << 4);      // Bit 1 : Cell Over voltage warning 
                    else bFault &= ~(1 << 4);
                    if (aData[i++]) bFault |= (1 << 3);      // Bit 2 : Low voltage warning
                    else bFault &= ~(1 << 3);
                    if (aData[i++]) bFault |= (1 << 5);      // Bit 3 : Low cell voltage warning
                    else bFault &= ~(1 << 5);
                    if (aData[i++]) bFault |= (1 << 6);      // Bit 4 : Charge over current warning
                    else bFault &= ~(1 << 6);
                    if (aData[i++]) bFault |= (1 << 7);      // Bit 5 : Discharge over current warning
                    else bFault &= ~(1 << 7);
                    i++;                                        // 06  Abnormal balancing current
                    i++;                                        // 07  Reserved
                    if ((aData[8]) || (aData[9])) bFault |= (1 << 0);      // Bit 8 : Charge over temperature warning, Bit 9 : Discharge over temperature warning
                    else bFault &= ~(1 << 0);
                    i++;                                        // 08   
                    i++;                                        // 09   
                    if ((aData[10]) || (aData[11])) bFault |= (1 << 1);      // Bit 10 : Charge low temperature warning, Bit 11 : Discharge low temperature warning
                    else bFault &= ~(1 << 1);
                    i++;                                        // 10   
                    i++;                                        // 11   
                    i++;                                        // 12   
                    i++;                                        // 13   
                    if (aData[i++]) bFault |= (1 << 9);      // 14  Cell Voltage Difference Warning
                    else bFault &= ~(1 << 9);
                    break;
            }

            rSystemData.StatusData.protection = bFault;
            rSystemData.StatusData.warning = bWarning;

            return rSystemData;
        }

        public static void SetSnmpData(int index, object sdata, ref DeviceSystemData aSystemData)
        {
            switch (index)
            {
                case 1: aSystemData.ValueData.voltageOfPack = (short)(Convert.ToDouble(sdata) / 10); break;           // voltageOfPack - 0.01V
                case 2: aSystemData.ValueData.current = (short)(Convert.ToDouble(sdata) - 10000); break;              // current       - 0.1A, Offset: 10000
                case 3: // Cell Voltage #1
                case 4: // Cell Voltage #2
                case 5: // Cell Voltage #3
                case 6: // Cell Voltage #4
                case 7: // Cell Voltage #5
                case 8: // Cell Voltage #6
                case 9: // Cell Voltage #7
                case 10: // Cell Voltage #8
                case 11: // Cell Voltage #9
                case 12: // Cell Voltage #10
                case 13: // Cell Voltage #11
                case 14: // Cell Voltage #12
                case 15: // Cell Voltage #13
                case 16: // Cell Voltage #14
                case 17: // Cell Voltage #15
                case 18: // Cell Voltage #16
                case 19: // Cell Voltage #17
                case 20: // Cell Voltage #18
                case 21: // Cell Voltage #19
                case 22: // Cell Voltage #20
                case 23: // Cell Voltage #21
                case 24: // Cell Voltage #22
                case 25: // Cell Voltage #23
                case 26: // Cell Voltage #24
                case 27: // Cell Voltage #25
                case 28: // Cell Voltage #26
                case 29: // Cell Voltage #27
                case 30: // Cell Voltage #28
                case 31: // Cell Voltage #29
                case 32: // Cell Voltage #30
                case 33: // Cell Voltage #31
                case 34: // Cell Voltage #32
                case 35: // Cell Voltage #33
                case 36: // Cell Voltage #34
                case 37: // Cell Voltage #35
                case 38: // Cell Voltage #36
                case 39: // Cell Voltage #37
                case 40: // Cell Voltage #38
                case 41: // Cell Voltage #39
                    aSystemData.ValueData.CellVoltage[index - 3] = (ushort)(Convert.ToDouble(sdata) / 1);
                    csUtils.MakeMaxAvgMinCellVoltage(ref aSystemData, 39);
                    break;          // Cell Voltage #16

                case 43: aSystemData.ValueData.Ext1Temperature = (short)(Convert.ToDouble(sdata) * 10); break;         // Temp of PCB - C
                case 44: aSystemData.ValueData.Ext2Temperature = (short)(Convert.ToDouble(sdata) * 10); break;         // Temp of Ambient - C
                case 45: aSystemData.AvgData.maxTemp = (short)(Convert.ToDouble(sdata) * 10); break;                  // Max. Temp

                case 46: aSystemData.ValueData.remainingCapacity = (short)(Convert.ToDouble(sdata) * 1); break;                  // Remaining Capacity

                case 47: aSystemData.ValueData.stateOfHealth = (short)(Convert.ToDouble(sdata) / 10); break;          // stateOfHealth
                case 48: aSystemData.ValueData.rSOC = (short)(Convert.ToDouble(sdata) / 10); break;                   // Relatvie State Of Charge

                case 49: aSystemData.StatusData.status = (short)(Convert.ToDouble(sdata) / 1); break;                 // Status
                case 50: aSystemData.StatusData.warning = MakeSnmpWarningData((short)Convert.ToDouble(sdata)); break;       // warning
                case 51: aSystemData.StatusData.protection = MakeSnmpTripData((short)Convert.ToDouble(sdata)); break;       // Protection
                case 52:               // FaultAndStatus
                    aSystemData.StatusData.faultAndStatus = (short)(Convert.ToDouble(sdata) / 1);
                    //aSystemData.StatusData.status = (short)(((short)Convert.ToDouble(sdata) >> 8) & 0x0003);
                    MakeAlarm(ref aSystemData);
                    break;
                case 53: aSystemData.ValueData.cycleCount = (short)(Convert.ToDouble(sdata) / 1); break;              // cycleCount

                case 54: // Temperature #1
                case 55: // Temperature #2
                case 56: // Temperature #3
                case 57: // Temperature #4
                case 58: // Temperature #5
                case 59: // Temperature #6
                case 60: // Temperature #7
                case 61: // Temperature #8
                    aSystemData.ValueData.CellTemperature[index - 54] = (short)(Convert.ToDouble(sdata) * 10);
                    csUtils.MakeMaxAvgMinTemperature(ref aSystemData, 8);
                    break;       // Temperature #12

                case 68: aSystemData.cellQty = (short)(Convert.ToDouble(sdata) * 1); break;       // Cell Number
                case 69: aSystemData.ValueData.designedCapacity = (short)(Convert.ToDouble(sdata) * 1); break;       // Cell Number
                case 70: break;     // Average Temperature
                case 71: aSystemData.ValueData.MaxBattChargeCurr = (short)(Convert.ToDouble(sdata) * 1); break;                  // Max. Charge Current - A
                case 72: aSystemData.ntcQty = (short)(Convert.ToDouble(sdata) * 1); break;       // NTC Number

                case 74: aSystemData.BmsDateTime.year = (short)(Convert.ToDouble(sdata) * 1); break;                  // BMS DateTime - Year
                case 75: aSystemData.BmsDateTime.month = (short)(Convert.ToDouble(sdata) * 1); break;                 // BMS DateTime - Month
                case 76: aSystemData.BmsDateTime.day = (short)(Convert.ToDouble(sdata) * 1); break;                   // BMS DateTime - Day
                case 77: aSystemData.BmsDateTime.hour = (short)(Convert.ToDouble(sdata) * 1); break;                  // BMS DateTime - Hour
                case 78: aSystemData.BmsDateTime.minute = (short)(Convert.ToDouble(sdata) * 1); break;                // BMS DateTime - Minute
                case 79:
                    aSystemData.BmsDateTime.second = (short)(Convert.ToDouble(sdata) * 1);                       // BMS DateTime - Second
                    aSystemData.BmsDateTime.DateTimeStr = string.Format("{0:0000}-{1:00}-{2:00} {3:00}:{4:00}:{5:00}"
                        , aSystemData.BmsDateTime.year
                        , aSystemData.BmsDateTime.month
                        , aSystemData.BmsDateTime.day
                        , aSystemData.BmsDateTime.hour
                        , aSystemData.BmsDateTime.minute
                        , aSystemData.BmsDateTime.second
                        );
                    break;

                case 81: aSystemData.FloatVoltage = (int)Convert.ToDouble(sdata); break;                              // Float Voltage - 0.01V
                case 82: aSystemData.BoostVoltage = (int)Convert.ToDouble(sdata); break;                              // Boost Voltage - 0.01V

                case 83: aSystemData.MinChargeTemp = (int)Convert.ToDouble(sdata); break;                             // Min. Charge Temp - C
                case 84: aSystemData.MaxChargeTemp = (int)Convert.ToDouble(sdata); break;                             // Max. Charge Temp - C

                case 90: aSystemData.Information.ModelName = (string)sdata; break;                                      // Model - string
                case 91: aSystemData.Information.SwProductRev = (string)sdata; break;                                   // BMS Fw Version - string
                case 92: aSystemData.Information.HwSerialNumber = (string)sdata; break;                                 // Pack Serial Number - string
                case 93: aSystemData.Information.VendorName = (string)sdata; break;                                     // Vendor Name - string
                case 94: aSystemData.Information.ProductCode = (string)sdata; break;                                    // Product Code - string
                case 95: aSystemData.Information.MajorMinorRev = (string)sdata; break;                                  // Major Minor Revision - string
                case 96: aSystemData.Information.HwProductRev = (string)sdata; break;                                   // Hardware Product Revision - string
                case 97: aSystemData.Information.ManufacturingDate = (string)sdata; break;                              // Manufacturing Date - string

                default:
                    break;
            }
        }
        public static void MakeAlarm(ref DeviceSystemData rSystemData)
        {
            if (((rSystemData.StatusData.faultAndStatus >> 14) & 0x0001) == 0x0001)
            {
                rSystemData.StatusData.batteryStatus = 4;       // Anti-theft Gyroscope
            }
            else if (((rSystemData.StatusData.faultAndStatus >> 15) & 0x0001) == 0x0001)
            {
                rSystemData.StatusData.batteryStatus = 5;       // Anti-theft Comm.
            }
            else if (rSystemData.StatusData.protection != 0x0000)
            {
                rSystemData.StatusData.batteryStatus = 2;
            }
            else if (rSystemData.StatusData.warning != 0x0000)
            {
                rSystemData.StatusData.batteryStatus = 1;
            }
            else
            {
                rSystemData.StatusData.batteryStatus = 0;
            }
        }
        public static short MakeSnmpTripData(short rdata)
        {
            short result = 0;
            bool[] bAlarm = csUtils.Int16ToBitArray(rdata);

            if (bAlarm[0] == true) result |= (short)(1 << 4);       // 0x0001：Cell OV
            if (bAlarm[1] == true) result |= (short)(1 << 2);       // 0x0002：Pack OV
            if (bAlarm[2] == true) result |= (short)(1 << 5);       // 0x0004：Cell UV
            if (bAlarm[3] == true) result |= (short)(1 << 3);       // 0x0008：Pack UV

            if (bAlarm[4] == true) result |= (short)(1 << 6);       // 0x0010：Charging OC1
            if (bAlarm[5] == true) result |= (short)(1 << 6);       // 0x0020：Charging OC2
            if (bAlarm[6] == true) result |= (short)(1 << 7);       // 0x0010：Discharging OC1
            if (bAlarm[7] == true) result |= (short)(1 << 7);       // 0x0020：Discharging OC2

            if (bAlarm[8] == true) result |= (short)(1 << 9);       // 0x0200：Short Circuit Protection

            if (bAlarm[9] == true) result |= (short)(1 << 0);       // 0x0080: Charging Over Tempratuer
            if (bAlarm[10] == true) result |= (short)(1 << 1);       // 0x0080: Charging Under Tempratuer
            if (bAlarm[11] == true) result |= (short)(1 << 0);      // 0x0040: Discharging Over Tempratuer
            if (bAlarm[12] == true) result |= (short)(1 << 1);      // 0x0040: Discharging Under Tempratuer

            return result;
        }
        public static short MakeSnmpWarningData(short rdata)
        {
            short result = 0;
            bool[] bAlarm = csUtils.Int16ToBitArray(rdata);

            if (bAlarm[0] == true) result |= (short)(1 << 4);       // 0x0001：Cell OV
            if (bAlarm[1] == true) result |= (short)(1 << 5);       // 0x0002：Cell UV
            if (bAlarm[2] == true) result |= (short)(1 << 2);       // 0x0004：Pack OV
            if (bAlarm[3] == true) result |= (short)(1 << 3);       // 0x0008：Pack UV

            if (bAlarm[4] == true) result |= (short)(1 << 6);       // 0x0010：Charging OC
            if (bAlarm[5] == true) result |= (short)(1 << 7);       // 0x0020：Discharging OC

            if (bAlarm[6] == true) result |= (short)(1 << 8);       // 0x0040：ENV OT
            if (bAlarm[7] == true) result |= (short)(1 << 9);       // 0x0080：ENV UT

            if (bAlarm[8] == true) result |= (short)(1 << 0);       // 0x0100: Charging Over Temprature
            if (bAlarm[9] == true) result |= (short)(1 << 1);       // 0x0200: Charging Under Temprature
            if (bAlarm[10] == true) result |= (short)(1 << 0);      // 0x0400: Discharging Over Temprature
            if (bAlarm[11] == true) result |= (short)(1 << 1);      // 0x0800: Discharging Under Temprature

            if (bAlarm[12] == true) result |= (short)(1 << 11);       // 0x1000：SOC Low

            if (bAlarm[13] == true) result |= (short)(1 << 13);       // 0x2000：Diff Volt
            if (bAlarm[14] == true) result |= (short)(1 << 14);       // 0x4000：CB Off

            return result;
        }
    }
}
