#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QStatusBar>
#include <QVector>
#include <QString>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

#include "worker.h"
#include "update.h"
#include "update_dc_event.h"

#define EMERGENCY_LEAVE_BL 0

class hwinf;
class MainWindow : public QMainWindow {
    Q_OBJECT

protected:
    void customEvent(QEvent *event) override;

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

    static const int START_PROGRESS_LOOP = -1;
    static const int STOP_PROGRESS_LOOP = -2;
    static const int BL_START_COUNT = 5;
    static const int BL_CHECK_COUNT = 5;
    static const int BL_IS_UP_COUNT = 5;
    static const int BL_STOP_COUNT = 5;

    UpdateDcEvent::UpdateStep updateStep() const { return m_updateStep; }
    void setUpdateStep(UpdateDcEvent::UpdateStep updateStep) { m_updateStep = updateStep; }

    QString targetDcVersion() {return  m_targetDcVersion; }

public slots:
    void onAppendText(QString, QString suffix = "");
    void onInsertText(QString);
    void onReplaceLast(QStringList, QString suffix = "");
    void onReplaceLast(QString, QString suffix = "");
    void onShowErrorMessage(QString, QString);
    void onShowStatusMessage(QString, QString);
    void onShowErrorMessage(QStringList);
    void onShowStatusMessage(QStringList);
    void onStopStartTimer();
    void onRestartExitTimer();
    void onEnableExit();
    void onDisableExit();
    void onShowDcDownload(QString);
    void onShowJsonDownload(QString);
    void onShowTariffUpdate(QString);
    void onShowISMASChecks(QString);
    void onShowISMASConnectivity(QString);
    void onShowUpdateRequest(QString);
    void onShowCustRepoStatus(QString);
    void onShowExecOpkgStatus(QString);
    void onShowExecOpkgCommand(QString);
    void onShowExecOpkgOverallResult(QString,bool);
    void onShowDownloadDCJsonFilesStatus(QString);
    void onShowSyncCustRepoStatus(QString);
    void onShowUpdateDCFirmware(QString);
    void onSetDcDownloadProgress(int);
    void onShowSummary(QString);
    void onSummary();
#if EMERGENCY_LEAVE_BL==1
    void emergencyLeaveBL();
#endif

signals:

#if EMERGENCY_LEAVE_BL==1
    void leaveBL();
#endif

private slots:
    void onQuit();

private:

    int const m_showLineLength = 37;

    void scrollDownTextEdit();
    void onShowMessage(QString, QString);

    Ui::MainWindow *ui;
    Worker *m_worker;
    int const m_width;
    QTimer *m_startTimer;
    QTimer *m_exitTimer;
    bool m_progressRunning;
    //int m_progressValue;
    UpdateDcEvent::UpdateStep m_updateStep;
    QTimer *m_statusTimer;
    QString m_targetDcVersion;
    int m_stepLabelChopCount{};

    QVector<QString> m_updateSteps{};

};
#endif // MAINWINDOW_H