#include "grifodebugdevice.h"

#include <QToolBar>
#include <QAction>

#include "grifodebugmessage.h"

class GrifoDebugDevice::Implementation
{
public:
    struct DbgMsg
    {
        uint32_t discretes; //Wow, Fast
        uint32_t tgtTestPattern;
        uint32_t scTestPattern;
        uint32_t hrTestPattern;

        uint32_t gcDbgShow;
        uint32_t gcDbgCrsX;
        uint32_t gcDbgCrsY;
    };

    QObject* parent;

    QToolBar* tb;

    QAction* aWow;
    QAction* aFast;

    Implementation(QObject* p):
        parent(p),
        tb(0)
    {
    }

    void init()
    {
        tb=new QToolBar("SimDbg");
        aWow=tb->addAction("WoW");
        aWow->setCheckable(true);

        aFast=tb->addAction("Fast");
        aFast->setCheckable(true);
    }
};

GrifoDebugDevice::GrifoDebugDevice():
    QgScopeDevice("dbgSim.grifo", "dbgSim"),
    p_(*new Implementation(this))
{
#if 0
    connect(p_.aWow, &QAction::toggled, this,
            [this](bool checked)
            {
                GrifoDebugMessage::cmpDbgSim.discrete_sim|=GrifoCMpDbgSimMessage::discrete_wow_override_enable;
                if (checked)
                    GrifoDebugMessage::cmpDbgSim.discrete_sim|=GrifoCMpDbgSimMessage::discrete_wow_active;
                else
                    GrifoDebugMessage::cmpDbgSim.discrete_sim&=~GrifoCMpDbgSimMessage::discrete_wow_active;
                GrifoDebugMessage().cmpUpdate();
            }
            );
#endif
}

GrifoDebugDevice::~GrifoDebugDevice()
{
    delete &p_;
}

GrifoDebugDevice& GrifoDebugDevice::instance()
{
    static GrifoDebugDevice* inst=new GrifoDebugDevice;
    return *inst;
}

QToolBar* GrifoDebugDevice::toolBar()
{
    if (!p_.tb)
    {
       p_.init();
       connect(p_.aWow, &QAction::toggled, this,
               [this](bool checked)
               {
                   GrifoDebugMessage::cmpDbgSim.discrete_sim|=GrifoCMpDbgSimMessage::discrete_wow_override_enable;
                   if (checked)
                       GrifoDebugMessage::cmpDbgSim.discrete_sim|=GrifoCMpDbgSimMessage::discrete_wow_active;
                   else
                       GrifoDebugMessage::cmpDbgSim.discrete_sim&=~GrifoCMpDbgSimMessage::discrete_wow_active;
                   GrifoDebugMessage().cmpUpdate();
               }
               );


    }
    return p_.tb;
}

bool GrifoDebugDevice::WoW() const
{
    return p_.aWow->isChecked();
}

void GrifoDebugDevice::setWoW(bool wow)
{
    p_.aWow->setChecked(wow);
}


bool GrifoDebugDevice::fastPowerup() const
{
    return p_.aFast->isChecked();
}

void GrifoDebugDevice::setFastPowerup(bool enable)
{
    p_.aFast->setChecked(enable);
    GrifoDebugMessage().cmpUpdate();
}

bool GrifoDebugDevice::remoteGC() const
{
    return GrifoDebugMessage::cmpDbgSim.remote_gc!=0;
}

void GrifoDebugDevice::setRemoteGC(bool enable)
{
    GrifoDebugMessage::cmpDbgSim.remote_gc=enable;
    GrifoDebugMessage().cmpUpdate();
}

bool GrifoDebugDevice::softSC() const
{
    return GrifoDebugMessage::cmpDbgSim.soft_scan_converter;
}

void GrifoDebugDevice::setSoftSC(bool enable)
{
    GrifoDebugMessage::cmpDbgSim.soft_scan_converter=enable ? 1 : 0;
    GrifoDebugMessage().cmpUpdate();
}

int GrifoDebugDevice::rawTestPattern() const
{
    return GrifoDebugMessage::cmpDbgSim.raw_test_pattern;
}

void GrifoDebugDevice::setRawTestPattern(int pattern)
{
    GrifoDebugMessage::cmpDbgSim.raw_test_pattern=pattern;
    GrifoDebugMessage().cmpUpdate();
}
