#include "sdayview.h"

#include <QPaintEvent>

#include <QPainter>
#include <QPainterPath>

#include "formcalendarview.h"

SDayView::SDayView(QWidget *parent) : QWidget(parent)
{
    m_nDay = 0;
    m_bSelected = false;
    m_nIndexRow = -1;
    m_nIndexColumn = -1;
    m_bCurrentMonth = false;
}

SDayView::~SDayView()
{

}

void SDayView::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    QSize nSize = size();
    QDate today = QDate::currentDate();

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

    QColor colorPen(200, 200, 200);

    int nCenterX = nSize.width()/2;
    int nCenterY = nSize.height()/2;

    painter.save();
    painter.setRenderHint(QPainter::Antialiasing, true);

    //Draw Background
    painter.fillRect(rectDisplay, QColor(255, 255, 255));

    if(FormCalendarView::GetDateStart()!=FormCalendarView::GetDateEnd())
    {
        if(m_Date==FormCalendarView::GetDateStart())
        {
            rectDisplay = QRect((nSize.width()-nSize.height()+20)/2, 10, nSize.height()-20, nSize.height()-20);
            rectRange = QRect(nSize.width()/2, nSize.height()*0.2, nSize.width()/2+1, nSize.height()*0.6);
        }
        else if(m_Date==FormCalendarView::GetDateEnd())
        {
            rectRange = QRect(0, nSize.height()*0.2, nSize.width()/2, nSize.height()*0.6);
        }
        else
        {
            rectRange = QRect(0, nSize.height()*0.2, nSize.width(), nSize.height()*0.6);
        }
    }


    painter.setPen(colorPen);

    if(m_bSelected==true)
    {
        //painter.fillRect(rectDisplay, QColor(100, 100, 100));
    }

    //painter.restore();
    //return;


    int nDate = m_Date.toJulianDay();
    int nDateStart = FormCalendarView::GetDateStart().toJulianDay();
    int nDateEnd = FormCalendarView::GetDateEnd().toJulianDay();

    if(nDate >= nDateStart && nDate <= nDateEnd)
    {
        painter.fillRect(rectRange, QColor(214, 224, 229));
    }

    int nRadius = nSize.width();
    if(nSize.height()<nSize.width())
    {
        nRadius = nSize.height();
    }

    nRadius = nRadius*0.8;

    //if(FormCalendarView::GetDateStart()!=FormCalendarView::GetDateEnd())
    {
        if(m_Date==FormCalendarView::GetDateStart() || m_Date==FormCalendarView::GetDateEnd())
        {
            //QRect rectCircle = QRect(nCenterX-30, nCenterY-30 , 60, 60);
            QRect rectCircle = QRect(nCenterX-nRadius/2, nCenterY-nRadius/2 , nRadius, nRadius);
            QColor colorCircle = QColor(39, 102, 120);
            QBrush brush(colorCircle, Qt::SolidPattern);
            QPainterPath path;
            painter.setPen(colorCircle);
            path.addEllipse(rectCircle);
            painter.setBrush(brush);
            painter.drawPath(path);
        }
    }


    QColor colorText = Qt::black;
    QString strDay = QString::number(m_nDay);

    if(m_nDay>0)
    {
        if(m_bCurrentMonth==true)
        {
            colorText = Qt::black;
        }
        else
        {
            colorText = QColor(102, 102, 102);
        }

        if(m_Date.dayOfWeek()==7)
        {
            if(m_bCurrentMonth==true)
            {
                colorText = Qt::red;
            }
        }
        else if(m_Date.dayOfWeek()==6)
        {
            if(m_bCurrentMonth==true)
            {
                colorText = QColor(0, 25, 255);
            }
        }

        if(m_Date==FormCalendarView::GetDateStart() || m_Date==FormCalendarView::GetDateEnd())
        {
            colorText = Qt::white;
        }

        painter.setPen(colorText);

        //QFont fontRoboto("Roboto Medium");
        //QFont fontRoboto("Roboto Regular");
        QFont fontRoboto("Roboto");
        fontRoboto.setPixelSize(20);

        painter.setFont(fontRoboto);
        painter.drawText(rectDisplay, Qt::AlignCenter , strDay);
    }


    if(today==m_Date)
    {
        painter.setPen(Qt::black);
        QRect rectToday = rectDisplay;

        if(nSize.width() > nSize.height())
        {
            rectToday = QRect(nCenterX-20, nCenterY-30, 40, 14);
            painter.setPen(Qt::red);

            QFont fontToday("Roboto");
            fontToday.setPixelSize(12);
            painter.setFont(fontToday);
            painter.drawText(rectToday, Qt::AlignCenter, "TODAY");
        }

        //QRect rectEllipse = rectDisplay;
        //rectEllipse = QRect(nCenterX-nCenterY+nMargins, nMargins, nCenterY*2-nMargins*2, nCenterY*2-nMargins*2);
        //rectEllipse = QRect(nCenterX-4, nCenterY+14, 8, 8);
        //QBrush brush(QColor(0, 0, 0), Qt::SolidPattern);
        //QPainterPath path;
        //path.addEllipse(rectEllipse);
        //painter.setBrush(brush);
        //painter.drawPath(path);
    }

    painter.restore();


}


void SDayView::SetRowColumn(int nRow, int nColumn)
{
    m_nIndexRow = nRow;
    m_nIndexColumn = nColumn;
}

int SDayView::GetIndexRow()
{
    return m_nIndexRow;
}

int SDayView::GetIndexColumn()
{
    return m_nIndexColumn;
}

void SDayView::SetDate(QDate nDate)
{
    m_Date = nDate;
    m_nDay = m_Date.day();
}

void SDayView::SetDate(QDate nDate, bool bCurrentMonth)
{
    SetDate(nDate);
    m_bCurrentMonth = bCurrentMonth;
}

void SDayView::ClearDate()
{
    m_nDay = 0;
    m_Date = QDate(1900, 1, 1);
}

QDate SDayView::GetDate()
{
    return m_Date;
}

int SDayView::GetYear()
{
    return m_Date.year();
}

int SDayView::GetMonth()
{
    return m_Date.month();
}

int SDayView::GetDay()
{
    return m_Date.day();
}

void SDayView::Select()
{
    m_bSelected = true;
}

void SDayView::Unselect()
{
    m_bSelected = false;
}

bool SDayView::IsSelected()
{
    return m_bSelected;
}

void SDayView::mousePressEvent(QMouseEvent *event)
{

}

void SDayView::mouseReleaseEvent(QMouseEvent *event)
{
    Clicked(m_nIndexRow, m_nIndexColumn);
    if(m_bSelected==true)
    {
        Unselect();
    }
    else
    {
        Select();
    }

    update();

}

void SDayView::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    update();
}
