#include "dialogcalendar.h"
#include "ui_dialogcalendar.h"

#include <QShowEvent>

#include "mainwindow.h"

#include <QScreen>
#include <QGuiApplication>
#include <QImage>
#include <QPainter>


DialogCalendar::DialogCalendar(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogCalendar)
{
    setAttribute(Qt::WA_TranslucentBackground);
    //setWindowFlags(Qt::CustomizeWindowHint);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog | Qt::Popup);
    //setWindowFlags(Qt::FramelessWindowHint);

    ui->setupUi(this);

    connect(ui->widget, SIGNAL(SetSearchDateUpdate(int)), parent, SLOT(SetSearchDateUpdate(int)));
    connect(ui->widget, SIGNAL(ExecuteClickDate()), this, SLOT(ExecuteClickDate()));

    ui->verticalLayout->setSpacing(1);

    QStringList strListText;
    /*
    strListText << "Today";
    strListText << "LAST 3 DAYS";
    strListText << "LAST WEEK";
    strListText << "LAST 2 WEEKS";
    strListText << "LAST MONTH";
    strListText << "LAST 6 MONTHS";
    strListText << "LAST YEAR";
    */
    strListText << "Today";
    strListText << "LAST 3 DAYS";
    strListText << "LAST WEEK";
    strListText << "LAST 2 WEEKS";
    strListText << "LAST 3 WEEKS";
    strListText << "LAST MONTH";
    strListText << "LAST 2 MONTH";

    int i=0;
    for(i=0 ; i<7 ; i++)
    {
        SCalendarMenuView* pItem = new SCalendarMenuView(this);
        pItem->SetData(i, strListText[i]);

        ui->verticalLayout->addWidget(pItem);
        connect(pItem, SIGNAL(ExecuteMenu(int)), this, SLOT(ExecuteMenu(int)));

        m_ListMenu.push_back(pItem);
    }

    m_ListMonthName << "January" << "Februry" << "March" << "April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";

    move(0, 0);

}

DialogCalendar::~DialogCalendar()
{
    int i=0;
    for(i=0 ; i<7 ; i++)
    {
        SCalendarMenuView* pItem = m_ListMenu[i];
        if(pItem!=NULL)
        {
            disconnect(pItem, SIGNAL(ExecuteMenu(int)), this, SLOT(ExecuteMenu(int)));
        }

        pItem->deleteLater();

    }

    m_ListMenu.clear();

    delete ui;

    /*
    if(m_pImageBackground!=NULL)
    {
        delete m_pImageBackground;
    }
    */

}

void DialogCalendar::SetCalendarDate(int nYear, int nMonth, int nDay)
{
    m_nYear = nYear;
    m_nMonth = nMonth;

    QString strYear = QString::number(nYear);
    QString strMonth = QString("%1(%2)").arg(m_ListMonthName[nMonth-1]).arg(QString::number(nMonth));

    strMonth = strMonth.left(3);

    ui->label_Year->setText(strYear);
    ui->label_Month->setText(strMonth);

    ui->widget->SetCalendar(nYear, nMonth);


}


void DialogCalendar::ExecuteMenu(int nID)
{
    int i=0;
    for(i=0 ; i<m_ListMenu.size() ; i++)
    {
        SCalendarMenuView* pItem = m_ListMenu[i];
        if(i!=nID)
        {
            if(pItem->IsSelected()==true)
            {
                pItem->Unselect();
                pItem->update();
            }
        }
    }

    QDate date = QDate::currentDate();
    int nYear = date.year();
    int nMonth = date.month();
    SetCalendarDate(nYear, nMonth, 1);

    m_nTypeSearchDuration = nID;


    switch(nID)
    {
    case 0:
        ui->widget->SetToday();
        break;
    case 1:
        ui->widget->SetLast3Days();
        break;
    case 2:
        ui->widget->SetLastWeek();
        break;
    case 3:
        ui->widget->SetLast2Weeks();
        break;
    case 4:
        //ui->widget->SetLastMonth();
        ui->widget->SetLast3Weeks();
        break;
    case 5:
        //ui->widget->SetLast6Months();
        ui->widget->SetLastMonth();
        break;
    case 6:
        //ui->widget->SetLastYear();
        ui->widget->SetLast2Month();
        break;
    case 7:
    default:
        break;

    }



    SetSearchDateUpdate(m_nTypeSearchDuration);
}

void DialogCalendar::on_pushButton_MonthPrev_clicked()
{
    m_nMonth = m_nMonth-1;
    if(m_nMonth<=0)
    {
        m_nMonth = 12;
        m_nYear = m_nYear-1;
    }
    ui->widget->ClearDayState();
    //ui->widget->SetCalendar(m_nYear, m_nMonth);
    SetCalendarDate(m_nYear, m_nMonth, 1);
    ui->widget->UpdateDayState();
}

void DialogCalendar::on_pushButton_MonthNext_clicked()
{
    m_nMonth = m_nMonth+1;
    if(m_nMonth>12)
    {
        m_nMonth = 1;
        m_nYear = m_nYear+1;
    }
    ui->widget->ClearDayState();
    //ui->widget->SetCalendar(m_nYear, m_nMonth);
    SetCalendarDate(m_nYear, m_nMonth, 1);
    ui->widget->UpdateDayState();
}





void DialogCalendar::on_pushButton_OK_clicked()
{
    QDialog::accept();
}

void DialogCalendar::on_pushButton_Cancel_clicked()
{
    QDialog::reject();
}

void DialogCalendar::showEvent(QShowEvent *pEvent)
{

    if(pEvent->type()==QEvent::Show)
    {

    }
    else if(pEvent->type()==QEvent::Hide)
    {

    }
}

void DialogCalendar::SetModeChangeStart()
{
    m_nModeChange = CHANGE_DATE_START;
    ui->widget->SetModeChangeStart();

    SetSearchDateUpdate(SEARCH_USER_DEFINED);
}

void DialogCalendar::SetModeChangeEnd()
{
    m_nModeChange = CHANGE_DATE_END;
    ui->widget->SetModeChangeEnd();

    SetSearchDateUpdate(SEARCH_USER_DEFINED);
}


void DialogCalendar::resizeEvent(QResizeEvent *pEvent)
{
    int nDisplayType = MainWindow::GetCommonData()->GetDisplayType();

    int nFontSize = 22;
    int nFontSizeCancel = 18;
    int nFontMonth = 24;
    int nFontMenu = 18;
    int nFontYear = 20;
    if(nDisplayType==SDISPLAY_1280X1024)
    {
        float fRatioX = 1280.0f / 1920.0f;
        float fRatioY = 1024.0f / 1080.0f;

        nFontMonth = 16;
        nFontSize = 16;
        nFontSizeCancel = 18 * fRatioX;
        nFontMenu = 14;

        ui->frame_background->setGeometry(0*fRatioX, 0*fRatioX, 1920*fRatioX, 1080*fRatioY);
        ui->frame_calendar->setGeometry(300*fRatioX, 270*fRatioX, 1270*fRatioX, 712*fRatioX);
        {
            ui->frame_top->setGeometry(0, 0, 1270*fRatioX, 99*fRatioX);
            {
                ui->label_Month->setGeometry(582*fRatioX-30, 48*fRatioX, 60*fRatioX+60, 27*fRatioX);
                ui->label_Year->setGeometry(592*fRatioX-30, 24*fRatioX, 46*fRatioX+60, 24*fRatioX);
                ui->pushButton_Cancel->setGeometry(60*fRatioX, 30*fRatioX, 111*fRatioX, 33*fRatioX);
                {
                    ui->frame_ImageCancel->setGeometry(0, 5, 17*fRatioX, 33*fRatioX-10);
                    ui->label_Cancel->setGeometry(17*fRatioX, 0, 71*fRatioX, 33*fRatioX);
                }
                ui->pushButton_MonthNext->setGeometry(660*fRatioX, 52*fRatioX, 10*fRatioX, 18*fRatioX);
                {
                    ui->frame_next->setGeometry(0, 0, 10*fRatioX, 18*fRatioX);
                }
                ui->pushButton_MonthPrev->setGeometry(557*fRatioX, 52*fRatioX, 10*fRatioX, 18*fRatioX);
                {
                    ui->frame_prev->setGeometry(0*fRatioX, 0, 10*fRatioX, 18*fRatioX);
                }
                ui->pushButton_OK->setGeometry(1087*fRatioX, 26*fRatioX+5, 162*fRatioX, 48*fRatioX-10);
            }

            ui->verticalLayoutWidget->setGeometry(0*fRatioX, 99*fRatioX, 200*fRatioX, 612*fRatioX);
            ui->widget->setGeometry(200*fRatioX, 99*fRatioX, 1084*fRatioX, 612*fRatioX);

        }
    }
    else if(nDisplayType==SDISPLAY_1920X1080)
    {
        ui->frame_background->setGeometry(0, 0, 1920, 1080);
        ui->frame_calendar->setGeometry(300, 260, 1270, 712);
        {
            ui->frame_top->setGeometry(0, 0, 1270, 99);
            {
                ui->label_Month->setGeometry(582, 48, 60, 27);
                ui->label_Year->setGeometry(592, 24, 46, 24);
                ui->pushButton_Cancel->setGeometry(60, 30, 111, 33);
                {
                    ui->frame_ImageCancel->setGeometry(0, 0, 17, 33);
                    ui->label_Cancel->setGeometry(17, 0, 71, 33);
                }
                ui->pushButton_MonthNext->setGeometry(660, 52, 10, 18);
                {
                    ui->frame_next->setGeometry(0, 0, 10, 18);
                }
                ui->pushButton_MonthPrev->setGeometry(557, 52, 10, 18);
                {
                    ui->frame_prev->setGeometry(0, 0, 10, 18);
                }
                ui->pushButton_OK->setGeometry(1087, 26, 162, 48);
            }

            ui->verticalLayoutWidget->setGeometry(0, 99, 185, 612);
            ui->widget->setGeometry(185, 99, 1084, 612);

        }
    }

    QString strPushButton = QString(
                "QLabel \n"
                 "{ \n"
                 "        font-family: 'Roboto'; \n"
                 "        font-size: %1px; \n"
                 "        font-weight: bold; \n"
                 "        color: #102528; \n"
                 "} \n").arg(nFontSizeCancel);

    ui->label_Cancel->setStyleSheet(strPushButton);


    strPushButton = QString(
    "QPushButton#pushButton_OK \n"
    "{ \n"
    "        qproperty-autoDefault: false; \n"
    "        qproperty-flat: false; \n"
    "        background: #FFFFFF; \n"
    "        border: none; \n"
    "        border-radius: 9px; \n"
    "        font-family: 'Roboto'; \n"
    "        font-size: %1px; \n"
    "        font-weight: bold; \n"
    "}").arg(nFontSizeCancel);
    ui->pushButton_OK->setStyleSheet(strPushButton);

    strPushButton = QString(
    "QLabel#label_Month \n"
    "{ \n"
    "        font-family: 'Roboto'; \n"
    "        font-size: %1px; \n"
    "        font-weight: bold; \n"
    "        color: white; \n"
    "        qproperty-alignment: 'AlignHCenter | AlignVCenter'; \n"
    "}").arg(nFontMonth);
    ui->label_Month->setStyleSheet(strPushButton);

    strPushButton = QString(
    "QLabel#label_Month \n"
    "{ \n"
    "        font-family: 'Roboto'; \n"
    "        font-size: %1px; \n"
    "        font-weight: bold; \n"
    "        color: white; \n"
    "        qproperty-alignment: 'AlignHCenter | AlignVCenter'; \n"
    "}").arg(nFontYear);
    ui->label_Year->setStyleSheet(strPushButton);


    int i=0;
    for(i=0 ; i<7 ; i++)
    {
        SCalendarMenuView* pItemView = m_ListMenu[i];
        pItemView->SetFontSize(nFontMenu);
    }
}

/*
void DialogCalendar::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QSize nSize = size();
    int nPaddingLeft = 0;

    QRect rectDisplay = QRect(nPaddingLeft, 0, nSize.width()-nPaddingLeft, nSize.height());

    QColor colorBackground;

    painter.setRenderHint(QPainter::SmoothPixmapTransform);

    colorBackground = Qt::white;
    if(m_pImageBackground!=NULL)
    {
        //painter.fillRect(QRect(0, 0, nSize.width(), nSize.height()), Qt::black);

        painter.drawImage(rectDisplay, *m_pImageBackground);
        painter.fillRect(QRect(0, 0, nSize.width(), nSize.height()), QColor(0, 0, 0, 128));
        return;
    }
}

*/

void DialogCalendar::SetDateStart(QDate date)
{
    ui->widget->SetDateStart(date);
}

void DialogCalendar::SetDateEnd(QDate date)
{
    ui->widget->SetDateEnd(date);
}


void DialogCalendar::keyReleaseEvent(QKeyEvent *event)
{
    int nKey = event->key();

    SThreadImageSave* pThread = MainWindow::GetCommonData()->GetThreadCaptureImage();

    if(nKey==Qt::Key_F10)
    {
        pThread->MissCapture();
    }
    else if(nKey==Qt::Key_F11)
    {
        pThread->MissCapture();
    }

}


int DialogCalendar::GetTypeSearchDuration()
{
    return m_nTypeSearchDuration;
}

void DialogCalendar::ExecuteClickDate()
{
    m_nTypeSearchDuration = 7;
    ExecuteMenu(m_nTypeSearchDuration);

}
