// Copyright (c) 2023 Dominic Masters
// 
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#include "VNSceneTool.hpp"

using namespace Dawn;

std::vector<std::string> VNSceneTool::getRequiredFlags() {
  return { "input", "output" };
}

std::map<std::string, std::string> VNSceneTool::getOptionalFlags() {
  return std::map<std::string, std::string>();
}

int32_t VNSceneTool::start() {
  File input = File(flags["input"]);
  if(!input.exists()) {
    std::cout << "Input file " + input.filename + " does not exist!" << std::endl;
    return 1;
  }

  std::string data;
  if(!input.readString(&data)) {
    std::cout << "Failed to read input file!" << std::endl;
    return 1;
  }

  auto xml = Xml::load(data);
  std::string error;
  struct VNScene scene;
  auto result = ((VNSceneParser()).parse(&xml, &scene, &error));
  if(result != 0) {
    std::cout << "Failed to parse scene: " << error << std::endl;
    return result;
  }

  // Generate output
  std::vector<std::string> outputData;
  VNSceneGen::generate(&outputData, &scene, "");

  // Load output file from name and type
  File outputFile = File(flags["output"] + "/vnscenes/" + scene.scene.name +  ".hpp");
  if(!outputFile.mkdirp()) {
    std::cout << "Failed to create output directory!" << std::endl;
    return 1;
  }

  // Combine vector into single string
  std::string outputDataStr = "";
  auto it = outputData.begin();
  while(it != outputData.end()) {
    outputDataStr += *it + "\n";
    ++it; 
  }
  
  if(!outputFile.writeString(outputDataStr)) {
    std::cout << "Failed to write output file!" << std::endl;
    return 1;
  }

  return 0;
}