use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::conversion::traits::{
    CheckedFrom, ConvertibleFrom, ExactFrom, RoundingFrom,
};
use malachite_base::num::float::NiceFloat;
use malachite_base::rounding_modes::RoundingMode;
use malachite_base::test_util::bench::{run_benchmark, BenchmarkType};
use malachite_base::test_util::generators::common::{GenConfig, GenMode};
use malachite_base::test_util::runner::Runner;
use malachite_q::test_util::bench::bucketers::{
    pair_1_rational_bit_bucketer, pair_2_rational_bit_bucketer, rational_bit_bucketer,
};
use malachite_q::test_util::generators::{
    rational_gen, rational_gen_rm, rational_gen_var_4, rational_rounding_mode_pair_gen_var_5,
};
use malachite_q::Rational;

pub(crate) fn register(runner: &mut Runner) {
    register_primitive_float_demos!(runner, demo_float_rounding_from_rational);
    register_primitive_float_demos!(runner, demo_float_rounding_from_rational_ref);
    register_primitive_float_demos!(runner, demo_float_from_rational);
    register_primitive_float_demos!(runner, demo_float_from_rational_ref);
    register_primitive_float_demos!(runner, demo_float_checked_from_rational);
    register_primitive_float_demos!(runner, demo_float_checked_from_rational_ref);
    register_primitive_float_demos!(runner, demo_float_exact_from_rational);
    register_primitive_float_demos!(runner, demo_float_exact_from_rational_ref);
    register_primitive_float_demos!(runner, demo_float_convertible_from_rational);
    register_primitive_float_demos!(runner, demo_float_convertible_from_rational_ref);

    register_primitive_float_benches!(
        runner,
        benchmark_float_rounding_from_rational_evaluation_strategy
    );
    register_bench!(
        runner,
        benchmark_f32_rounding_from_down_rational_library_comparison
    );
    register_bench!(
        runner,
        benchmark_f64_rounding_from_down_rational_library_comparison
    );
    register_primitive_float_benches!(runner, benchmark_float_from_rational_evaluation_strategy);
    register_primitive_float_benches!(
        runner,
        benchmark_float_checked_from_rational_evaluation_strategy
    );
    register_primitive_float_benches!(
        runner,
        benchmark_float_exact_from_rational_evaluation_strategy
    );
    register_primitive_float_benches!(
        runner,
        benchmark_float_convertible_from_rational_evaluation_strategy
    );
}

fn demo_float_rounding_from_rational<
    T: for<'a> ConvertibleFrom<&'a Rational> + PrimitiveFloat + RoundingFrom<Rational>,
>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
) where
    Rational: From<T>,
{
    for (n, rm) in rational_rounding_mode_pair_gen_var_5::<T>()
        .get(gm, &config)
        .take(limit)
    {
        println!(
            "{}::rounding_from({}, {}) = {}",
            T::NAME,
            n.clone(),
            rm,
            NiceFloat(T::rounding_from(n, rm))
        );
    }
}

fn demo_float_rounding_from_rational_ref<
    T: for<'a> ConvertibleFrom<&'a Rational> + PrimitiveFloat + for<'a> RoundingFrom<&'a Rational>,
>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
) where
    Rational: From<T>,
{
    for (n, rm) in rational_rounding_mode_pair_gen_var_5::<T>()
        .get(gm, &config)
        .take(limit)
    {
        println!(
            "{}::rounding_from(&{}, {}) = {}",
            T::NAME,
            n,
            rm,
            NiceFloat(T::rounding_from(&n, rm))
        );
    }
}

fn demo_float_from_rational<T: From<Rational> + PrimitiveFloat>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
) {
    for n in rational_gen().get(gm, &config).take(limit) {
        println!(
            "{}::from({}) = {}",
            T::NAME,
            n.clone(),
            NiceFloat(T::from(n))
        );
    }
}

fn demo_float_from_rational_ref<T: for<'a> From<&'a Rational> + PrimitiveFloat>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
) {
    for n in rational_gen().get(gm, &config).take(limit) {
        println!("{}::from(&{}) = {}", T::NAME, n, NiceFloat(T::from(&n)));
    }
}

fn demo_float_checked_from_rational<T: CheckedFrom<Rational> + PrimitiveFloat>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
) {
    for n in rational_gen().get(gm, &config).take(limit) {
        println!(
            "{}::checked_from({}) = {:?}",
            T::NAME,
            n.clone(),
            T::checked_from(n).map(NiceFloat)
        );
    }
}

fn demo_float_checked_from_rational_ref<T: for<'a> CheckedFrom<&'a Rational> + PrimitiveFloat>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
) {
    for n in rational_gen().get(gm, &config).take(limit) {
        println!(
            "{}::checked_from(&{}) = {:?}",
            T::NAME,
            n,
            T::checked_from(&n).map(NiceFloat)
        );
    }
}

fn demo_float_exact_from_rational<T: ExactFrom<Rational> + PrimitiveFloat>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
) where
    Rational: From<T>,
{
    for n in rational_gen_var_4::<T>().get(gm, &config).take(limit) {
        println!(
            "{}::exact_from({}) = {}",
            T::NAME,
            n.clone(),
            NiceFloat(T::exact_from(n))
        );
    }
}

fn demo_float_exact_from_rational_ref<T: for<'a> ExactFrom<&'a Rational> + PrimitiveFloat>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
) where
    Rational: From<T>,
{
    for n in rational_gen_var_4::<T>().get(gm, &config).take(limit) {
        println!(
            "{}::exact_from(&{}) = {}",
            T::NAME,
            n,
            NiceFloat(T::exact_from(&n))
        );
    }
}

fn demo_float_convertible_from_rational<T: ConvertibleFrom<Rational> + PrimitiveFloat>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
) {
    for n in rational_gen().get(gm, &config).take(limit) {
        let n_old = n.clone();
        if T::convertible_from(n) {
            println!("{} is convertible to an {}", n_old, T::NAME);
        } else {
            println!("{} is not convertible to an {}", n_old, T::NAME);
        }
    }
}

fn demo_float_convertible_from_rational_ref<
    T: for<'a> ConvertibleFrom<&'a Rational> + PrimitiveFloat,
>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
) {
    for n in rational_gen().get(gm, &config).take(limit) {
        if T::convertible_from(&n) {
            println!("{} is convertible to an {}", n, T::NAME);
        } else {
            println!("{} is not convertible to an {}", n, T::NAME);
        }
    }
}

fn benchmark_float_rounding_from_rational_evaluation_strategy<
    T: for<'a> ConvertibleFrom<&'a Rational>
        + PrimitiveFloat
        + RoundingFrom<Rational>
        + for<'a> RoundingFrom<&'a Rational>,
>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
    file_name: &str,
) where
    Rational: From<T>,
{
    run_benchmark(
        &format!("{}::rounding_from(Rational, RoundingMode)", T::NAME),
        BenchmarkType::EvaluationStrategy,
        rational_rounding_mode_pair_gen_var_5::<T>().get(gm, &config),
        gm.name(),
        limit,
        file_name,
        &pair_1_rational_bit_bucketer("n"),
        &mut [
            (
                &format!("{}::rounding_from(Rational, RoundingMode)", T::NAME),
                &mut |(n, rm)| no_out!(T::rounding_from(n, rm)),
            ),
            (
                &format!("{}::rounding_from(&Rational, RoundingMode)", T::NAME),
                &mut |(n, rm)| no_out!(T::rounding_from(&n, rm)),
            ),
        ],
    );
}

fn benchmark_f32_rounding_from_down_rational_library_comparison(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
    file_name: &str,
) {
    run_benchmark(
        "f32::rounding_from(Rational, RoundingMode::Down)",
        BenchmarkType::LibraryComparison,
        rational_gen_rm().get(gm, &config),
        gm.name(),
        limit,
        file_name,
        &pair_2_rational_bit_bucketer("n"),
        &mut [
            ("Malachite", &mut |(_, n)| {
                no_out!(f32::rounding_from(n, RoundingMode::Down))
            }),
            ("rug", &mut |(n, _)| no_out!(n.to_f32())),
        ],
    );
}

fn benchmark_f64_rounding_from_down_rational_library_comparison(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
    file_name: &str,
) {
    run_benchmark(
        "f64::rounding_from(Rational, RoundingMode::Down)",
        BenchmarkType::LibraryComparison,
        rational_gen_rm().get(gm, &config),
        gm.name(),
        limit,
        file_name,
        &pair_2_rational_bit_bucketer("n"),
        &mut [
            ("Malachite", &mut |(_, n)| {
                no_out!(f64::rounding_from(n, RoundingMode::Down))
            }),
            ("rug", &mut |(n, _)| no_out!(n.to_f64())),
        ],
    );
}

#[allow(unused_must_use)]
fn benchmark_float_from_rational_evaluation_strategy<
    T: From<Rational> + for<'a> From<&'a Rational> + PrimitiveFloat,
>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
    file_name: &str,
) {
    run_benchmark(
        &format!("{}::from(Rational)", T::NAME),
        BenchmarkType::EvaluationStrategy,
        rational_gen().get(gm, &config),
        gm.name(),
        limit,
        file_name,
        &rational_bit_bucketer("n"),
        &mut [
            (&format!("{}::from(Rational)", T::NAME), &mut |n| {
                no_out!(T::from(n))
            }),
            (&format!("{}::from(&Rational)", T::NAME), &mut |n| {
                no_out!(T::from(&n))
            }),
        ],
    );
}

fn benchmark_float_checked_from_rational_evaluation_strategy<
    T: CheckedFrom<Rational> + for<'a> CheckedFrom<&'a Rational> + PrimitiveFloat,
>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
    file_name: &str,
) {
    run_benchmark(
        &format!("{}::checked_from(Rational)", T::NAME),
        BenchmarkType::EvaluationStrategy,
        rational_gen().get(gm, &config),
        gm.name(),
        limit,
        file_name,
        &rational_bit_bucketer("n"),
        &mut [
            (&format!("{}::checked_from(Rational)", T::NAME), &mut |n| {
                no_out!(T::checked_from(n))
            }),
            (&format!("{}::checked_from(&Rational)", T::NAME), &mut |n| {
                no_out!(T::checked_from(&n))
            }),
        ],
    );
}

fn benchmark_float_exact_from_rational_evaluation_strategy<
    T: ExactFrom<Rational> + for<'a> ExactFrom<&'a Rational> + PrimitiveFloat,
>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
    file_name: &str,
) where
    Rational: From<T>,
{
    run_benchmark(
        &format!("{}::exact_from(Rational)", T::NAME),
        BenchmarkType::EvaluationStrategy,
        rational_gen_var_4::<T>().get(gm, &config),
        gm.name(),
        limit,
        file_name,
        &rational_bit_bucketer("n"),
        &mut [
            (&format!("{}::exact_from(Rational)", T::NAME), &mut |n| {
                no_out!(T::exact_from(n))
            }),
            (&format!("{}::exact_from(&Rational)", T::NAME), &mut |n| {
                no_out!(T::exact_from(&n))
            }),
        ],
    );
}

fn benchmark_float_convertible_from_rational_evaluation_strategy<
    T: ConvertibleFrom<Rational> + for<'a> ConvertibleFrom<&'a Rational> + PrimitiveFloat,
>(
    gm: GenMode,
    config: GenConfig,
    limit: usize,
    file_name: &str,
) {
    run_benchmark(
        &format!("{}::convertible_from(Rational)", T::NAME),
        BenchmarkType::EvaluationStrategy,
        rational_gen().get(gm, &config),
        gm.name(),
        limit,
        file_name,
        &rational_bit_bucketer("n"),
        &mut [
            (
                &format!("{}::convertible_from(Rational)", T::NAME),
                &mut |n| no_out!(T::convertible_from(n)),
            ),
            (
                &format!("{}::convertible_from(&Rational)", T::NAME),
                &mut |n| no_out!(T::convertible_from(&n)),
            ),
        ],
    );
}
