#include "wplanitem.h"
#include "gutil/qgui.h"
#include <QJsonArray>

wPlanItem::wPlanItem(QListWidgetItem *item, const QJsonObject *json) : m_item(item) {
    setAttribute(Qt::WA_DeleteOnClose);

    auto vBox = new VBox(this);
    vBox->setContentsMargins(6, 6, 0, 6);
    auto hBox = new HBox(vBox);

    fdIdx = new QLabel("1");
    hBox->addWidget(fdIdx);

    tStart = new QTimeEdit;
    tStart->setDisplayFormat("HH:mm");
    hBox->addWidget(tStart);

    tEnd = new QTimeEdit;
    tEnd->setDisplayFormat("HH:mm");
    tEnd->setTime(QTime(23, 59, 59));

    hBox->addWidget(tEnd);

    bnDel = new QPushButton;
    hBox->addWidget(bnDel);

    hBox = new HBox(vBox);

    sMon = new QPushButton(tr("M"));
    sMon ->setCheckable(true);
    sMon ->setProperty("ssName", "weeklySelector");
    hBox->addWidget(sMon);
    hBox->addStretch();

    sTues = new QPushButton(tr("Tu"));
    sTues->setCheckable(true);
    sTues->setProperty("ssName", "weeklySelector");
    hBox->addWidget(sTues);
    hBox->addStretch();

    sWed = new QPushButton(tr("W"));
    sWed ->setCheckable(true);
    sWed ->setProperty("ssName", "weeklySelector");
    hBox->addWidget(sWed);
    hBox->addStretch();

    sThur = new QPushButton(tr("Th"));
    sThur->setCheckable(true);
    sThur->setProperty("ssName", "weeklySelector");
    hBox->addWidget(sThur);
    hBox->addStretch();

    sFri = new QPushButton(tr("F"));
    sFri ->setCheckable(true);
    sFri ->setProperty("ssName", "weeklySelector");
    hBox->addWidget(sFri);
    hBox->addStretch();

    sSat = new QPushButton(tr("Sa"));
    sSat ->setCheckable(true);
    sSat ->setProperty("ssName", "weeklySelector");
    hBox->addWidget(sSat);
    hBox->addStretch();

    sSun = new QPushButton(tr("Su"));
    sSun ->setCheckable(true);
    sSun ->setProperty("ssName", "weeklySelector");
    hBox->addWidget(sSun);

    if(json==0) {
        sMon ->setChecked(true);
        sTues->setChecked(true);
        sWed ->setChecked(true);
        sThur->setChecked(true);
        sFri ->setChecked(true);
        sSat ->setChecked(true);
        sSun ->setChecked(true);
    } else {
        tStart->setTime(QTime::fromString(json->value("start").toString(), "hh:mm"));
        tEnd->setTime(QTime::fromString(json->value("end").toString(), "hh:mm"));
        auto oWeekly = json->value("weekly").toArray();
        sMon ->setChecked(oWeekly.contains(1));
        sTues->setChecked(oWeekly.contains(2));
        sWed ->setChecked(oWeekly.contains(3));
        sThur->setChecked(oWeekly.contains(4));
        sFri ->setChecked(oWeekly.contains(5));
        sSat ->setChecked(oWeekly.contains(6));
        sSun ->setChecked(oWeekly.contains(7));
    }
    connect(tStart, SIGNAL(timeChanged(const QTime&)), this, SLOT(onItemChanged()));
    connect(tEnd,   SIGNAL(timeChanged(const QTime&)), this, SLOT(onItemChanged()));
    connect(sMon,   SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(sTues,  SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(sWed,   SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(sThur,  SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(sFri,   SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(sSat,   SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(sSun,   SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(bnDel,  SIGNAL(clicked(bool)),             this, SLOT(onDelete(bool)));
}

void wPlanItem::onItemChanged(){
    emit sigItemChanged(m_item);
}

void wPlanItem::onDelete(bool){
    emit sigDelete(m_item);
}

QJsonObject wPlanItem::planJson() const {
    QJsonObject oRoot;
    QJsonArray  oWeekly;
    if(sMon ->isChecked()) oWeekly.append(QJsonValue(1));
    if(sTues->isChecked()) oWeekly.append(QJsonValue(2));
    if(sWed ->isChecked()) oWeekly.append(QJsonValue(3));
    if(sThur->isChecked()) oWeekly.append(QJsonValue(4));
    if(sFri ->isChecked()) oWeekly.append(QJsonValue(5));
    if(sSat ->isChecked()) oWeekly.append(QJsonValue(6));
    if(sSun ->isChecked()) oWeekly.append(QJsonValue(7));
    oRoot["start"]  = tStart->time().toString("hh:mm");
    oRoot["end"]    = tEnd->time().toString("hh:mm");
    oRoot["weekly"] = oWeekly;
    return oRoot;
}
