#include "stdafx.h"

#include "xtr/io/CstGraphExporter.h"

namespace xtr::io {

BOOL CstGraphExporter::Export(const CstGraphOptions& exportParams, const CString fileName, const FileType type) {
  options = exportParams;
  std::ofstream file(fileName);
  if (!file.is_open()) {
    return FALSE;
  }
  if (type == FileType::csv) {
    const auto linePutter = [&file, &triplets = this->options.triplets](const Triplet& data) {
      if (triplets) {
        file << data.from << ';' << static_cast<uint32_t>(data.type) << ';' << data.to << '\n';
      } else {
        file << data.from << ';' << data.to << '\n';
      }
    };

    if (options.linkage.has_value()) {
      OutputLinks(options.linkage.value(), linePutter);
    } else {
      OutputLinks(LinkType::formula, linePutter);
      OutputLinks(LinkType::lexDef, linePutter);
      OutputLinks(LinkType::lexTerm, linePutter);
    }
  } else {
    const auto linePutter = [&file, &triplets = this->options.triplets](const Triplet& data) {
      if (triplets) {
        file << data.from << '\t' << static_cast<uint32_t>(data.type) << '\t' << data.to << '\n';
      } else {
        file << data.from << '\t' << data.to << '\n';
      }
    };
    if (options.linkage.has_value()) {
      OutputLinks(options.linkage.value(), linePutter);
    } else {
      OutputLinks(LinkType::formula, linePutter);
      OutputLinks(LinkType::lexDef, linePutter);
      OutputLinks(LinkType::lexTerm, linePutter);
    }
  }

  file.close();
  return TRUE;
}

} // namespace xtr::io