using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LFP_Manager.DataStructure;
using LFP_Manager.Utils;

namespace LFP_Manager.Function
{
    class csMakeDataFunction
    {
        private static DeviceSystemData MakeAlarmTripData(int id, short ndata, DeviceSystemData rSystemData)
        {
            bool[] aData = csUtils.Int16ToBitArray(ndata);
            short bFault = rSystemData.StatusData.protect;
            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.protect = bFault;
            rSystemData.StatusData.warning = bWarning;

            return rSystemData;
        }
    }
}
