#include "Admin_view.h"
#include "Service.h"
#include <qheaderview.h>
#include<QObject>
#include <QMessageBox>
#include <algorithm>

Admin_view::Admin_view(Service& subject, User& user, QWidget* parent) : _subject{subject} , _user{user}, QWidget{parent}
{
    _subject.attach(this);
    _combo = new QComboBox{};
    _combo->addItems(QStringList{QString::fromStdString("All"), QString::fromStdString("Wood"), QString::fromStdString("Artifacts"), QString::fromStdString("Painting")});
    _combo->setCurrentIndex(0);
    _table = new QTableWidget{1, 3};
    _table->setHorizontalHeaderLabels(QStringList{"Name", "Category", "Current price"});
    _table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    populate_table();
    QVBoxLayout* layout = new QVBoxLayout{};
    layout->addWidget(_combo);
    layout->addWidget(_table);
    _name = new QLineEdit{};
    _category = new QLineEdit{};
    _price = new QLineEdit{};
    _validator = new QIntValidator();
    _validator->setBottom(0);
    _price->setValidator(_validator);
    _add_button = new QPushButton{"Add"};
    layout->addWidget(_name);
    layout->addWidget(_category);
    layout->addWidget(_price);
    layout->addWidget(_add_button);
    _table_item = new QTableWidget{1, 3};
    _table_item->setHorizontalHeaderLabels(QStringList{"User Id", "Date", "Offer"});
    _table_item->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    layout->addWidget(_table_item);
    setLayout(layout);
    this->setWindowTitle(QString::fromStdString(_user.name()));
    QObject::connect(_add_button, &QPushButton::clicked, [&]() {
        std::string name = _name->text().toStdString();
        std::string category = _category->text().toStdString();
        int price = _price->text().toInt();
        if (name == "" || category == "" || price == 0)
        {
            QMessageBox::critical(this, "Error", "Invalid input");
            return;
        }
        try
        {
            _subject.add(name, category, price);
        }
        catch (std::exception& e)
        {
            QMessageBox::critical(this, "Error", e.what());
        }
        });
    QObject::connect(_combo, &QComboBox::currentTextChanged, this, &Admin_view::populate_table);
    QObject::connect(_table, &QTableWidget::itemSelectionChanged, this, &Admin_view::populate_table_item);
}

void Admin_view::update()
{
    populate_table();
}

void Admin_view::populate_table()
{   
    std::string _category = _combo->currentText().toStdString();
    _table->setRowCount(0);
    std::vector<Item> items = _subject.get_items();
    std::sort(items.begin(), items.end(), [](const Item& a, const Item& b) {return a.price() < b.price(); });
    for (auto item : items)
    {
        if(_category == "All" || _category == item.category())
        {
            int row = _table->rowCount();
            _table->insertRow(row);
            _table->setItem(row, 0, new QTableWidgetItem{QString::fromStdString(item.name())});
            _table->setItem(row, 1, new QTableWidgetItem{QString::fromStdString(item.category())});
            _table->setItem(row, 2, new QTableWidgetItem{QString::number(item.price())});
        }
    }
}



void Admin_view::populate_table_item(){
    //QMessageBox::critical(this, "Error", "Invalid input");
    QModelIndexList selection = _table->selectionModel()->selectedIndexes();
    if (selection.isEmpty())
    {
        _table_item->setRowCount(0);
        return;
    }
    int row = selection.at(0).row();
    std::string name = _table->item(row, 0)->text().toStdString();
    Item item = _subject.find_item(name);
    _table_item->setRowCount(0);
    auto offers = item.offers();  
    std::sort(offers.begin(), offers.end(), [](const auto& a, const auto& b) {return std::get<1>(a) > std::get<1>(b); });
    for( auto offer : offers){
            int row = _table_item->rowCount();
            _table_item->insertRow(row);
            _table_item->setItem(row, 0, new QTableWidgetItem{QString::number(std::get<0>(offer))});
            _table_item->setItem(row, 1, new QTableWidgetItem{QString::fromStdString(std::get<1>(offer))});
            _table_item->setItem(row, 2, new QTableWidgetItem{QString::number(std::get<2>(offer))});
    }

}