#include "wplanitem.h"
#include "ui_wplanitem.h"

wPlanItem::wPlanItem(QListWidgetItem *item, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::wPlanItem),
    m_item(item)
{
    setAttribute(Qt::WA_DeleteOnClose);
    ui->setupUi(this);
    initBegin();
    ui->sMon ->setChecked(true);
    ui->sTues->setChecked(true);
    ui->sWed ->setChecked(true);
    ui->sThur->setChecked(true);
    ui->sFri ->setChecked(true);
    ui->sSat ->setChecked(true);
    ui->sSun ->setChecked(true);
    initEnd();
}

wPlanItem::wPlanItem(const QJsonObject &oRoot, QListWidgetItem *item, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::wPlanItem),
    m_item(item)
{
    setAttribute(Qt::WA_DeleteOnClose);
    ui->setupUi(this);
    initBegin();
    setPlan(oRoot);
    initEnd();
}

void wPlanItem::initBegin()
{
    ui->sMon ->setCheckable(true);
    ui->sTues->setCheckable(true);
    ui->sWed ->setCheckable(true);
    ui->sThur->setCheckable(true);
    ui->sFri ->setCheckable(true);
    ui->sSat ->setCheckable(true);
    ui->sSun ->setCheckable(true);

    ui->sMon ->setProperty("ssType", "progEditor");
    ui->sTues->setProperty("ssType", "progEditor");
    ui->sWed ->setProperty("ssType", "progEditor");
    ui->sThur->setProperty("ssType", "progEditor");
    ui->sFri ->setProperty("ssType", "progEditor");
    ui->sSat ->setProperty("ssType", "progEditor");
    ui->sSun ->setProperty("ssType", "progEditor");

    ui->sMon ->setProperty("ssName", "weeklySelector");
    ui->sTues->setProperty("ssName", "weeklySelector");
    ui->sWed ->setProperty("ssName", "weeklySelector");
    ui->sThur->setProperty("ssName", "weeklySelector");
    ui->sFri ->setProperty("ssName", "weeklySelector");
    ui->sSat ->setProperty("ssName", "weeklySelector");
    ui->sSun ->setProperty("ssName", "weeklySelector");
}

void wPlanItem::initEnd()
{
    connect(ui->tStart, SIGNAL(timeChanged(const QTime&)), this, SLOT(onItemChanged()));
    connect(ui->tEnd,   SIGNAL(timeChanged(const QTime&)), this, SLOT(onItemChanged()));
    connect(ui->sMon,   SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(ui->sTues,  SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(ui->sWed,   SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(ui->sThur,  SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(ui->sFri,   SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(ui->sSat,   SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(ui->sSun,   SIGNAL(toggled(bool)),             this, SLOT(onItemChanged()));
    connect(ui->bnDel,  SIGNAL(clicked(bool)),             this, SLOT(onDelete(bool)));
}

wPlanItem::~wPlanItem()
{
    delete ui;
}

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

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

void wPlanItem::setNum(int n)
{
    ui->wNum->setText(QString("%1").arg(n));
}

void wPlanItem::setPlan(const QJsonObject &oRoot)
{
    QJsonArray oWeekly = oRoot["weekly"].toArray();
    ui->tStart->setTime(QTime::fromString(oRoot["start"].toString(), "hh:mm"));
    ui->tEnd->setTime(QTime::fromString(oRoot["end"].toString(), "hh:mm"));
    ui->sMon ->setChecked(oWeekly.contains(QJsonValue(1)));
    ui->sTues->setChecked(oWeekly.contains(QJsonValue(2)));
    ui->sWed ->setChecked(oWeekly.contains(QJsonValue(3)));
    ui->sThur->setChecked(oWeekly.contains(QJsonValue(4)));
    ui->sFri ->setChecked(oWeekly.contains(QJsonValue(5)));
    ui->sSat ->setChecked(oWeekly.contains(QJsonValue(6)));
    ui->sSun ->setChecked(oWeekly.contains(QJsonValue(7)));
}

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