#ifndef MDI_WINDOW_H
#define MDI_WINDOW_H


#include <QMainWindow>
#include <QDialog>
#include <QTextEdit>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include "Objects.h"
#include "MdiChild.h"
#include "ColorPicker.h"


class QAction;
class QMenu;
class QMdiArea;
class QMdiSubWindow;
class QSignalMapper;
class QTcpServer;
class Ui_MainWindow;
class Ui_PaletteEditor;
struct UiContext;


/*
class MdiOptions
{
public:
    void serialize();
    void deserialize();
};
*/



class PaletteEditorDialog : public QDialog
{
    Q_OBJECT
public:
    explicit PaletteEditorDialog(QWidget *parent = 0);
    void init(Ui_PaletteEditor* ui);

    uint32_t GetColorScheme() const;//      { return editor->colorWheel->palette(); }
    void SetColorScheme(uint32_t col);
private slots:
    void setMode1();
    void setMode2();
    void setMode3();
    void setMode4();
    void setMode5();
    void updatePalette();
private:
    Ui_PaletteEditor *editor;
    QLabel *tints[10];
    QLabel *tones[10];
    QLabel *shades[10];
    QLabel *PrimaryHSVs[4];
    QLabel *SecondaryHSVs[16];
    QLabel *swatchHues[4];
    QLabel *swatchValues[4];
};



class MdiWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MdiWindow(QWidget *parent = 0);
    void init(Ui_MainWindow* ui, UiContext* context);

    bool openFile(const QString &fileName); // switches to the file if already open
    bool loadFile(const QString &fileName); // load worker, usually call openFile instead
signals:

protected:
    void closeEvent(QCloseEvent *event) override;
    void dragEnterEvent(QDragEnterEvent *e) override;
    void dropEvent(QDropEvent *e) override;

public slots:

private slots:

    // File menu actions
    void newFile();
    void open();
    void save();
    void saveAs();
    void exportPDF();

    void handleRequest();
    void updatePreview();

    void setWebPreviewURL(QString fileName);
    void reloadWebPreview();
    void refreshWebPreview();
    void refreshPreviewNow();
    void setAutoRefreshEnabled(bool a_enable) { m_autoRefreshEnabled = a_enable; refreshPreviewNow(); }
    void setToolbarLabelsEnabled(bool a_enable);
    void setToolbarSmall(bool a_enable);

    void newStyle();
    void deleteStyle();
    void newTemplate();
    void deleteTemplate();

    void newPalette();
    void editPalette();
    void paletteChanged();
    void templateChanged();
    void pageMarginsChanged();

    void removeTemplateItem();
    void addTemplateItem();
    void moveTemplateItemUp();
    void moveTemplateItemDown();

    // Edit menu actions
#ifndef QT_NO_CLIPBOARD
    void cut();
    void copy();
    void paste();
#endif

    void bold();
    void italic();
    void quote();
    void code();
    void heading1();
    void heading2();
    void heading3();
    void heading4();
    void hyperlink();
    void image();
    void unorderedList();
    void orderedList();
    void horizontalRule();
    void timestamp();
    void pageNumber();
    void pageCount();
    void date();
    void time();
    void tableOfContents();
    void options();
    void saveAll();
    void selectAll();
    void del();
    void undo();
    void redo();
    void find();
    void findNext();
    void findPrevious();
    void goToLine();

    // Help
    void support();
    void website();

    // Window menu actions
    void cascadeSubWindows();
    void tileSubWindows();
    void tabSubWindows();

    void updateActions();
    MdiChild *createMdiChild();
    void switchLayoutDirection();
    void setActiveSubWindow(QWidget *window);

    void changeRootObject(const QModelIndex &index);
    void changePropertiesObject(const QModelIndex &index);

    // Dialog events
    void upgradeClicked();


    void checkForUpdates();
    void checkProgress(qint64 bytesReceived, qint64 bytesTotal);
    void checkFinished();
    void downloadUpdate();
    void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
    void downloadFinished();
    void installUpdate();
    void installProgress(qint64 bytesReceived, qint64 bytesTotal);
    void installFinished();
    void restart();


    bool RequestTrialUse();
    bool RequestLicenseUpgrade();
    bool RequestTrialActivation();

    void trialActivate();
    void trialActivationProgress(qint64 bytesReceived, qint64 bytesTotal);
    void trialActivationFinished();

    void showTrialDialog();

    // Project settings
    void updateProjectSettingsOnWidgetChange();
    void updateProjectSettingsOnTextChange();
    void saveProject();
    void populateProjectDialogValues();
    void passwordShow();
    void passwordHide();


    void openRecentFile();
private:
    void setCurrentFile(const QString &fileName);
    void updateRecentFileActions();
    enum { MaxRecentFiles = 5 };
    QAction *recentFileActs[MaxRecentFiles];
    QAction *separatorAct;


    void changeProjectTree(QObject *rootObject);
    void updateObjectTree(QObject *rootObject);
    void updatePropertiesView(QObject *object);
    void setupServer();
    void readSettings();
    void readLicense();
    void writeSettings();
    MdiChild *activeMdiChild();
    QMdiSubWindow *findMdiChild(const QString &fileName);

    QString outputFileName();
    void outputPDF(bool preview);
    bool m_doingRefresh;
    bool m_refreshNeeded;
    bool m_autoRefreshEnabled;

    QString currentProject;
    QString currentStyle;
    QString currentTemplate;
  
    bool avoidProjectPopulateRecursion;
    bool avoidPopulateRecursion;
    bool paletteEditIsNew;

    //Solution solution;
    QByteArray pdfContents;
    QTcpServer *server;
    QMdiArea *mdiArea;
    QSignalMapper *windowMapper;
    Ui_MainWindow *appWindow;
    UiContext *uiContext;
    QString currentTmpFile;

    QNetworkAccessManager networkManager;
    QNetworkReply* checkRequest;
    QNetworkReply* downloadRequest;
    QNetworkReply* trialActivateRequest;
    std::string latestVersion;
    std::string latestUrl;
};


#endif // MDI_WINDOW_H
