// This file is part of OpenMVG, an Open Multiple View Geometry C++ library.

// Copyright (c) 2015 Pierre MOULON.

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "openMVG/sfm/sfm_data.hpp"
#include "openMVG/sfm/sfm_data_io.hpp"
#include "openMVG/system/logger.hpp"

#include "third_party/cmdLine/cmdLine.h"
#include "third_party/stlplus3/filesystemSimplified/file_system.hpp"

#include <string>

using namespace openMVG;
using namespace openMVG::sfm;

// Convert from a SfM_Data format to another
int main(int argc, char **argv)
{
  CmdLine cmd;

  std::string
    sSfM_Data_Filename_In,
    sSfM_Data_Filename_Out;

  cmd.add(make_option('i', sSfM_Data_Filename_In, "input_file"));
  cmd.add(make_switch('V', "VIEWS"));
  cmd.add(make_switch('I', "INTRINSICS"));
  cmd.add(make_switch('E', "EXTRINSICS"));
  cmd.add(make_switch('S', "STRUCTURE"));
  cmd.add(make_switch('C', "CONTROL_POINTS"));
  cmd.add(make_option('o', sSfM_Data_Filename_Out, "output_file"));

  try {
    if (argc == 1) throw std::string("Invalid command line parameter.");
    cmd.process(argc, argv);
  } catch (const std::string& s) {
    OPENMVG_LOG_INFO << "Usage: " << argv[0] << '\n'
      << "[-i|--input_file] path to the input SfM_Data scene\n"
      << "[-o|--output_file] path to the output SfM_Data scene\n"
      << "\t .json, .bin, .xml, .ply, .baf\n"
      << "\n[Options to export partial data (by default all data are exported)]\n"
      << "\nUsable for json/bin/xml format\n"
      << "[-V|--VIEWS] export views\n"
      << "[-I|--INTRINSICS] export intrinsics\n"
      << "[-E|--EXTRINSICS] export extrinsics (view poses)\n"
      << "[-S|--STRUCTURE] export structure\n"
      << "[-C|--CONTROL_POINTS] export control points";

    OPENMVG_LOG_ERROR << s;
    return EXIT_FAILURE;
  }

  if (sSfM_Data_Filename_In.empty() || sSfM_Data_Filename_Out.empty())
  {
    OPENMVG_LOG_ERROR << "Invalid input or output filename.";
    return EXIT_FAILURE;
  }

  // OptionSwitch is cloned in cmd.add(),
  // so we must use cmd.used() instead of testing OptionSwitch.used
  int flags =
    (cmd.used('V') ? VIEWS      : 0)
  | (cmd.used('I') ? INTRINSICS : 0)
  | (cmd.used('E') ? EXTRINSICS : 0)
  | (cmd.used('S') ? STRUCTURE  : 0)
  | (cmd.used('C') ? CONTROL_POINTS : 0);

  flags = (flags) ? flags : ALL;

  // Load input SfM_Data scene
  SfM_Data sfm_data;
  if (!Load(sfm_data, sSfM_Data_Filename_In, ESfM_Data(ALL)))
  {
    OPENMVG_LOG_ERROR << "The input SfM_Data file \"" << sSfM_Data_Filename_In << "\" cannot be read.";
    return EXIT_FAILURE;
  }

  // Export the SfM_Data scene in the expected format
  if (Save(
    sfm_data,
    sSfM_Data_Filename_Out.c_str(),
    ESfM_Data(flags)))
  {
    return EXIT_SUCCESS;
  }

  OPENMVG_LOG_ERROR << "An error occured while trying to save \"" << sSfM_Data_Filename_Out << "\".";
  return EXIT_FAILURE;
}
