#include "trench.h"
#include <string.h>
#include <string>
#include <exception>
#include <stdio.h>
#include <iostream>
#include <fstream>

Trench::Trench(int size, std::string colour, int price, int quantity, std::string photograph){
    this->_size = size;
    this->_colour = colour;
    this->_price = price;
    this->_quantity = quantity;
    this->_photograph=photograph;
}

Trench::~Trench(){
    
}

Trench::Trench(const Trench &trench){
    _size = trench.size();
    _colour = trench.colour();
    _price = trench.price();
    _quantity = trench.quantity();
    _photograph = trench.photograph();
}

Trench& Trench::operator=(const Trench &trench){
    this->_size = trench.size();
    this->_colour=trench.colour();
    this->_price = trench.price();
    this->_quantity = trench.quantity();
    this->_photograph = trench.photograph();
    return *this;
}

int Trench::size() const {
    return this->_size;
}

void Trench::size(int newSize){
    this->_size = newSize;
}

std::string Trench::colour() const {
    return this->_colour;
}

void Trench::colour(std::string newColour){
    this->_colour = newColour;
}

int Trench::price() const {
    return this->_price;
}

void Trench::price(int newPrice){
    this->_price = newPrice;
}

int Trench::quantity() const{
    return this->_quantity;
}

void Trench::quantity(int newQuantity){
    this->_quantity = newQuantity;
}

std::string Trench::photograph() const {
    return this->_photograph;
}

void Trench::photograph(std::string newPhotograph){
    this->_photograph = newPhotograph;
}

bool Trench::operator==(const Trench& t){
    if(this->_size==t.size() && this->_colour == t.colour() && this->_photograph == t.photograph()){
        return true;
    }
    return false;
}

std::ostream& operator<<(std::ostream& os, const Trench& trench){
    os << "Size: " << trench.size() << " | Colour: " << trench.colour() << " | Price: " << trench.price() << " | Quantity: " << trench.quantity() << " | Photograph Link: " << trench.photograph() << std::endl;
    return os;
}

std::ofstream& operator<<(std::ofstream& os, const Trench& trench){
    os << trench.size() << "," << trench.colour() << "," << trench.price() << "," << trench.quantity() << "," << trench.photograph() << std::endl;
    return os;
}

std::ifstream& operator>>(std::ifstream& is, Trench& trench){
    std::string line;
    std::getline(is, line);
    if(line == ""){
        return is;
    }
    std::string delimiter = ",";
    size_t pos = 0;
    std::string token;
    int i = 0;
    while ((pos = line.find(delimiter)) != std::string::npos) {
        token = line.substr(0, pos);
        switch(i){
            case 0:
                trench.size(std::stoi(token));
                break;
            case 1:
                trench.colour(token);
                break;
            case 2:
                trench.price(std::stoi(token));
                break;
            case 3:
                trench.quantity(std::stoi(token));
                break;
        }
        line.erase(0, pos + delimiter.length());
        i++;
    }
    trench.photograph(line);
    return is;
}


