/*
 * Copyright 2019 Google LLC, Andrew Hines
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef VISQOL_INCLUDE_COMMANDLINE_PARSER_H
#define VISQOL_INCLUDE_COMMANDLINE_PARSER_H

#include <string>
#include <utility>
#include <vector>

#include "absl/status/statusor.h"
#include "file_path.h"

namespace Visqol {

/**
 * The relative location of the default SVR model file for audio.
 */
extern const char kDefaultAudioModelFile[];

/**
 * The relative location of the default TFLite model file for speech.
 */
extern const char kDefaultSpeechModelFile[];

/**
 * This struct is used for storing args provided at the command line.
 */
struct CommandLineArgs {
  /**
   * The path to the reference signal for comparison. If this is not supplied a
   * path to a batch file must be supplied.
   */
  FilePath reference_signal_path;

  /**
   * The path to the degraded signal for comparison. If this is not supplied a
   * path to a batch file must be supplied.
   */
  FilePath degraded_signal_path;

  /**
   * The path to the similarity to quality mapper model file. If this is not
   * supplied, the default model file is used.
   */
  FilePath similarity_to_quality_mapper_model;

  /**
   * The path to a CSV file for storing similarity results. Optional.
   */
  FilePath results_output_csv;

  /**
   * The path to a CSV file containing 1 or more paths pairs for comparison.
   * Optional.
   */
  FilePath batch_input_csv;

  /**
   * The path to a txt file for storing additional debug info generated by the
   * comparisons. Optional.
   */
  FilePath debug_output_path;

  /**
   * If true, the reference and degraded signal paths and their similarity
   * score will be output to the console.
   */
  bool verbose = false;

  /**
   * If true, the input audio files will be compared using the ViSQOL speech
   * mode.
   */
  bool use_speech_mode = false;

  /**
   * When using the speech comparison mode, a value of true for this bool will
   * prevent perfect NSIM scores of 1.0 being mapped to a perfect MOS-LQO of
   * 5.0. It will instead be ~4.x. If the bool is false, a perfect NSIM score
   * will be mapped to a perfect MOS-LQO.
   */
  bool use_unscaled_speech_mos_mapping = false;

  /**
   * The search_window_radius parameter determines how far the algorithm will
   * search to discover patch matches. For a given reference frame, it will look
   * at 2*search_window_radius + 1 patches to find the most optimal match.
   */
  int search_window_radius = 60;

  /**
   * If true, use a lattice model to map similarity to MOS.
   */
  bool use_lattice_model = true;
};

/**
 * This class is used for parsing args provided at the command line.
 * Basic validation of the args is also provided.
 */
class VisqolCommandLineParser {
 public:
  /**
   * Parses and validates args provided at the command line.
   *
   * @param argc The count of args provided.
   * @param argv The args that were provided.
   *
   * @return A struct which holds the parsed args, if parsing was successful.
   *    Else an error status.
   */
  static absl::StatusOr<CommandLineArgs> Parse(int argc, char** argv);

  /**
   * Takes the files to be compared (either individual or batch) that were
   * provided at the command line and constructs a vector of path pairs.
   *
   * @param cmd_res The parsed command line args.
   *
   * @return The vector of file path pairs for comparison.
   */
  static std::vector<ReferenceDegradedPathPair> BuildFilePairPaths(
      const CommandLineArgs& cmd_res);

 private:
  /**
   * For a given file path, check if the file exists.
   *
   * @param path A FilePath to check for existence.
   *
   * @return True if the file exists, else false.
   */
  static bool FileExists(const FilePath& path);

  /**
   * Parses a batch CSV file to return a vector of file path pairs
   * for comparison.
   *
   * @param batch_input_path The path to the batch CSV file.
   *
   * @return A vector of file path pairs for comparison.
   */
  static std::vector<ReferenceDegradedPathPair> ReadFilesToCompare(
      const FilePath& batch_input_path);
};
};  // namespace Visqol

#endif  // VISQOL_INCLUDE_COMMANDLINE_PARSER_H
