#include "PaginationBar.h"
#include <QDebug>
#include <QIntValidator>
#include <QStyle>

PaginationBar::PaginationBar(int totalPages, QWidget *parent)
    : QWidget(parent)
    , totalPages(totalPages)
    , currentPage(1)
{
    layout = new QHBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(5);

    leftArrow = new QPushButton(this);
    leftArrow->setFixedSize(40, 40);
    leftArrow->setStyleSheet(R"(
        QPushButton {
            background: transparent;
            border: none;
            border-radius: 5px;
            padding: 0;
            background-image: url(:/png/res/png/arrowLeft.png);
            background-position: center;
            background-repeat: no-repeat;
        }
        QPushButton:hover {
            background-color: #e5e5e5;
        }
    )");
    connect(leftArrow, &QPushButton::clicked, this, &PaginationBar::goToPreviousPage);

    rightArrow = new QPushButton(this);
    rightArrow->setFixedSize(40, 40);
    rightArrow->setStyleSheet(R"(
        QPushButton {
            background: transparent;
            border: none;
            border-radius: 5px;
            padding: 0;
            background-image: url(:/png/res/png/arrowRight.png);
            background-position: center;
            background-repeat: no-repeat;
        }
        QPushButton:hover {
            background-color: #e5e5e5;
        }
    )");
    connect(rightArrow, &QPushButton::clicked, this, &PaginationBar::goToNextPage);

    layout->addWidget(leftArrow);
    layout->addWidget(rightArrow);
    updateButtons();
}

void PaginationBar::onPageButtonClicked(int page)
{
    currentPage = page;
    emit pageChanged(currentPage);
    updateButtons();
}

void PaginationBar::goToPreviousPage()
{
    if (currentPage > 1) {
        currentPage--;
        emit pageChanged(currentPage);
        updateButtons();
    }
}

void PaginationBar::goToNextPage()
{
    if (currentPage < totalPages) {
        currentPage++;
        emit pageChanged(currentPage);
        updateButtons();
    }
}

void PaginationBar::onEllipsisClicked(QPushButton *ellipsisButton)
{
    if (activeLineEdit) {
        layout->replaceWidget(activeLineEdit, previousEllipsisButton);
        activeLineEdit->deleteLater();
        activeLineEdit = nullptr;
        if (previousEllipsisButton) {
            previousEllipsisButton->show();
        }
    }

    auto *lineEdit = new QLineEdit(this);
    lineEdit->setFixedSize(40, 40);
    lineEdit->setStyleSheet(R"(
        border: 2px solid #DCD174;
        font-family: Inter;
        font-size: 18px;
        font-weight: 400;
        line-height: 31.47px;
        text-align: left;
        background: #00000000;
        color: #13385F;
    )");
    lineEdit->setAlignment(Qt::AlignCenter);
    lineEdit->setValidator(new QIntValidator(1, totalPages, this));
    layout->replaceWidget(ellipsisButton, lineEdit);
    ellipsisButton->hide();

    lineEdit->setFocus();

    activeLineEdit = lineEdit;
    previousEllipsisButton = ellipsisButton;

    connect(lineEdit, &QLineEdit::editingFinished, [this, lineEdit, ellipsisButton]() {
        bool ok;
        int page = lineEdit->text().toInt(&ok);
        if (ok && page >= 1 && page <= totalPages) {
            currentPage = page;
            emit pageChanged(currentPage);
        }

        layout->replaceWidget(lineEdit, ellipsisButton);
        lineEdit->deleteLater();
        activeLineEdit = nullptr;
        ellipsisButton->show();

        updateButtons();
    });
}

void PaginationBar::updateButtons()
{
    if (activeLineEdit) {
        for (auto *button : pageButtons) {
            if (!button->isVisible()) {
                layout->replaceWidget(activeLineEdit, button);
                activeLineEdit->deleteLater();
                activeLineEdit = nullptr;
                button->show();
                break;
            }
        }
    }

    for (auto *button : pageButtons) {
        layout->removeWidget(button);
        button->deleteLater();
    }
    pageButtons.clear();

    if (totalPages <= 7) {
        for (int i = 1; i <= totalPages; ++i) {
            addPageButton(i);
        }
    } else {
        if (currentPage <= 4) {
            for (int i = 1; i <= 5; ++i)
                addPageButton(i);
            addEllipsisButton();
            addPageButton(totalPages);
        } else if (currentPage >= totalPages - 3) {
            addPageButton(1);
            addEllipsisButton();
            for (int i = totalPages - 4; i <= totalPages; ++i)
                addPageButton(i);
        } else {
            addPageButton(1);
            addEllipsisButton();
            for (int i = currentPage - 1; i <= currentPage + 1; ++i)
                addPageButton(i);
            addEllipsisButton();
            addPageButton(totalPages);
        }
    }
}

void PaginationBar::addPageButton(int page)
{
    auto *button = new QPushButton(QString::number(page), this);
    button->setFixedSize(40, 40);
    if (page == currentPage) {
        button->setStyleSheet(R"(
            QPushButton {
                background: #13385f;
                border: none;
                border-radius: 5px;
                color: white;
            }
        )");
    } else {
        button->setStyleSheet(R"(
            QPushButton {
                background: transparent;
                border: none;
                border-radius: 5px;
                color: #13385f;
            }
            QPushButton:hover {
                background-color: #e5e5e5;
            }
        )");
    }
    connect(button, &QPushButton::clicked, [this, page]() { onPageButtonClicked(page); });
    pageButtons.append(button);
    layout->insertWidget(layout->count() - 1, button);
}

void PaginationBar::addEllipsisButton()
{
    auto *button = new QPushButton("...", this);
    button->setFixedSize(40, 40);
    button->setStyleSheet(R"(
        QPushButton {
            background: transparent;
            border: none;
            color: #13385f;
        }
    )");
    connect(button, &QPushButton::clicked, [this, button]() { onEllipsisClicked(button); });
    pageButtons.append(button);
    layout->insertWidget(layout->count() - 1, button);
}

void PaginationBar::setTotalPages(int newTotalPages)
{
    totalPages = newTotalPages;
    currentPage = 1;
    updateButtons();
    //emit pageChanged(currentPage);
}
