#include "PagingWidget.h"
#include "ui_PagingWidget.h"
#include <QDebug>
#include "CAppInfo.h"

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

    initPages(7);
}

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

void PagingWidget::setUI(const int nX, const int nY)
{
    setGeometry(nX, nY, g_appInfoPtr->m_fRate*350, g_appInfoPtr->m_fRate*30);
    ui->btn_previous->setGeometry(0, 0, g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30);
    for(int i = 0; i < m_nLimitPage; i++)
    {
        QPushButton* btn = QWidget::findChild<QPushButton*>(QString("btn_page%1").arg(i+1));
        if(btn)
        {
            btn->setGeometry(ui->btn_previous->x() + (ui->btn_previous->width() + g_appInfoPtr->m_fRate*10)*(i+ 1),
                             ui->btn_previous->y(), ui->btn_previous->width(), ui->btn_previous->height());
        }
    }
    ui->btn_next->setGeometry(ui->btn_page7->x() + ui->btn_previous->width() + g_appInfoPtr->m_fRate*10,
                     ui->btn_previous->y(), ui->btn_previous->width(), ui->btn_previous->height());
}

void PagingWidget::setMaxPageSize(int nMaxSize)
{
    initPages(nMaxSize);
}

void PagingWidget::initPages(const int nMaxSize)
{
    m_nMaxPageSize = nMaxSize;
    m_nCurrentPage = 1;
    initPageCtrl(1);
    /*
    m_nCurrentPage = 1;
    if(nMaxSize <= m_nLimitPage)
    {
        bool bSetNextGeometry = false;
        for(int i = 0; i < m_nLimitPage; i++)
        {
            QPushButton* btn = QWidget::findChild<QPushButton*>(QString("btn_page%1").arg(i+1));
            if(btn)
            {
                if(i < nMaxSize)
                {
                    btn->setVisible(true);
                    btn->setText(QString::number(i+1));
                }
                else
                {
                    btn->setVisible(false);
                    if(!bSetNextGeometry)
                    {
                        ui->btn_next->setGeometry(btn->x(), btn->y(),
                                              ui->btn_next->width(), ui->btn_next->height());
                        bSetNextGeometry = true;
                    }
                }
            }
        }
    }
    else
    {
        for(int i = 0; i < m_nLimitPage - 2; i++)
        {
            QPushButton* btn = QWidget::findChild<QPushButton*>(QString("btn_page%1").arg(i+1));
            if(btn)
            {
                btn->setVisible(true);
                btn->setText(QString::number(i+1));
            }
        }
        ui->btn_page6->setText(QString("..."));
        ui->btn_page7->setText(QString::number(nMaxSize));
    }*/
}

void PagingWidget::initPageCtrl(const int nCurPage)
{
    ui->btn_next->setGeometry(ui->btn_page7->x() + ui->btn_previous->width() + g_appInfoPtr->m_fRate*10,
		ui->btn_previous->y(), ui->btn_previous->width(), ui->btn_previous->height());

    if(m_nMaxPageSize <= m_nLimitPage)
    {
        bool bSetNextGeometry = false;
        for(int i = 0; i < m_nLimitPage; i++)
        {
            QPushButton* btn = QWidget::findChild<QPushButton*>(QString("btn_page%1").arg(i+1));
            if(btn)
            {
                if(i < m_nMaxPageSize)
                {
                    btn->setVisible(true);
                    btn->setText(QString::number(i+1));
                }
                else
                {
                    btn->setVisible(false);
                    if(!bSetNextGeometry)
                    {
                        ui->btn_next->setGeometry(btn->x(), btn->y(),
                                              ui->btn_next->width(), ui->btn_next->height());
                        bSetNextGeometry = true;
                    }
                }
            }
        }
    }
    else if(m_nCurrentPage < 6)
    {
        for(int i = 0; i < m_nLimitPage - 2; i++)
        {
            QPushButton* btn = QWidget::findChild<QPushButton*>(QString("btn_page%1").arg(i+1));
            if(btn)
            {
                btn->setVisible(true);
                btn->setText(QString::number(i+1));
            }
        }
		ui->btn_page6->setVisible(true);
		ui->btn_page7->setVisible(true);
        ui->btn_page6->setText(QString("..."));
        ui->btn_page7->setText(QString::number(m_nMaxPageSize));
    }
    else if(m_nMaxPageSize - m_nCurrentPage > 3)
    {
        for(int i = 0; i < m_nLimitPage - 2; i++)
        {
            QPushButton* btn = QWidget::findChild<QPushButton*>(QString("btn_page%1").arg(i+1));
            if(btn)
            {
                btn->setVisible(true);
                btn->setText(QString::number(m_nCurrentPage - 4 + i+1));
            }
        }
		ui->btn_page6->setVisible(true);
		ui->btn_page7->setVisible(true);
        ui->btn_page6->setText(QString("..."));
        ui->btn_page7->setText(QString::number(m_nMaxPageSize));
    }
    else
    {
        for(int i = 0; i < m_nLimitPage; i++)
        {
            QPushButton* btn = QWidget::findChild<QPushButton*>(QString("btn_page%1").arg(i+1));
            if(btn)
            {
                btn->setVisible(true);
                btn->setText(QString::number(m_nMaxPageSize - 7 + i + 1));
            }
        }
    }

    for(int i = 0; i < m_nLimitPage; i++)
    {
        QPushButton* btn = QWidget::findChild<QPushButton*>(QString("btn_page%1").arg(i+1));
        if(btn)
        {
            if(btn->text() != "..." &&
                    btn->text().toInt() == m_nCurrentPage)
            {
                btn->setStyleSheet(QString("QPushButton {\
                                    background:rgba(100, 153, 218,1);\
                                    font-size:%1px;\
                                    font-family:\"Microsoft YaHei\";\
                                    font-weight:600;\
                                    color: rgba(255, 255, 255, 1);\
                                    border-radius:%2px;}").arg((int)(g_appInfoPtr->m_fRate*12)).arg((int)(g_appInfoPtr->m_fRate*4))) ;
            }
            else
            {
                btn->setStyleSheet(QString("QPushButton {\
                                       background:rgba(255, 255, 255,1);\
                                       font-size:%1px;\
                                       font-family:\"Microsoft YaHei\";\
                                       font-weight:400;\
                                       color: rgba(75, 82, 96, 1);\
                                       border: %2px solid rgba(204, 211, 224, 1);\
                                       border-radius:%3px;}").arg((int)(g_appInfoPtr->m_fRate*12))
                                        .arg((int)(g_appInfoPtr->m_fRate)).arg((int)(g_appInfoPtr->m_fRate*4))) ;
            }
        }
    }
    ui->btn_previous->setStyleSheet(QString("QPushButton {\
                           background:rgba(255, 255, 255,1);\
                           font-size:%1px;\
                           font-family:\"Microsoft YaHei\";\
                           font-weight:400;\
                           color: rgba(75, 82, 96, 1);\
                           border: %2px solid rgba(204, 211, 224, 1);\
                           border-radius:%3px;}").arg((int)(g_appInfoPtr->m_fRate*12)).arg((int)(g_appInfoPtr->m_fRate)).arg((int)(g_appInfoPtr->m_fRate*4))) ;
    ui->btn_next->setStyleSheet(QString("QPushButton {\
                          background:rgba(255, 255, 255,1);\
                          font-size:%1px;\
                          font-family:\"Microsoft YaHei\";\
                          font-weight:400;\
                          color: rgba(75, 82, 96, 1);\
                          border: %2px solid rgba(204, 211, 224, 1);\
                          border-radius:%3px;}").arg((int)(g_appInfoPtr->m_fRate*12)).arg((int)(g_appInfoPtr->m_fRate)).arg((int)(g_appInfoPtr->m_fRate*4))) ;
}

void PagingWidget::setCurPage(const int nCurPage)
{
    if(m_nCurrentPage != nCurPage)
    {
        m_nCurrentPage = nCurPage;
        initPageCtrl(m_nCurrentPage);
        qDebug()<<m_nCurrentPage;
        emit pageChanged(m_nCurrentPage);
    }
}


void PagingWidget::on_btn_previous_clicked()
{
    if(m_nCurrentPage > 1)
    {
        setCurPage(m_nCurrentPage - 1);
    }
}

void PagingWidget::on_btn_page1_clicked()
{
    setCurPage(ui->btn_page1->text().toInt());
}

void PagingWidget::on_btn_page2_clicked()
{
    setCurPage(ui->btn_page2->text().toInt());
}

void PagingWidget::on_btn_page3_clicked()
{
    setCurPage(ui->btn_page3->text().toInt());
}

void PagingWidget::on_btn_page4_clicked()
{
    setCurPage(ui->btn_page4->text().toInt());
}

void PagingWidget::on_btn_page5_clicked()
{
    setCurPage(ui->btn_page5->text().toInt());
}

void PagingWidget::on_btn_page6_clicked()
{
    if(ui->btn_page6->text() == "...")
    {
        setCurPage(ui->btn_page5->text().toInt() + 1);
    }
    else
    {
        setCurPage(ui->btn_page6->text().toInt());
    }
}

void PagingWidget::on_btn_page7_clicked()
{
    setCurPage(ui->btn_page7->text().toInt());
}

void PagingWidget::on_btn_next_clicked()
{
    if(m_nCurrentPage < m_nMaxPageSize)
    {
        setCurPage(m_nCurrentPage + 1);
    }
}
