#include "timewidget.h"
#include "../lock-dialog/lockdialogmodel.h"

#define TIME_FONT_SIZE 36
#define TIME_DATE_SIZE 18
#define TIMEWIDGET_SIZE 1000,100

TimeWidget::TimeWidget(LockDialogModel *model, QWidget *parent)
    : QWidget(parent)
    , m_modelLockDialog(model)
{
    this->setMinimumSize(TIMEWIDGET_SIZE);

    m_curFontSize = m_modelLockDialog->getCurFontSize();
    m_ptToPx = m_modelLockDialog->getPtToPx();
    QVBoxLayout *m_VBLayout = new QVBoxLayout(this);

    m_t_label = new KLabel(this);
    m_t_label->setAlignment(Qt::AlignCenter);
    m_t_label->setFontSize(TIME_FONT_SIZE);
    m_t_label->setStyleSheet("QLabel{color: white;}"
                        "QToolTip{border-radius:4px; background-color:#FFFFFF; color:black;}");
    m_t_label->setText(getLongFormatDate(TIME));
    m_t_label->adjustSize();

    m_d_label = new KLabel(this);
    m_d_label->setAlignment(Qt::AlignCenter);
    m_d_label->setStyleSheet("QLabel{color: white;}"
                        "QToolTip{border-radius:4px; background-color:#FFFFFF; color:black;}");
    m_d_label->setText(getLongFormatDate(DATE));
    m_d_label->setFontSize(TIME_DATE_SIZE);
    m_d_label->adjustSize();

    m_VBLayout->addWidget(m_t_label);
    m_VBLayout->addWidget(m_d_label);
    this->setLayout(m_VBLayout);
    this->adjustSize();

    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(update_datatime()));
    this->timerStart();
}

TimeWidget::~TimeWidget()
{
    m_timer->deleteLater();
}

void TimeWidget::updateUI()
{
    this->setFixedSize(this->width(), this->height());
    m_t_label->setFontSize((TIME_FONT_SIZE + m_curFontSize)*m_ptToPx);
    m_d_label->setFontSize((TIME_DATE_SIZE + m_curFontSize)*m_ptToPx);
}

void TimeWidget::timerStart()
{
    m_timer->start(1000);
}

void TimeWidget::timerStop()
{
    m_timer->stop();
}

QString TimeWidget::getLongFormatDate(int type)
{
    kdk_logn_dateinfo *dateInfo = kdk_system_login_lock_dateinfo(getenv("USER"));
    if (type == DATE)
    {
        QString date = dateInfo->date;
        QString week = dateInfo->week;
        if (dateInfo)
        {
            kdk_free_logn_dateinfo(dateInfo);
        }
        return  date + " " + week;
    }
    else if (type == TIME)
    {
        QString time = dateInfo->time;
        if (dateInfo)
        {
            kdk_free_logn_dateinfo(dateInfo);
        }
        return time;
    }
}

void TimeWidget::update_datatime()
{
    m_t_label->setText(getLongFormatDate(TIME));
    m_d_label->setText(getLongFormatDate(DATE));
}
