#include "Ship/ShipJsonBuilder.h"

#include "Ship/Ship.h"

#include <errno.h>
#include <fstream>
#include <iostream>

ShipJsonBuilder::ShipJsonBuilder(const std::filesystem::path& path) {
    parseFile(path);
}

std::unique_ptr<Ship> ShipJsonBuilder::buildBrig(Time* time, const std::string& name) {
    return setTime(time)
        .setName(name)
        .setCapacity(_shipsData["brige"]["capacity"])
        .setCrew(_shipsData["brige"]["crew"])
        .setArmor(_shipsData["brige"]["armor"])
        .setDurability(_shipsData["brige"]["durability"])
        .build();
}
std::unique_ptr<Ship> ShipJsonBuilder::buildFrigate(Time* time, const std::string& name) {
    return setTime(time)
        .setName(name)
        .setCapacity(_shipsData["frigate"]["capacity"])
        .setCrew(_shipsData["frigate"]["crew"])
        .setArmor(_shipsData["frigate"]["armor"])
        .setDurability(_shipsData["frigate"]["durability"])
        .build();
}
std::unique_ptr<Ship> ShipJsonBuilder::buildGaleon(Time* time, const std::string& name) {
    return setTime(time)
        .setName(name)
        .setCapacity(_shipsData["galeon"]["capacity"])
        .setCrew(_shipsData["galeon"]["crew"])
        .setArmor(_shipsData["galeon"]["armor"])
        .setDurability(_shipsData["galeon"]["durability"])
        .build();
}

void ShipJsonBuilder::parseFile(const std::filesystem::path& path) {
    std::ifstream file(path);
    if (!file.is_open()) {
        std::cout << "Can't open a file! erno: " << strerror(errno) << '\n';
        abort();
    }
    _shipsData = json::parse(file);
}