#include "PageWidget.h"
#include "ui_PageWidget.h"

#include <QtGlobal>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QDebug>
#include <QIntValidator>

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

    QString qss = QString(".QLabel[page=\"true\"] { padding: 1px; }")
            + QString(".QLabel[currentPage=\"true\"] { color: rgb(190, 0, 0);}")
            + QString(".QLabel[page=\"true\"]:hover { color: white; border-radius: 4px; background-color: qlineargradient(spread:reflect, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(53, 121, 238, 255), stop:1 rgba(0, 202, 237, 255));}");
    this->setStyleSheet(qss);
    setBlockSize(blockSize);
    initialize();
    maxPage = 0;
    ui->pageLineEdit->setText(QString::number(1));


}

PageWidget::~PageWidget() {
    delete ui;
    delete pageLabels;
}

bool PageWidget::eventFilter(QObject *watched, QEvent *e) {
    if (e->type() == QEvent::MouseButtonRelease) {
        int page = -1;
        if (watched == ui->previousPageLabel) { page = getCurrentPage() - 1; }
        if (watched == ui->nextPageLabel) { page = getCurrentPage() + 1; }
        for (int i = 0; i < pageLabels->count(); ++i) {
            if (watched == pageLabels->at(i)) {
                page = pageLabels->at(i)->text().toInt();
                break;
            }
        }

        if (-1 != page) {
            setCurrentPage(page, true);
            return true;
        }
    }
    if (watched == ui->pageLineEdit && e->type() == QEvent::KeyRelease) {
        QKeyEvent *ke = static_cast<QKeyEvent *>(e);
        if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
        {
            int page = ui->pageLineEdit->text().toInt();
            if(page > maxPage)
            {
                page = maxPage;
                ui->pageLineEdit->setText(QString::number(page));
            }
            setCurrentPage(page, true);
            return true;
        }
    }
    return QWidget::eventFilter(watched, e);
}

int PageWidget::getBlockSize() const {
    return blockSize;
}

int PageWidget::getMaxPage() const {
    return maxPage;
}

int PageWidget::getCurrentPage() const {
    return currentPage;
}

void PageWidget::setMaxPage(int page) {
    page = qMax(page, 1);

    if (maxPage != page) {
        maxPage = page;
        currentPage = 1;
        updatePageLabels();
    }
}

void PageWidget::setCurrentPage(int page, bool signalEmitted) {
    page = qMax(page, 1);
    page = qMin(page, maxPage);

    if (page != currentPage) {
        currentPage = page;
        updatePageLabels();
        ui->pageLineEdit->setText(QString::number(page));

        if (signalEmitted) {
            emit currentPageChanged(page);
        }
    }
}

QComboBox *PageWidget::getComboBox() const
{

    return ui->comboBox;

}

void PageWidget::setBlockSize(int blockSize) {
    blockSize = qMax(blockSize, 3);
    if (blockSize % 2 == 0) {
        ++blockSize;
    }
    this->blockSize = blockSize;
}


// 分成三个部分, 左...中...右
void PageWidget::initialize() {

    ui->pageLineEdit->installEventFilter(this);
    ui->pageLineEdit->setValidator(new QIntValidator(1, 10000000, this));
    ui->nextPageLabel->setProperty("page", "true");
    ui->previousPageLabel->setProperty("page", "true");
    ui->nextPageLabel->installEventFilter(this);
    ui->previousPageLabel->installEventFilter(this);
    pageLabels = new QList<QLabel *>();

    // 设置布局
    QHBoxLayout *leftLayout = new QHBoxLayout();
    QHBoxLayout *centerLayout = new QHBoxLayout();
    QHBoxLayout *rightLayout = new QHBoxLayout();

    leftLayout->setContentsMargins(0, 0, 0, 0);
    leftLayout->setSpacing(0);
    centerLayout->setContentsMargins(0, 0, 0, 0);
    centerLayout->setSpacing(0);
    rightLayout->setContentsMargins(0, 0, 0, 0);
    rightLayout->setSpacing(0);

    for (int i = 0; i < blockSize * 3-2; ++i) {
        QLabel *label = new QLabel(QString::number(i + 1));
        label->setProperty("page", "true");
        label->installEventFilter(this);
        pageLabels->append(label);
        if (i < 1) {
            leftLayout->addWidget(label);
        } else if (i < blockSize * 2) {
            centerLayout->addWidget(label);
        } else {
            rightLayout->addWidget(label);
        }
    }

    ui->leftPagesWidget->setLayout(leftLayout);
    ui->centerPagesWidget->setLayout(centerLayout);
    ui->rightPagesWidget->setLayout(rightLayout);
    initCombo();
}

void PageWidget::initCombo(){
    ui->comboBox->addItem("10条/页", QVariant(10));  // 显示文本为 "Apple"，值为 1
    ui->comboBox->addItem("50条/页", QVariant(50));
    ui->comboBox->addItem("100条/页", QVariant(100));
    ui->comboBox->addItem("200条/页", QVariant(200));
}

void PageWidget::updatePageLabels() {
    ui->leftSeparateLabel->hide();
    ui->rightSeparateLabel->hide();

    if (maxPage <= blockSize * 3) {
        for (int i = 0; i < pageLabels->count(); i += 1) {
            QLabel *label = pageLabels->at(i);
            if (i < maxPage) {
                label->setText(QString::number(i + 1));
                label->show();
            } else {
                label->hide();
            }

            if (currentPage - 1 == i) {
                label->setProperty("currentPage", "true");
            } else {
                label->setProperty("currentPage", "false");
            }

            label->setStyleSheet("/**/");
        }
        return;
    }

    int c = currentPage;
    int n = blockSize;
    int m = maxPage;
    int centerStartPage = 0;

    if (c >= 1 && c < n + n / 2 + 1) {
        // 显示前 n * 2 个, 后 n 个: 只显示右边的分隔符
        centerStartPage = 1;
        ui->rightSeparateLabel->show();
    } else if (c > m - n - n / 2 && c <= m) {
        // 显示前 n 个, 后 n * 2 个: 只显示左边的分隔符
        centerStartPage = m - n - n ;
        ui->leftSeparateLabel->show();
    } else {
        // 显示[1, n], [c - n/2, c + n/2], [m - n + 1, m]: 两个分隔符都显示
        centerStartPage = c - n;
        ui->rightSeparateLabel->show();
        ui->leftSeparateLabel->show();
    }

    pageLabels->at(0)->setText(QString::number(1));// 前面 n 个
    for (int i = 1; i < 6; ++i) {
        pageLabels->at(i)->setText(QString::number(centerStartPage + i)); // 中间 n 个
    }
    pageLabels->at(6)->setText(QString::number(m));// 后面 n 个

    for (int i = 0; i < pageLabels->count(); ++i) {
        QLabel *label = pageLabels->at(i);
        int page = label->text().toInt();
        if (page == currentPage) {
            label->setProperty("currentPage", "true");
        } else {
            label->setProperty("currentPage", "false");
        }

        label->setStyleSheet("/**/");
        label->show();
    }
}



