#include "Store/Store.h"

#include <iomanip>
#include <iostream>

Store::Store(std::vector<std::unique_ptr<Cargo>>&& cargos)
    : _seed(_rd()), _cargoes(std::move(cargos)) {}

std::random_device Store::_rd{};

void Store::nextDay() {
    std::uniform_int_distribution<int> dist(-50, 50);
    for (auto& cargo : _cargoes) {
        cargo->amount += dist(_seed);
    }
}

size_t Store::getTotalPrice() const {
    size_t value{0};
    for (const auto& cargo : _cargoes) {
        value += cargo->getPrice();
    }

    return value;
}

std::vector<std::string> Store::getCargosName() const {
    std::vector<std::string> names;
    for (const auto& cargo : _cargoes) {
        names.push_back(cargo->name());
    }

    return names;
}

// Shame on me, I copy-past it. But remember DRY!!
// This is only to speed up process of creating exercises, sorry :)
void Store::printCargo() const {
    std::cout << "|----------------|----------------|----------------|\n";
    std::cout << "|      NAME      "
              << "|     AMOUNT     "
              << "|     PRICE      |" << '\n';
    std::cout << "|----------------|----------------|----------------|\n";
    for (const auto& cargo : _cargoes) {
        std::cout << "|" << std::setw(15) << std::left << cargo->name() << " | " << std::setw(15) << cargo->amount << "| " << std::setw(15) << cargo->getPrice() << "|\n";
    }
    std::cout << "|----------------|----------------|----------------|\n";
}
