#include <QAxWidget>
#include <QLibraryInfo>
#include <QSysInfo>
#include <QApplication>
#include <QDesktopWidget>
#include "ggant.h"
#include <ocidl.h>
#include <QDebug>
#include <QStringList>
#include <QMouseEvent>
#include <QUuid>

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

bool optInitToZero = true;

class GantInspector : public VcGanttLib::VcGantt {
public:

protected:
    void mousePressEvent(QMouseEvent *me);
};

void GantInspector::mousePressEvent(QMouseEvent *me)
{
    const QPoint pos = me->pos();
    IDispatch *dispReturn = (IDispatch *)(optInitToZero ? 0x0 : 0xdeadbeef);
    qDebug() << "dispReturn=" << dispReturn << " &dispReturn=" << &dispReturn;
    VcGanttLib::VcObjectTypeEnum identObjType = VcGanttLib::vcObjTypeNone;
    qDebug() <<  "Calling IdentifyObjectAt(" << pos << "&dispReturn="<< &dispReturn << ',' << identObjType << ')';
    bool rc = IdentifyObjectAt(pos.x(), pos.y(), &dispReturn, identObjType);
    qDebug() <<  "res=" << rc << "dispReturn=" << dispReturn;
    if (rc && dispReturn) {
        QAxObject axo(dispReturn);
        qDebug() << "\nObject at " <<pos << '\n' << axo.generateDocumentation() << '\n';
        dispReturn->Release();
    }
}

int main(int argc, char **argv)
{
    QApplication a(argc, argv);
    QStringList args = QCoreApplication::arguments();
    args.pop_front();
    optInitToZero = !args.contains("-i");

    GantInspector gantWidget;
    const QRect ag = QApplication::desktop()->availableGeometry(&gantWidget);
    gantWidget.resize(ag.width() * 2 / 3, ag.height() / 2);
    gantWidget.move(ag.topLeft() + QPoint((ag.width() - gantWidget.width()) /2 , 100));

    IDispatch *disp = 0;
    gantWidget.queryInterface(IID_IDispatch, (void **)&disp);
    qDebug() <<  QT_VERSION_STR << args
#if QT_VERSION >= 0x050000
             << '\n' << QLibraryInfo::build()
#endif
             << "\nword size" << QSysInfo::WordSize << '\n';
    if (!disp) {
        qDebug("Did not obtain IID_IDispatch");
        return -1;
    }

    qDebug() <<  "disp=" << disp
             << "Press to dump docu of obj at mouse, fex title bar.";

    gantWidget.setWindowTitle(QT_VERSION_STR);
    gantWidget.show();
    int r = a.exec();
    disp->Release();
    return r;
}
