#include "dateTimeWidget.h"
#include "./ui_dateTimeWidget.h"
#include "dateTimeSelectionPanel.h"

#include <QTime>
#include <QTimer>

DateTimeWidget::DateTimeWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::dateTimeWidget)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_TranslucentBackground);

    initialize();
}

DateTimeWidget::~DateTimeWidget()
{
    delete ui;

    if(m_pTimer_Range->isActive())
        m_pTimer_Range->stop();

    // if(m_pTimer_RealTime->isActive())
    //     m_pTimer_RealTime->stop();
}

bool DateTimeWidget::eventFilter(QObject* obj, QEvent* event)
{
    QPushButton *pButton = qobject_cast<QPushButton*>(obj);
    if(pButton)
    {
        QString strName = pButton->property("name").toString();
        if(strName == "historicalDataBtn")
        {
            if(event->type() == QEvent::Enter)
            {
                pButton->setText(QString::fromWCharArray(L"实时数据"));
                return true;
            }
            else if(event->type() == QEvent::Leave)
            {
                pButton->setText(QString::fromWCharArray(L"历史数据"));
                return true;
            }
        }
    }

    return QObject::eventFilter(obj, event);
}

void DateTimeWidget::initialize()
{
    ui->btnHistoricalData->setProperty("name", "historicalDataBtn");
    ui->btnHistoricalData->installEventFilter(this);

    connect(ui->btnDate, SIGNAL(clicked()), this, SLOT(onBtnClicked_date()));
    connect(ui->btnHistoricalData, SIGNAL(clicked()), this, SLOT(onBtnClicked_historicalData()));

    //int nMinUnit = 60 * 1000; //以毫秒为最小单位
    //ui->btnRange_1m->setProperty("range", 1*nMinUnit);
    ui->btnRange_1m->setProperty("timeUnit", TU_Minute_1);
    connect(ui->btnRange_1m, SIGNAL(clicked()), this, SLOT(onBtnClicked_range()));
    //ui->btnRange_3m->setProperty("range", 3*nMinUnit);
    ui->btnRange_3m->setProperty("timeUnit", TU_Minute_3);
    connect(ui->btnRange_3m, SIGNAL(clicked()), this, SLOT(onBtnClicked_range()));
    //ui->btnRange_5m->setProperty("range", 5*nMinUnit);
    ui->btnRange_5m->setProperty("timeUnit", TU_Minute_5);
    connect(ui->btnRange_5m, SIGNAL(clicked()), this, SLOT(onBtnClicked_range()));
    //ui->btnRange_10m->setProperty("range", 10*nMinUnit);
    ui->btnRange_10m->setProperty("timeUnit", TU_Minute_10);
    connect(ui->btnRange_10m, SIGNAL(clicked()), this, SLOT(onBtnClicked_range()));
    //ui->btnRange_30m->setProperty("range", 30*nMinUnit);
    ui->btnRange_30m->setProperty("timeUnit", TU_Minute_30);
    connect(ui->btnRange_30m, SIGNAL(clicked()), this, SLOT(onBtnClicked_range()));
    //ui->btnRange_1h->setProperty("range", 1*60*nMinUnit);
    ui->btnRange_1h->setProperty("timeUnit", TU_Hour);
    connect(ui->btnRange_1h, SIGNAL(clicked()), this, SLOT(onBtnClicked_range()));
    //ui->btnRange_1d->setProperty("range", 24*60*nMinUnit);
    ui->btnRange_1d->setProperty("timeUnit", TU_Day);
    connect(ui->btnRange_1d, SIGNAL(clicked()), this, SLOT(onBtnClicked_range()));
    //ui->btnRange_30d->setProperty("range", 30*24*60*nMinUnit);
    ui->btnRange_30d->setProperty("timeUnit", TU_Month);
    connect(ui->btnRange_30d, SIGNAL(clicked()), this, SLOT(onBtnClicked_range()));

    connect(ui->btnRangeSelect1, SIGNAL(clicked()), this, SLOT(onBtnClicked_rangeSelect()));
    connect(ui->btnRangeSelect2, SIGNAL(clicked()), this, SLOT(onBtnClicked_rangeSelect()));

    QString strCurrentDate = QDate::currentDate().toString("yyyy/MM/dd");
    ui->btnDate->setText(strCurrentDate);
    QString strCurrentTime = QTime::currentTime().toString("HH:mm:ss");
    ui->label_time->setText(strCurrentTime);

    m_pTimer_Range = new QTimer(this);
    connect(m_pTimer_Range, SIGNAL(timeout()), this, SLOT(onTimeout_rangeSelect()));
    // m_pTimer_RealTime = new QTimer(this);
    // connect(m_pTimer_RealTime, SIGNAL(timeout()), this, SLOT(onTimeout_realTime()));
    // m_pTimer_RealTime->start(1000);

    m_pSelectionPanel = new DateTimeSelectionPanel(this);
    connect(m_pSelectionPanel, SIGNAL(selectResults(QDateTime)), this, SLOT(onDateTimeSelectResults(QDateTime)));
}

void DateTimeWidget::setRange(TimeUnit unit)
{
    QString strName = g_timeUnit.value(unit);
    ui->btnRangeSelect1->setText(strName);
    QChar c = strName.back();
    if(c == 'm')
        ui->btnRangeSelect2->setText("Minute Chart Range");
    else if(c == 'h')
        ui->btnRangeSelect2->setText("Hour Chart Range");
    else if(c == 'd')
        ui->btnRangeSelect2->setText("Day Chart Range");
}

void DateTimeWidget::setState(DateTimeWidgetState state)
{
    if(state == realTime)
    {
        //m_pTimer_RealTime->start(1000);
        ui->stacked_time->setCurrentIndex(0);
    }
    else if(state == historical)
    {
        //m_pTimer_RealTime->stop();
        ui->btnHistoricalData->setText(QString::fromWCharArray(L"历史数据"));
        ui->stacked_time->setCurrentIndex(1);
    }
}

void DateTimeWidget::setDateTime(const QDateTime& dateTime)
{
    QString strTime = dateTime.time().toString("HH:mm:ss");
    ui->label_time->setText(strTime);
    //if(dateTime.time() == QTime(0,0,0))
    {
        QString strDate = dateTime.date().toString("yyyy/MM/dd");
        ui->btnDate->setText(strDate);
    }
}

void DateTimeWidget::onBtnClicked_historicalData()
{
    setState(realTime);
    emit viewRealTimeData();
}
void DateTimeWidget::onBtnClicked_realTimeData()
{

}

void DateTimeWidget::onBtnClicked_date()
{
    emit showMask();

    QPoint originPoint = ui->btnDate->mapToGlobal(QPoint(0, 0));
    int nX = originPoint.x() + (ui->btnDate->width() - m_pSelectionPanel->width());
    int nY = originPoint.y() + ui->btnDate->height();
    m_pSelectionPanel->move(nX, nY);
    m_pSelectionPanel->show_();
}

void DateTimeWidget::onBtnClicked_rangeSelect()
{
    ui->stacked_range->setCurrentIndex(1);
    m_pTimer_Range->start(5000);
}

void DateTimeWidget::onBtnClicked_range()
{
    QPushButton* pBtn = qobject_cast<QPushButton*>(sender());
    if(!pBtn)
        return;

    TimeUnit unit = (TimeUnit)pBtn->property("timeUnit").toInt();
    setRange(unit);
    emit timeRangeChanged(unit);

    ui->stacked_range->setCurrentIndex(0);
    if(m_pTimer_Range->isActive())
        m_pTimer_Range->stop();
}

void DateTimeWidget::onTimeout_rangeSelect()
{
    ui->stacked_range->setCurrentIndex(0);
}

void DateTimeWidget::onTimeout_realTime()
{
    QString strCurrentTime = QTime::currentTime().toString("HH:mm:ss");
    ui->label_time->setText(strCurrentTime);
    if(QTime::currentTime() == QTime(0,0,0))
    {
        QString strCurrentDate = QDate::currentDate().toString("yyyy/MM/dd");
        ui->btnDate->setText(strCurrentDate);
    }
}

void DateTimeWidget::onDateTimeSelectResults(QDateTime dateTime)
{
    if(dateTime.date() == QDate::currentDate() && ui->stacked_time->currentIndex() == 0)
    {
        emit hideMask();
        return;
    }

    setState(historical);
    dateTime.setTime(QTime::currentTime());
    emit viewHistoricalData(dateTime);
}
