#include "HTMLRepository.h"


HTMLRepository::HTMLRepository(std::string fileName){
    this->_fileName = fileName;
}

HTMLRepository::~HTMLRepository(){
    this->saveToFile();
}

void HTMLRepository::saveToFile(){
    std::ofstream file(this->_fileName);
    if(!file.is_open()){
        return;
    }
    file << "<!DOCTYPE html><html><head><title>Shopping list</title></head><body><table border=\"1\"><tr><td>Size</td><td>Colour</td><td>Price</td><td>Quantity</td><td>Photograph Link</td><td>Photo</td></tr>";
    for(auto trench : this->_data){
        file << "<tr><td>" << trench.size() << "</td><td>" << trench.colour() << "</td><td>" << trench.price() << "</td><td>" << trench.quantity() << "</td><td>" << "<a href=\"" << trench.photograph() << "\">Link</a>  " << "</td><td>" << "<img src=\"" << trench.photograph() << "\" width=100 height=100>" << "</td></tr>";
    }
    file << "</table></body></html>";
    file.close();
}

void HTMLRepository::open(){
    std::string command = "start " + this->_fileName;
    system(command.c_str());
}