#include "formcalendarview.h"
#include "ui_formcalendarview.h"



#include <QResizeEvent>

#include <QLabel>

#include "dialogcalendar.h"

#include "commondata.h"

QDate FormCalendarView::m_DateStart;
QDate FormCalendarView::m_DateEnd;

FormCalendarView::FormCalendarView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormCalendarView)
{
    ui->setupUi(this);

    m_nMode = CHANGE_DATE_START;

    m_DateStart = QDate::currentDate();
    m_DateEnd = QDate::currentDate();

    ui->gridLayout->setSpacing(0);
    ui->horizontalLayout->setSpacing(0);

    int i=0;
    int j=0;

    m_pViewDays = new SDayView**[7];
    for(i=0 ; i<6 ; i++)
    {
        m_pViewDays[i] = new SDayView*[6];

        for(j=0 ; j<7 ; j++)
        {
            m_pViewDays[i][j] = new SDayView(this);
            m_pViewDays[i][j]->SetRowColumn(i, j);
            //m_pViewDays[i][j]->setGeometry(0, 0, 100, 30);

            m_pViewDays[i][j]->setStyleSheet("QWidget{ background: none; border: none;} ");

            connect(m_pViewDays[i][j], SIGNAL(Clicked(int, int)), this, SLOT(Clicked(int, int)));
        }

    }

    for(i=0 ; i<6 ; i++)
    {
        for(j=0 ; j<7 ; j++)
        {
            ui->gridLayout->addWidget(m_pViewDays[i][j], i, j);
        }
    }


    QStringList strListDayName;
    strListDayName << "SUN";
    strListDayName << "MON";
    strListDayName << "TUE";
    strListDayName << "WED";
    strListDayName << "THU";
    strListDayName << "FRI";
    strListDayName << "SAT";

    ui->horizontalLayout->addSpacing(50);

    for(i=0 ; i<7 ; i++)
    {
        QLabel* pLabel = new QLabel(this);
        QString strDayName = strListDayName[i];
        pLabel->setText(strDayName);

        if(i==0)
        {
            pLabel->setStyleSheet("QLabel{ background: #D6E0E5; qproperty-alignment: AlignCenter; color: #FF0000;}");
        }
        else if(i==6)
        {
            pLabel->setStyleSheet("QLabel{ background: #D6E0E5; qproperty-alignment: AlignCenter; color: #0019FF;}");
        }
        else
        {
            pLabel->setStyleSheet("QLabel{ background: #D6E0E5; qproperty-alignment: AlignCenter;}");
        }


        ui->horizontalLayout->addWidget(pLabel);
    }

    ui->horizontalLayout->addSpacing(50);


}

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


void FormCalendarView::resizeEvent(QResizeEvent *event)
{
    QSize nSize = event->size();
    int nHeightTitle = 66;

    ui->verticalLayoutWidget->setGeometry(0, 0, nSize.width(), nSize.height());

    QSize nSizeDayOfWeek(nSize.width(), nHeightTitle);
    ui->frame_DayOfWeek->setMaximumSize(nSizeDayOfWeek);


    ui->gridLayoutWidget->setGeometry(50, 0, nSize.width()-100, nSize.height()-nHeightTitle);


    ui->horizontalLayoutWidget->setGeometry(0, 0, nSize.width(), nHeightTitle);


}


void FormCalendarView::SetCalendar(int nYear, int nMonth)
{
      int nStartDay = 0;
      int nMonthDay = 0;
      int nPrevMonthDay = 0;
      int nPrevMonth = nMonth-1;
      int nPrevYear = nYear;
      int nNextMonth = nMonth+1;
      int nNextYear = nYear;


      QDate date(nYear, nMonth, 1);

      nMonthDay = date.daysInMonth();
      nStartDay = date.dayOfWeek();


      if(nPrevMonth<=0)
      {
          nPrevYear = nYear - 1;
          nPrevMonth = 12;
      }

      if(nNextMonth>12)
      {
          nNextYear = nYear+1;
          nNextMonth = 1;
      }


      QDate datePrevMonth(nPrevYear, nPrevMonth, 1);
      nPrevMonthDay = datePrevMonth.daysInMonth();


      int i=0;
      int j=0;

      int nIndexDisplay = 0;
      int nDay = 1;
      int nIndexNextMonthDay = 1;
      for(i=0 ; i<6 ; i++)
      {
          for(j=0 ; j<7 ; j++)
          {
              if(nIndexDisplay>=nStartDay && nDay<=nMonthDay)
              {
                  m_pViewDays[i][j]->SetDate(QDate(nYear, nMonth, nDay), true);
                  nDay++;
              }
              else
              {
                  if(i==0)
                  {
                      int nPrevDay = nPrevMonthDay - nStartDay + (j+1);
                      m_pViewDays[i][j]->SetDate(QDate(nPrevYear, nPrevMonth, nPrevDay), false);
                  }
                  else
                  {
                      m_pViewDays[i][j]->SetDate(QDate(nNextYear, nNextMonth, nIndexNextMonthDay), false);
                      nIndexNextMonthDay++;
                  }
                  //m_pViewDays[i][j]->ClearDate();
              }
              nIndexDisplay++;
          }
      }

      ui->gridLayout->update();
      update();


}

void FormCalendarView::SetToday()
{
    ClearDayState();

    m_DateEnd = QDate::currentDate();

    m_DateStart = m_DateEnd;

    UpdateDayState();
}


void FormCalendarView::SetLast3Days()
{
    ClearDayState();

    m_DateEnd = QDate::currentDate();

    m_DateStart = m_DateEnd.addDays(-2);

    UpdateDayState();
}

void FormCalendarView::SetLastWeek()
{
    ClearDayState();

    m_DateEnd = QDate::currentDate();

    m_DateStart = m_DateEnd.addDays(-6);

    UpdateDayState();
}

void FormCalendarView::SetLast2Weeks()
{
    ClearDayState();

    m_DateEnd = QDate::currentDate();

    m_DateStart = m_DateEnd.addDays(-13);

    UpdateDayState();
}

void FormCalendarView::SetLast3Weeks()
{
    ClearDayState();

    m_DateEnd = QDate::currentDate();

    m_DateStart = m_DateEnd.addDays(-20);

    UpdateDayState();
}

void FormCalendarView::SetLastMonth()
{
    ClearDayState();

    m_DateEnd = QDate::currentDate();

    m_DateStart = m_DateEnd.addMonths(-1);
    m_DateStart = m_DateStart.addDays(1);

    UpdateDayState();
}

void FormCalendarView::SetLast2Month()
{
    ClearDayState();

    m_DateEnd = QDate::currentDate();

    m_DateStart = m_DateEnd.addMonths(-2);
    m_DateStart = m_DateStart.addDays(1);

    UpdateDayState();
}

void FormCalendarView::SetLast6Months()
{
    ClearDayState();

    m_DateEnd = QDate::currentDate();

    m_DateStart = m_DateEnd.addMonths(-6);
    m_DateStart = m_DateStart.addDays(1);

    UpdateDayState();
}

void FormCalendarView::SetLastYear()
{
    ClearDayState();

    m_DateEnd = QDate::currentDate();

    m_DateStart = m_DateEnd.addYears(-1);
    m_DateStart = m_DateStart.addDays(1);

    UpdateDayState();
}


void FormCalendarView::UpdateDayState()
{
    int i=0;
    int j=0;

    for(i=0 ; i<6 ; i++)
    {
        for(j=0 ; j<7 ; j++)
        {
            if(m_pViewDays[i][j]->GetDate()==m_DateStart)
            {
                m_pViewDays[i][j]->Select();
                //m_pViewDays[i][j]->update();
            }
            if(m_pViewDays[i][j]->GetDate()==m_DateEnd)
            {
                m_pViewDays[i][j]->Select();
                //m_pViewDays[i][j]->update();
            }
        }
    }

    update();
}

void FormCalendarView::ClearDayState()
{
    int i=0;
    int j=0;

    for(i=0 ; i<6 ; i++)
    {
        for(j=0 ; j<7 ; j++)
        {
            if(m_pViewDays[i][j]->IsSelected()==true)
            {
                m_pViewDays[i][j]->Unselect();
                m_pViewDays[i][j]->update();
            }
        }
    }
    update();
}

QDate FormCalendarView::GetDateStart()
{
    return m_DateStart;
}

QDate FormCalendarView::GetDateEnd()
{
    return m_DateEnd;
}

void FormCalendarView::SetDateStart(QDate date)
{
    m_DateStart = date;
}

void FormCalendarView::SetDateEnd(QDate date)
{
    m_DateEnd = date;
}

void FormCalendarView::Clicked(int nIndexRow, int nIndexColumn)
{
    if(m_nMode==CHANGE_DATE_START)
    {
        QDate date = m_pViewDays[nIndexRow][nIndexColumn]->GetDate();
        m_DateStart = date;
        m_DateEnd = date;
        m_nMode = CHANGE_DATE_END;
    }
    else if(m_nMode==CHANGE_DATE_END)
    {
        QDate date = m_pViewDays[nIndexRow][nIndexColumn]->GetDate();
        m_DateEnd = date;
        m_nMode = CHANGE_DATE_START;
    }

    if(m_DateStart>m_DateEnd)
    {
        QDate dateTmp;
        dateTmp = m_DateStart;
        m_DateStart = m_DateEnd;
        m_DateEnd = dateTmp;
    }

    UpdateDayState();

    SetSearchDateUpdate(SEARCH_USER_DEFINED);
    ExecuteClickDate();
}

void FormCalendarView::SetModeChangeStart()
{
    m_nMode = CHANGE_DATE_START;
}

void FormCalendarView::SetModeChangeEnd()
{
    m_nMode = CHANGE_DATE_END;
}


