#include "game-const.hpp"
#include "game-data.hpp"
#include "yaml-cpp/yaml.h"
#include "../resize.hpp"
namespace Game {
struct config_t {
  struct path_t {
    utf8 screenshots = {}; ///< расположение папки скринов
    utf8 data = {}; ///< путь к data.zip
    utf8 font = {}; ///< путь к файлу шритфа
  } path = {}; ///< пути к файлам
  // TODO sound
  // TODO input
  // TODO game
  void load(CN(a83::Str) fname) {
    a83::log("loading config.yml");
    YAML::Node file = YAML::LoadFile(fname);
    auto node = file["path"];
    IFERROR( !node, "Game.config.load: !node.path");
    path.screenshots = sconv<utf8>(node["screenshots"].as<std::string>());
    path.data = sconv<utf8>(node["data"].as<std::string>());
    path.font = sconv<utf8>(node["font"].as<std::string>());
    node = file["graphic"];
    IFERROR( !node, "Game.config.load: !node.graphic");
    set_flag_if(node["vsync"].as<bool>(), Game::status, GF_VSYNC);
    // graphic.fskip uint TODO
    set_flag_if(node["high"].as<bool>(), Game::status, GF_HIGH_DETAILED);
    set_flag_if(node["light"].as<bool>(), Game::status, GF_DRAW_LIGHT);
    set_flag_if(node["haze"].as<bool>(), Game::status, GF_DRAW_HAZE);
    set_flag_if(node["blink"].as<bool>(), Game::status, GF_BLINK_OPTMZ);
    set_flag_if(node["fullscr"].as<bool>(), Game::status, GF_FULLSCREEN);
    wnd.mode = ResizeMode(node["resize"].as<int>());
  } // load
  //! сохранить настройки
  void save(CN(a83::Str) fname) {
    YAML::Node file = YAML::LoadFile(fname);
    auto node = file["graphic"];
    node["vsync"] = bool(Game::status & GF_VSYNC);
    // graphic.fskip uint TODO
    node["high"] = bool(Game::status & GF_HIGH_DETAILED);
    node["light"] = bool(Game::status & GF_DRAW_LIGHT);
    node["haze"] = bool(Game::status & GF_DRAW_HAZE);
    node["blink"] = bool(Game::status & GF_BLINK_OPTMZ);
    node["fullscr"] = bool(Game::status & GF_FULLSCREEN);
    node["resize"] = int(wnd.mode);
    // перезапись файла
    std::ofstream fout(fname);
    fout << file;
  }
} config = {}; ///< настройки игры
} // game ns