#include "ui_About.h"
#include "ui_CheckUpdates.h"
#include "ui_ExtensibleObjectModelUI.h"
#include "ui_Help.h"
#include "ui_LicenseUpgrade.h"
#include "ui_NewProjectWizard.h"
#include "ui_PaletteEditor.h"
#include "ui_SendBugReport.h"
#include "ui_Settings.h"

#include "EventLog.h"
#include "ColorPicker.h"
#include "GenericTableUI.h"
#include "MdiWindow.h"
#include "Version.h"
#include "SystemInfomation.h"
#include <QtWidgets>
#include <QWebView>


class UiLogger : public Logger
{
public:
	void init(EventLog* a_log)
	{
		m_log = a_log;
	}
	void logMessage(yqLogLevel a_logLevel, const char *a_location, const char *a_message)
	{
		m_log->LogMessage(a_location, a_message);
	}
private:
	EventLog* m_log;
};


struct UiContext
{
	Ui_About about;
    QDialog aboutDialog;

    Ui_CheckUpdates checkUpdates;
    QDialog checkUpdatesDialog;

    Ui_MainWindow appWindow;
    MdiWindow mainWindow;

    Ui_Help help;
    QDialog helpDialog;

    Ui_LicenseUpgrade licenseUpgrade;
    QDialog licenseUpgradeDialog;

    Ui_NewProjectWizard newProject;
    QDialog newProjectDialog;

    Ui_PaletteEditor paletteEditor;
    QDialog paletteEditorDialog;

    Ui_SendBugReport sendBugReport;
    QDialog sendBugReportDialog;

    Ui_Settings settings;
    QDialog settingsDialog;

    ColorPicker colorPicker;

    EventLog log;
    GenericTableUI logView;
    UiLogger logger;

	void setupUi()
	{
        logger.init(&log);
        g_currentModule.m_messageLogger = &logger;
        YQ_LOG_DEBUG("Initializing");

        about.setupUi(&aboutDialog);
        checkUpdates.setupUi(&checkUpdatesDialog);
        appWindow.setupUi(&mainWindow);
        help.setupUi(&helpDialog);
        licenseUpgrade.setupUi(&licenseUpgradeDialog);
        newProject.setupUi(&newProjectDialog);
        paletteEditor.setupUi(&paletteEditorDialog);
        sendBugReport.setupUi(&sendBugReportDialog);
        settings.setupUi(&settingsDialog);

        // Fill-in details
        SystemInfo sysInfo;
        getSystemInfo(sysInfo);
        sendBugReport.appName->setText(APP_NAME);
        sendBugReport.appVer->setText(APP_VER_STR);
        sendBugReport.platName->setText(sysInfo.m_osType.c_str());
        sendBugReport.platVer->setText(sysInfo.m_osVersion.c_str());
        sendBugReport.cpu->setText(sysInfo.m_cpuType.c_str());
        sendBugReport.dateTime->setText(QDateTime::currentDateTime().toString());
        sendBugReport.ram->setText(sysInfo.m_ramSizeStr.c_str());
        sendBugReport.res->setText(sysInfo.m_resolutionStr.c_str());

        about.appName->setText(APP_NAME);
        about.appVer->setText(APP_VER_STR_LONG);
        about.appIcon->setPixmap(QPixmap(APP_ICON));

        // Setup standard keyboard short cuts
        appWindow.actionNew->setShortcuts(QKeySequence::New);
        appWindow.actionOpen->setShortcuts(QKeySequence::Open);
        appWindow.actionSave->setShortcuts(QKeySequence::Save);
        appWindow.actionSave_As->setShortcuts(QKeySequence::SaveAs);
        appWindow.actionExit->setShortcuts(QKeySequence::Quit);
        appWindow.actionCut->setShortcuts(QKeySequence::Cut);
        appWindow.actionCopy->setShortcuts(QKeySequence::Copy);
        appWindow.actionPaste->setShortcuts(QKeySequence::Paste);
        appWindow.actionHelp->setShortcuts(QKeySequence::HelpContents);
        appWindow.actionDelete->setShortcuts(QKeySequence::Delete);
        appWindow.actionClose->setShortcuts(QKeySequence::Close);
        appWindow.actionUndo->setShortcuts(QKeySequence::Undo);
        appWindow.actionRedo->setShortcuts(QKeySequence::Redo);
        appWindow.actionOptions->setShortcuts(QKeySequence::Preferences);
        appWindow.actionBold->setShortcuts(QKeySequence::Bold);
        appWindow.actionItalic->setShortcuts(QKeySequence::Italic);
        appWindow.actionFind->setShortcuts(QKeySequence::Find);
        appWindow.actionFind_Next->setShortcuts(QKeySequence::FindNext);
        appWindow.actionFind_Previous->setShortcuts(QKeySequence::FindPrevious);
        appWindow.actionSelect_All->setShortcuts(QKeySequence::SelectAll);
        appWindow.actionNext->setShortcuts(QKeySequence::NextChild);
        appWindow.actionPrevious->setShortcuts(QKeySequence::PreviousChild);

        QObject::connect(licenseUpgrade.upgradeButton, SIGNAL(clicked()), &mainWindow, SLOT(upgradeClicked()));

        QObject::connect(appWindow.actionExit,              SIGNAL(triggered(bool)), qApp,                  SLOT(closeAllWindows()));
        QObject::connect(appWindow.actionAbout,             SIGNAL(triggered(bool)), &aboutDialog,          SLOT(exec()));
        QObject::connect(appWindow.actionCheck_for_Updates, SIGNAL(triggered(bool)), &checkUpdatesDialog,   SLOT(exec()));
        QObject::connect(appWindow.actionHelp,              SIGNAL(triggered(bool)), &helpDialog,           SLOT(exec()));
        QObject::connect(appWindow.actionUpgrade,           SIGNAL(triggered(bool)), &licenseUpgradeDialog, SLOT(exec()));
        QObject::connect(appWindow.actionNew_Project,       SIGNAL(triggered(bool)), &newProjectDialog,     SLOT(exec()));
        QObject::connect(appWindow.actionChoose_Palette_Old,SIGNAL(triggered(bool)), &colorPicker,          SLOT(exec()));
        QObject::connect(appWindow.actionChoose_Palette,    SIGNAL(triggered(bool)), &paletteEditorDialog,  SLOT(exec()));
        QObject::connect(appWindow.actionReport_a_bug,      SIGNAL(triggered(bool)), &sendBugReportDialog,  SLOT(exec()));
        QObject::connect(appWindow.actionOptions,           SIGNAL(triggered(bool)), &settingsDialog,       SLOT(exec()));

        logView.init(&log, appWindow.eventLog);
        mainWindow.init(&appWindow);
        mainWindow.show();

        YQ_LOG_DEBUG("Started");

 /*

   // Actions require handling:

        actionSave_All;
        actionSelect_All;
        actionDelete;
        actionUndo;
        actionRedo;
        actionFind;
        actionFind_Next;
        actionFind_Previous;
        actionGo_to_Line;

        actionCode;

        actionHyperlink;
        actionImage;
        actionUnordered_List;
        actionOrdered_List;
        actionHorizontal_Rule;
        actionTimestamp;
        actionPage_Number;
        actionPage_Count;
        actionOptions;

  // QKeySequences that might be useful actions to add:

        WhatsThis,
        ZoomIn,
        ZoomOut,
        Print,
        Refresh,
        AddTab,
        Underline,
        Back,
        Forward,
        Replace,

 */
	}
};


