#include "Collector_view.h"
#include "Service.h"
#include <qheaderview.h>
#include <algorithm>
#include <QModelIndex>
#include <tuple>
#include <QMessageBox>

Collector_view::Collector_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);
    _table_item = new QTableWidget{1, 3};
    _table_item->setHorizontalHeaderLabels(QStringList{"User Id", "Date", "Offer"});
    _table_item->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    layout->addWidget(_table_item);
    _bid = new QLineEdit{};
    QIntValidator* _validator = new QIntValidator();
    _validator->setBottom(0);
    _bid->setValidator(_validator);
    layout->addWidget(_bid);
    _bid_button = new QPushButton{"Bid"};
    layout->addWidget(_bid_button);
    setLayout(layout);
    this->setWindowTitle(QString::fromStdString(_user.name()));
    QObject::connect(_combo, &QComboBox::currentTextChanged, this, &Collector_view::populate_table);
    QObject::connect(_table, &QTableWidget::cellClicked, this, &Collector_view::populate_table_item);
    QObject::connect(_bid_button, &QPushButton::clicked, this, &Collector_view::bid_item);
}

void Collector_view::update()
{
    populate_table_item();
    populate_table();
}

void Collector_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 Collector_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))});
    }

}

void Collector_view::bid_item(){
    QModelIndexList selection = _table->selectionModel()->selectedIndexes();
    if (selection.isEmpty())
    {
        return;
    }
    int row = selection.at(0).row();
    std::string name = _table->item(row, 0)->text().toStdString();
    Item item = _subject.find_item(name);
    int bid = _bid->text().toInt();
    if(bid < item.price()){
        QMessageBox::critical(this, "Error", "Invalid input");
        return;
    }
    _subject.bid(name,bid , _user.id(), "2023.06.13");
    _bid->setText("");

}