#include "CSVRepository.h"

CSVRepository::CSVRepository(std::string fileName,bool loadOnStart){
    this->_fileName = fileName;
    if(loadOnStart)
        this->loadFromFile();
}

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

void CSVRepository::loadFromFile(){
    std::ifstream file(this->_fileName);
    if(!file.is_open()){
        return;
    }
    
    while(true){
        Trench trench;
        file >> trench;
        if(file.eof()){
            break;
        }
        this->_data.push_back(trench);
    }
    file.close();
}

void CSVRepository::saveToFile(){
    std::ofstream file(this->_fileName);
    if(!file.is_open()){
        return;
    }
    for(auto trench : this->_data){
        file << trench;
    }
    file.close();
}

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