#include "CSVRepository.h"

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

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-plus-plus " + this->_fileName + " > /dev/null 2>&1";
    system(command.c_str());
}