//          Copyright Carl Philipp Reh 2009 - 2021.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <fcppt/args_vector.hpp>
#include <fcppt/text.hpp>
#include <fcppt/catch/begin.hpp>
#include <fcppt/catch/either.hpp> // IWYU pragma: keep
#include <fcppt/catch/end.hpp>
#include <fcppt/catch/record.hpp> // IWYU pragma: keep
#include <fcppt/catch/strong_typedef.hpp> // IWYU pragma: keep
#include <fcppt/catch/variant.hpp> // IWYU pragma: keep
#include <fcppt/either/comparison.hpp>
#include <fcppt/either/make_failure.hpp>
#include <fcppt/options/apply.hpp>
#include <fcppt/options/argument.hpp>
#include <fcppt/options/argument_usage.hpp>
#include <fcppt/options/duplicate_names.hpp>
#include <fcppt/options/error_output.hpp> // IWYU pragma: keep
#include <fcppt/options/flag.hpp>
#include <fcppt/options/flag_usage.hpp>
#include <fcppt/options/long_name.hpp>
#include <fcppt/options/make_active_value.hpp>
#include <fcppt/options/make_inactive_value.hpp>
#include <fcppt/options/make_success.hpp>
#include <fcppt/options/optional_help_text.hpp>
#include <fcppt/options/optional_short_name.hpp>
#include <fcppt/options/parse.hpp>
#include <fcppt/options/pretty_type.hpp>
#include <fcppt/options/product_usage.hpp>
#include <fcppt/options/result_of.hpp>
#include <fcppt/options/short_name.hpp>
#include <fcppt/options/usage.hpp>
#include <fcppt/options/usage_variant.hpp>
#include <fcppt/record/comparison.hpp> // IWYU pragma: keep
#include <fcppt/record/make_label.hpp>
#include <fcppt/test/options/catch_error.hpp> // IWYU pragma: keep
#include <fcppt/test/options/catch_usage.hpp> // IWYU pragma: keep
#include <fcppt/config/external_begin.hpp>
#include <catch2/catch_test_macros.hpp>
#include <fcppt/config/external_end.hpp>

FCPPT_CATCH_BEGIN
// NOLINTBEGIN(bugprone-throwing-static-initialization,clang-analyzer-optin.core.EnumCastOutOfRange,misc-const-correctness,cert-err58-cpp,fuchsia-statically-constructed-objects,misc-use-anonymous-namespace,cppcoreguidelines-avoid-do-while)

TEST_CASE("options::apply", "[options]")
{
  FCPPT_RECORD_MAKE_LABEL(arg_label);

  using int_arg_type = fcppt::options::argument<arg_label, int>;

  FCPPT_RECORD_MAKE_LABEL(flag_label);

  using int_flag_type = fcppt::options::flag<flag_label, int>;

  auto const mult_parser{fcppt::options::apply(
      int_arg_type{
          fcppt::options::long_name{FCPPT_TEXT("arg1")}, fcppt::options::optional_help_text{}},
      int_flag_type{
          fcppt::options::optional_short_name{fcppt::options::short_name{FCPPT_TEXT("f")}},
          fcppt::options::long_name{FCPPT_TEXT("flag")},
          fcppt::options::make_active_value(42),
          fcppt::options::make_inactive_value(10),
          fcppt::options::optional_help_text{}})};

  using result_type = fcppt::options::result_of<decltype(mult_parser)>;

  CHECK(
      fcppt::options::parse(
          mult_parser, fcppt::args_vector{FCPPT_TEXT("--flag"), FCPPT_TEXT("123")}) ==
      fcppt::options::make_success(
          result_type{arg_label{} = 123, flag_label{} = 42}));

  CHECK(
      fcppt::options::parse(mult_parser, fcppt::args_vector{}) ==
      fcppt::either::make_failure<result_type>(fcppt::options::error{fcppt::options::error_variant{
          fcppt::options::missing_argument_error{fcppt::options::long_name{FCPPT_TEXT("arg1")}}}}));

  CHECK(
      mult_parser.usage() ==
      fcppt::options::usage{fcppt::options::usage_variant{fcppt::options::product_usage{
          fcppt::options::usage{fcppt::options::usage_variant{fcppt::options::argument_usage{
              fcppt::options::long_name{FCPPT_TEXT("arg1")},
              fcppt::options::pretty_type<int>(),
              fcppt::options::optional_help_text{}}}},
          fcppt::options::usage{fcppt::options::usage_variant{fcppt::options::flag_usage{
              fcppt::options::long_name{FCPPT_TEXT("flag")},
              fcppt::options::optional_short_name{fcppt::options::short_name{FCPPT_TEXT("f")}},
              fcppt::options::optional_help_text{}}}}}}});
}

TEST_CASE("options::apply missing multi", "[options]")
{
  FCPPT_RECORD_MAKE_LABEL(arg1_label);
  FCPPT_RECORD_MAKE_LABEL(arg2_label);

  auto const mult_parser{fcppt::options::apply(
      fcppt::options::argument<arg1_label, int>{
          fcppt::options::long_name{FCPPT_TEXT("arg1")}, fcppt::options::optional_help_text{}},
      fcppt::options::argument<arg2_label, int>{
          fcppt::options::long_name{FCPPT_TEXT("arg2")}, fcppt::options::optional_help_text{}})};

  using result_type = fcppt::options::result_of<decltype(mult_parser)>;

  CHECK(
      fcppt::options::parse(mult_parser, fcppt::args_vector{}) ==
      fcppt::either::make_failure<result_type>(
          fcppt::options::error{fcppt::options::error_variant{fcppt::options::error_product{
              fcppt::options::error{
                  fcppt::options::error_variant{fcppt::options::missing_argument_error{
                      fcppt::options::long_name{FCPPT_TEXT("arg1")}}}},
              fcppt::options::error{
                  fcppt::options::error_variant{fcppt::options::missing_argument_error{
                      fcppt::options::long_name{FCPPT_TEXT("arg2")}}}}}}}));
}

TEST_CASE("options::apply duplicate names", "[options]")
{
  FCPPT_RECORD_MAKE_LABEL(flag_label1);

  FCPPT_RECORD_MAKE_LABEL(flag_label2);

  using flag1_type = fcppt::options::flag<flag_label1, int>;

  using flag2_type = fcppt::options::flag<flag_label2, int>;

  CHECK_THROWS_AS(
      fcppt::options::apply(
          flag1_type{
              fcppt::options::optional_short_name{},
              fcppt::options::long_name{FCPPT_TEXT("flag")},
              fcppt::options::make_active_value(0),
              fcppt::options::make_inactive_value(1),
              fcppt::options::optional_help_text{}},
          flag2_type{
              fcppt::options::optional_short_name{},
              fcppt::options::long_name{FCPPT_TEXT("flag")},
              fcppt::options::make_active_value(0),
              fcppt::options::make_inactive_value(1),
              fcppt::options::optional_help_text{}}),
      fcppt::options::duplicate_names);
}

// NOLINTEND(bugprone-throwing-static-initialization,clang-analyzer-optin.core.EnumCastOutOfRange,misc-const-correctness,cert-err58-cpp,fuchsia-statically-constructed-objects,misc-use-anonymous-namespace,cppcoreguidelines-avoid-do-while)
FCPPT_CATCH_END
