#ifndef VISUALIZATIONPAGE_H
#define VISUALIZATIONPAGE_H

#include <QWidget>
#include <QButtonGroup>
#include <QListWidgetItem>

#include "visualizationchart.h"
#include "bmsinterface.h"

namespace Ui {
class VisualizationPage;
}

#define TURN_ON_DUMMY_DATA_FLOW_TO_GRAPHS 0

class VisualizationPage : public QFrame
{
    Q_OBJECT

public:
    enum TabWidgetPage
    {
        Voltage = 0,
        Apmerage,
        //Temperature,
        TemperatureBat,
        TemperatureBms,
        Cells,
        CellsAll,
    } m_currentPageIndex;

    #define ChartsIndexInt int

    enum ChartsIndex
    {
        VoltageMax = 0,
        VoltageMid,
        VoltageMin,
        AmperageMax,
        AmperageMid,
        AmperageMin,
        //TemperatureMax,
        //TemperatureMid,
        //TemperatureMin,
        TemperatureBatMax,
        TemperatureBatMid,
        TemperatureBatMin,
        TemperatureBmsMax,
        TemperatureBmsMid,
        TemperatureBmsMin,
        CellsMax,
        CellsMid,
        CellsMin,
        //CellsAll,

        ChartsIndexNumber
    };

    enum ChartsForCellsIndex
    {
        //CellQtwhite = 0					    , // EFDBC5	239,219,197
        CellQtblack  =0                         ,
        CellQtdarkGray                      ,
        CellQtgray                          ,
        CellQtlightGray                     ,
        CellQtred                           ,
        CellQtgreen                         ,
        CellQtblue                          ,
        CellQtcyan                          ,
        CellQtmagenta                       ,
        CellQtyellow                        ,
        CellQtdarkRed                       ,
        CellQtdarkGreen                     ,
        CellQtdarkBlue                      ,
        CellQtdarkCyan                      ,
        CellQtdarkMagenta                   ,
        CellQtdarkYellow                    ,
        CellAlmond 					    , // EFDBC5	239,219,197
        CellAntiqueBrass				, // CD9575	205,149,117
        CellApricot						, // FDD9B5	253,217,181
        CellAquamarine					, // 78DBE2	120,219,226
        CellAsparagus					, // 87A96B	135,169,107
        CellAtomicTangerine             , // FFA474	255,164,116
        CellBananaMania                 , // FAE7B5	250,231,181
        CellBeaver						, // 9F8170	159,129,112
        CellBittersweet					, // FD7C6E	253,124,110
        CellBlack						, // 232323	35,35,35
        CellBlue						, // 1F75FE	31,117,254
        CellBlueBell					, // ADADD6	173,173,214
        CellBlueGreen					, // 199EBD	25,158,189
        CellBlueViolet					, // 2E5090	115,102,189
        CellBluetiful					, // 7366BD	46,80,144
        CellBlush						, // DE5D83	222,93,131
        CellBrickRed					, // CB4154	203,65,84
        CellBrown						, // B5674D	180,103,77
        CellBurntOrange                 , // FF7F49	255,127,73
        CellBurntSienna                 , // EA7E5D	234,126,93
        CellCadetBlue					, // B0B7C6	176,183,198
        CellCanary						, // FFFF99	255,255,159
        CellCaribbeanGreen				, // 1CD3A2	28,211,162
        CellCarnationPink				, // FFAACC	255,170,204
        CellCerise						, // FF43A4	221,68,146
        CellCerulean					, // 1DACD6	29,172,214
        CellChestnut					, // BC5D58	188,93,88
        CellCopper						, // DD9475	221,148,117
        CellCornflower					, // 9ACEEB	154,206,235
        CellCottonCandy                 , // FFBCD9	255,188,217
        CellDenim						, // 2B6CC4	43,108,196
        CellDesertSand					, // EFCDB8	239,205,184
        CellEggplant					, // DD4492	110,81,96
        CellElectricLime				, // 1DF914	29,249,20
        CellFern						, // 71BC78	113,188,120
        CellForestGreen                 , // 6DAE81	109,174,129
        CellFuchsia						, // C364C5	195,100,197
        CellFuzzyWuzzy					, // CC6666	204,102,102
        CellGold						, // E7C697	231,198,151
        CellGoldenrod					, // FCD975	255,217,117
        CellGrannySmithApple			, // A8E4A0	168,228,160
        CellGray						, // 95918C	149,145,140
        CellGreen						, // 1CAC78	28,172,120
        CellGreenYellow                 , // F0E891	240,232,145
        CellHotMagenta					, // FF1DCE	255,29,206
        CellInchworm					, // B2EC5D	178,236,93
        CellIndigo						, // 5D76CB	93,118,203
        CellJazzberryJam				, // CA3767	202,55,103
        CellJungleGreen                 , // 3BB08F	59,176,143
        CellLaserLemon					, // FDFC74	253,252,116
        CellLavender					, // FCB4D5	252,180,213
        CellMacaroniAndCheese			, // FFBD88	255,189,136
        CellMagenta						, // FCB4D5	246,100,175
        CellMahogany					, // CD4A4A	205,74,74
        CellManatee						, // 979AAA	151,154,170
        CellMangoTango					, // FF8243	255,130,67
        CellMaroon						, // C8385A	200,56,90
        CellMauvelous					, // EF98AA	239,152,170
        CellMelon						, // FDBCB4	253,188,180
        CellMidnightBlue				, // 1A4876	26,72,118
        CellMountainMeadow				, // 30BA8F	48,186,143
        CellNavyBlue					, // 1974D2	25,116,210
        CellNeonCarrot					, // FFA343	255,163,67
        CellOliveGreen					, // BAB86C	186,184,108
        CellOrange						, // FF7538	255,117,56
        CellOrchid						, // C0448F	230,168,215
        CellOuterSpace					, // 414AAC	65,74,76
        CellOutrageousOrange			, // FF6E4A	255,110,74
        CellPacificBlue                 , // 1CA9C9	28,169,201
        CellPeach						, // FFCFAB	255,207,171
        CellPeriwinkle					, // C5D0E6	197,208,230
        CellPiggyPink					, // FDD7E4	253,215,228
        CellPineGreen					, // 158078	21,128,120
        CellPinkFlamingo				, // FC74FD	252,116,253
        CellPinkSherbet                 , // F780A1	247,128,161
        CellPlum						, // 8E4584	142,69,133
        CellPurpleHeart                 , // 7442C8	116,66,200
        CellPurpleMountainsMajesty      , // 9D81BA	157,129,186
        CellPurplePizza                 , // FF1DCE	255,29,206
        CellRadicalRed					, // FF496C	255,73,107
        CellRawSienna                   , // D68A59	214,138,89
        CellRazzleDazzleRose			, // E6A8D7	255,72,208
        CellRazzmatazz					, // E3256B	227,37,107
        CellRed							, // EE204D	238,32,77
        CellRedOrange					, // FF5349	255,83,73
        CellRedViolet					, // c0448f	192,68,143
        CellRobinsEggBlue               , // 1FCECB	31,206,203
        CellRoyalPurple                 , // 7851A9	120,81,169
        CellSalmon						, // FF9BAA	255,155,170
        CellScarlet						, // FC2847	242,40,71
        CellScreaminGreen				, // 76FF7A	118,255,122
        CellSeaGreen					, // 9FE2BF	159,226,191
        CellSepia						, // A5694F	165,105,79
        CellShadow						, // 8A795D	138,121,93
        CellShamrock					, // 45CEA2	69,206,162
        CellShockingPink				, // FB7EFD	251,126,253
        CellSilver						, // CDC5C2	205,197,194
        CellSkyBlue                     , // 80DAEB	128,218,235
        CellSpringGreen                 , // ECEABE	236,234,190
        CellSunglow						, // FFCF48	255,207,72
        CellSunsetOrange				, // FD5E53	253,94,83
        CellTan							, // FAA76C	250,167,108
        CellTickleMePink				, // FC89AC	252,137,172
        CellTimberwolf					, // DBD7D2	219,215,210
        CellTropicalRainForest          , // 17806D	23,128,109
        CellTumbleweed					, // DEAA88	222,170,136
        CellTurquoiseBlue				, // 77DDE7	119,221,231
        CellUnmellowYellow				, // FDFC74	253,252,116
        CellVioletPurple				, // 926EAE	146,110,174
        CellVioletRed					, // F75394	247,83,148
        CellVividTangerine				, // FFA089	255,160,137
        CellVividViolet                 , // 8F509D	143,80,157
        CellWhite						, // EDEDED	237,237,237
        CellWildBlueYonder              , // A2ADD0	162,173,208
        CellWildStrawberry				, // F664AF	255,67,164
        CellWildWatermelon				, // FC6C85	252,108,133
        CellWisteria					, // CDA4DE	205,164,222
        CellYellow						, // FCE883	252,232,131
        CellYellowGreen                 , // C5E384	197,227,132
        CellYellowOrange				, //FFB653	255,182,83

        CellColorsNumber				, //120
    };

    void setIsPageActivated(bool isActivated);
    bool isPageActivated();

    BMSInterface *bms() const;
    void setDieBieMS(BMSInterface *dieBieMS);

    explicit VisualizationPage(QWidget *parent = nullptr);
    ~VisualizationPage();

public slots:
    void onVisualizationPageSelected();

private slots:
    void onTabBarButtonClicked();

    void onSetValA();
    void onSetValV();
    void onSetValT();

    void onSetSecsV(int secs);
    void onSetSecsA(int secs);
    void onSetSecsT(int secs);
    void onSetSecsC(int secs);
    //void onSetSecsCA(int secs);

private slots:
    void timerSlot();
    void valuesReceived(BMS_VALUES values);
    void cellsReceived(int cellCount, QVector<double> cellVoltageArray);
    void auxReceived(int auxCount, QVector<double> auxVoltageArray);
    void expTempReceived(int expTempCount, QVector<double> expTempVoltageArray);

    void onChecked(QListWidgetItem *item);

    void on_checkBox_stateChanged(int arg1);

private:
    Ui::VisualizationPage *ui;

    BMSInterface *mDieBieMS;

    bool mIsThisPageActivated;

    QButtonGroup* m_btnGroup;

    VisualizationChart* chartV;
    VisualizationChart* chartA;
    VisualizationChart* chartT;
    VisualizationChart* chartTB;
    VisualizationChart* chartC;
    VisualizationChart* chartCA;

    void initColorsForChartCellsAll();

    //
/*
    QVector<double> mPackVoltage;

    QVector<double> mHCLoadVoltage;
    QVector<double> mLCLoadVoltage;

    QVector<double> mHCLoadCurrent;
    //QVector<double> mMCLoadCurrent_;
    QVector<double> mLCLoadCurrent;

    QVector<double> mAuxVoltage;
    QVector<double> mAuxCurrent;

    QVector<double> mCellVHigh;
    QVector<double> mCellVAverage;
    QVector<double> mCellVLow;

    QVector<double> mTempBMSHigh;
    QVector<double> mTempBMSAverage;
    //QVector<double> mTempBMSLow_;

    QVector<double> mTempBattHigh;
    QVector<double> mTempBattAverage;
    //QVector<double> mTempBattLow_;


    QVector<double> mSeconds;
*/

    QVector<double> mPackVoltage;
    QVector<double> mPackCurrent;

    QVector<double> mLCLoadVoltage;
    QVector<double> mLCLoadCurrent;

    QVector<double> mHCLoadVoltage;
    QVector<double> mHCLoadCurrent;

    QVector<double> mChargerVoltage;

    QVector<double> mAuxVoltage;
    QVector<double> mAuxCurrent;

    QVector<double> mCellVHigh;
    QVector<double> mCellVAverage;
    QVector<double> mCellVLow;

    QVector<double> mTempBMSHigh;
    QVector<double> mTempBMSAverage;
    QVector<double> mTempBMSLow;

    QVector<double> mTempBattHigh;
    QVector<double> mTempBattAverage;
    QVector<double> mTempBattLow;

    QVector<double> mHumidity;

    QVector<double> mCellsVals;

    QVector<double> mSeconds;

    double mSecondCounter;
    qint64 mLastUpdateTime;
    bool mUpdateValPlot;

    QTimer* mTimer;

    static int mOldCellsNumber;

    void initListWidgetOfCells(int cells);

    void initForPlot();

    void initForCharts();

    void appendDoubleAndTrunc(QVector<double> *vec, double num, int maxSize);
};

#endif // VISUALIZATIONPAGE_H
