#include <QAxWidget>
#include <QLibraryInfo>
#include <QSysInfo>
#include <QApplication>
#include <QDesktopWidget>
#include <QScreen>
#include "vcgantt.h"
#include <ocidl.h>
#include <QDebug>
#include <QStringList>
#include <QMouseEvent>
#include <QUuid>
#include <QTimer>
#ifdef QT_DIAG_LIB
#  include <eventfilter.h>
#  include <qwindowdump.h>
#  include <nativewindowdump.h>
#endif

bool optNativeWins = false;

/* type lib id: {A4E79A23-C9E1-11CF-BDD7-02608C4302A9}
 * dumpcpp -getfile {A4E79A23-C9E1-11CF-BDD7-02608C4302A9}
 * To generate vcganttlib.cpp/.h
 * dumpcpp -axwindow {A4E79A23-C9E1-11CF-BDD7-02608C4302A9} -o vcgantt
  */

class GantWindow : public VcGanttLib::VcGantt {
        Q_OBJECT
public:
    explicit GantWindow(QWindow *parent = 0) :  VcGanttLib::VcGantt(QString(),parent) {}

protected:
    void keyPressEvent(QKeyEvent *ke) Q_DECL_OVERRIDE;
    void mousePressEvent(QMouseEvent *me) Q_DECL_OVERRIDE;
public slots:
    void dump();
};

void GantWindow::keyPressEvent(QKeyEvent *ke)
{
    qDebug() << __FUNCTION__ << ke;
    dump();
}

void GantWindow::mousePressEvent(QMouseEvent *me)
{
    qDebug() << __FUNCTION__ << me;
    dump();
}

void GantWindow::dump()
{
#ifdef QT_DIAG_LIB
    qDebug() << '\n';
    QtDiag::dumpAllWindows();
    if (optNativeWins)
        QtDiag::dumpNativeQtTopLevels();
    qDebug() << "GetFocus=" << GetFocus();
#endif
}

int main(int argc, char **argv)
{
    QGuiApplication a(argc, argv);
    QStringList args = QCoreApplication::arguments();
    args.pop_front();
    optNativeWins = args.contains("-a");

#ifdef QT_DIAG_LIB
       if (!args.contains("-e")) {
           QtDiag::EventFilter::EventCategories cats = QtDiag::EventFilter::KeyEvents
                   | QtDiag::EventFilter::MouseEvents;
           QtDiag::EventFilter *ef =  new QtDiag::EventFilter(cats);
           a.installEventFilter(ef);
           qDebug()<< "installEventFilter";
       }
#endif
    const QRect ag = QGuiApplication::primaryScreen()->availableGeometry();
    const QSize size(ag.width() * 2 / 3, ag.height() / 2);

    QScopedPointer<QWindow> mainWin;
    GantWindow *gantWin=0;
    if (args.contains("-p")) {
        mainWin.reset(new QWindow);
        mainWin->setObjectName("mainWIn");
        mainWin->resize(size);
        gantWin = new GantWindow(mainWin.data());
        gantWin->resize(size);
    } else {
        gantWin = new GantWindow;
        gantWin->resize(size);
        mainWin.reset(gantWin);

    }
    mainWin->setFramePosition(ag.topLeft() + QPoint((ag.width() - mainWin->width()) /2 , 100));

    if (args.contains("-t")) {
        QTimer  *tm  =new QTimer(gantWin);
        QObject::connect(tm, &QTimer::timeout, gantWin, &GantWindow::dump);
        tm->start(2000);
    }

    qDebug() <<  QT_VERSION_STR << args
             << '\n' << QLibraryInfo::build()
             << "\nword size" << QSysInfo::WordSize << '\n';

    mainWin->setTitle(QT_VERSION_STR + QLatin1String(" QAxWindow"));
    mainWin->show();
    if (mainWin.data() != gantWin)
        gantWin->show();
    return  a.exec();
}

#include "main.moc"
