#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "alarmsservice.h"
#include "flowlayout.h"
#include "httpclient.h"
#include "journalservice.h"
#include "sensorservice.h"
#include "sensorwidget.h"
#include "paginationbar.h"
#include "qcustomplot.h"

#include <QMainWindow>
#include <QMap>
#include <QPixmap>
#include <QTreeWidget>
#include <QVBoxLayout>

#ifdef Q_OS_WIN
#include <Qlabel>
#elif defined(Q_OS_LINUX)
#include <QLabel>
#endif

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

struct TabImageData
{
    QLabel *label;
    QPixmap pixmap;
    QSize originalSize;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

private slots:
    void onIncidentsPageChanged(int page);
    void onJournalPageChanged(int page);
    void onSearchIncidentsClicked();
    void onSearchJournalClicked();
    void updateTreeClicked();
    void onRealTimeStatisticClicked();
    void onArchiveStatisticClicked();
    void onShowStatisticClicked();
    void updateRealTimeChart();
    void onSensorSelectStatisticChanged(int index);
    void ondialogClosedFromWidget(QString groupWithSensor, QDateTime start, QDateTime end);
    void onSaveButtonSettings();
    void onCancelButtonSettings();
    void onImportButtonSettings();
    void onExportButtonSettings();
    void onServerButtonSettings();

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

private:
    AlarmsService *asr;
    JournalService *jsr;
    HttpClient *httpClient;
    QWidget *widgetContainer;
    PaginationBar *paginationBarIncident;
    PaginationBar *paginationBarJournal;
    FlowLayout *layout;
    SensorService *sensorService;
    JournalService *journalService;
    AlarmsService *alarmsService;
    QMap<QString, QString> nameToId;
    AlarmSettings *settingsIncident;
    JournalSettings *journalSettings;
    bool statisticType;
    QMap<QString, int> groupWithSensorToIndex;
    QList<QString> groupWithSensor;
    QTimer *realTimeChartTimer;
    QCustomPlot *statisticChart;
    QMap<QString, QMap<QString, QPair<QString, QString>>> translate;
    Settings settings;

private:
    //COMMON------------------------------------------------------
    void loadSensorsTree(QTreeWidget *treeWidget);
    void moveTopLevelItems(QTreeWidget *source, QTreeWidget *destination);
    void startTranslateUpdateTimer();
    void TranslateUpdate();
    void startRefreshTimer();
    //------------------------------------------------------------

    //SENSOR------------------------------------------------------
    //dialog windows
    void showSensorDialog(SensorWidget *sensor);
    void onSensorDialogFinished(int result);
    //api requests
    void loadSensors(const QString &group);
    void loadSensorGroups();
    //update widgets
    void updateDisplayedWidgets();
    void onSearchTextChanged(const QString &text);
    void onGroupSelected(int index);
    void onShowHiddenSelected(int index);
    //utility
    void SetupSensorTab();
    //------------------------------------------------------------

    //INCEDENTS---------------------------------------------------
    //update widgets
    void onSearchIncedentsTextChanged(const QString &text);
    void onTreeIncidentsChanged(QTreeWidgetItem *item, int column);
    void onTimeIntervalChanged(const QString &selectedInterval);
    AlarmSettings* getIncidentDataSettings();
    //utility
    void SetupIncidentTab();
    //------------------------------------------------------------

    //JOURNAL-----------------------------------------------------
    //update widgets
    void onSearchJournalTextChanged(const QString &text);
    void onTreeJournalChanged(QTreeWidgetItem *item, int column);
    JournalSettings* getJournalDataSettings();
    //utility
    void SetupJournalTab();
    //------------------------------------------------------------

    //Statistic---------------------------------------------------
    //update widgets
    void updateComboboxStatistic();
    //utility
    void SetupStatisticTab();
    //------------------------------------------------------------
};
#endif // MAINWINDOW_H
