#ifndef MAINWINDOWNEW_H
#define MAINWINDOWNEW_H

#include <QMainWindow>
#include <QWidget>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QLabel>
#include <QPushButton>
#include <QButtonGroup>
#include <QLoggingCategory>

#include <QMouseEvent>
#include <QMainWindow>
#include <QLabel>
#include <QTimer>
#include <QProcess>
#include <QSettings>
#include <QMessageBox>
#include <QDebug>
#include <QFileDialog>
#include <QListWidgetItem>
#include <cmath>
#include <QEventLoop>
#include <QDesktopServices>
#include <QLoggingCategory>

#include "bmsinterface.h"
#include "ui_mainwindow.h"
#include "parametereditor.h"
#include "utility.h"

#include "utility.h"
#include "customlefttabbarbutton.h"
#include "connectandenterpage.h"
#include "debugprintpage.h"

// RELEASE_VERSION = true => fixed window size and no window header pannel with |hide|expand|close buttons
#define RELEASE_VERSION true

/*
 start  0  12 = Default that is not in bms it's in somekind of real offline file (we do NOT have connection to bms but there parameters show up somehow)
connect 8  8 Config updated with default params not from file, but from default params of bms
     bms  edit | bms edit
^  -   8     7 |   8    8 read bms conf
-> -   7     7 |   7    8 read default 8 is defaul - that was on the start
v  -   8     7 |   7    7 write bms config
S  -           |          save currect config from bms into outside file to the flash card
     before    |   after
*/

namespace Ui {
class MainWindowNew;
}

class MainWindowNew : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindowNew(QWidget *parent = nullptr);
    ~MainWindowNew();
private:
    Ui::MainWindowNew *ui;

//////////////////////////////////////

public:
    enum TabWidgetPage
    {
        ConnectWindow = 0,
        AkbMonitor,
        CellsMonitor,
        Configuration,
        Visualization,
        History,
        SeriveOfBms,
        SettingsExtra,
    };

    TabWidgetPage m_currentPageIndex = AkbMonitor;
    TabWidgetPage m_prevPageIndex = AkbMonitor;

    bool isConnectWindowActivated() const;


    void initAnimationForLeftTabsBar();

    void setAnimParams(QParallelAnimationGroup *group, QPropertyAnimation *a, int sec, int from, int to);
    void setAnimParamsMin(QParallelAnimationGroup *group, QPropertyAnimation *a, int sec, CustomLeftTabBarButton *btn);
    void setAnimParamsMax(QParallelAnimationGroup *group, QPropertyAnimation *a, int sec, CustomLeftTabBarButton *btn);
    void setAnimParamsMin(QParallelAnimationGroup *group, QPropertyAnimation *a, int sec, QLabel *btn);
    void setAnimParamsMax(QParallelAnimationGroup *group, QPropertyAnimation *a, int sec, QLabel *btn);
    void setAnimLogoMin(QParallelAnimationGroup *group, QPropertyAnimation *a, int sec, QLabel *btn);
    void setAnimLogoMax(QParallelAnimationGroup *group, QPropertyAnimation *a, int sec, QLabel *btn);
    int m_logoWay;

    void clearBtnText(CustomLeftTabBarButton *btn);
    void clearAllBtnsText();
    void restoreBtnText(CustomLeftTabBarButton *btn);
    void restoreAllBtnsText();

    bool m_minimize;
    QParallelAnimationGroup *m_animgroupMin;
    QParallelAnimationGroup *m_animgroupMax;


signals:
    void sendStatusLabelStyleSheet(QString s);

    void sigShowConnectWindow();
    void loadParams();

private slots:
    void onAnyLeftTabBarButtonClicked();

    void timerSlot();
    void showStatusInfo(QString info, bool isGood);
    void showMessageDialog(const QString &title, const QString &msg, bool isGood, bool richText);
    void serialPortNotWritable(const QString &port);
    void bmsconfUpdated();
    void bmsConfigCheckResult(QStringList paramsNotSet);


    ///void bmsconfUpdated();
    ///void showStatusInfo(QString info, bool isGood);
    ///void onTimerSlot();

    void onShrink();
    //void onMainMenuBarButtonClicked();
    //void onHideThisAndShowConnectWindow();
    //void onShowConnectWindow();

    void onShowConnectWindow();
    void onHideConnectWindow();

private:
    const int mWidth = 1366;
    const int mHeight = 738;

    const int mWidthConnectPage = 637;
    const int mHeightConnectPage = 433;

    void moveApplicationToTheCenter();

    void setAppSizeForFullView();
    void setAppSizeForConnectionPage();

    QSettings mSettings;
    QString mVersion;
    QString mStatusLabelText;
    //QLabel *mStatusLabel;
    bool mKeyLeft;
    bool mKeyRight;
    QTimer *mTimer;
    int mStatusInfoTime;
    bool mMcConfRead;

    BMSInterface *mDieBieMS; // Business logic class

    DebugPrintPage *mPageDebugPrint;

    // UI
    QButtonGroup* m_btnGroupMainMenuBar;

    //ConnectAndEnterPage mConnect;
    bool mIsConnectionPrepared;
    QString mComPortToConnect;

    void setAllMainMenuBarButtonsVisible(bool val);
    void setLeftTopWidgetsVisible(bool value);
    void unselectAllLeftTabBarButtons();
    void selectCurrentLeftTabBarButton();

    void onSelectCurrentLeftTabBarButton();

    void reloadPages();
    void initDieBieMs();
    bool addParamRow(ConfigParams *params, QString paramName);

    void checkUdev();

    //
    void showWindowInOneStaticPlaceWithoutHideResizeCloseButtons();

    ///
    /// To make Window move on MOUSE CLICK MOVE
    ///
    protected:
        void mousePressEvent(QMouseEvent *evt)
        {
            oldPos = evt->globalPos();
        }

        void mouseMoveEvent(QMouseEvent *evt)
        {
            const QPoint delta = evt->globalPos() - oldPos;
            move(x()+delta.x(), y()+delta.y());
            oldPos = evt->globalPos();
        }

    private:
        QPoint oldPos;
    ///
    /// To make Window move on MOUSE CLICK MOVE
    ///
};


#endif // MAINWINDOWNEW_H
