using Test

using HydrophoneCalibrations
using HydrophoneCalibrations: enforce_hertz_unit, interp
using HydrophoneCalibrations.Unitful

tolerance_v2p = 1e4u"Pa/V";


@testset "HydrophoneCalibrations.jl" begin
    @testset "precision acoustics hydrophone" begin
        v2p = volt_to_pressure(1e6, :PA_3197)
        sensitivity_at_1mhz = 34u"mV/MPa"
        conversion_factor = uconvert(u"Pa/V", 1/sensitivity_at_1mhz)
        @test isapprox(v2p, conversion_factor, atol=tolerance_v2p)
    end

    @testset "onda hydrophone" begin
        v2p = volt_to_pressure(1e6, :Onda_HGL0200_2322)
        sensitivity_at_1mhz = 4.11e-8u"V/Pa"
        conversion_factor = uconvert(u"Pa/V", 1/sensitivity_at_1mhz)
        @test isapprox(v2p, conversion_factor, atol=tolerance_v2p)
    end

    @testset "onda hydrophone with preamplifier (linear calculations)" begin
        v2p = volt_to_pressure(1e6, :Onda_HGL0200_2322, :Onda_AH2020_1238_20dB)
        sensitivity_at_1mhz = 4.11e-8u"V/Pa"
        hydrophone_capacitance_at_1mhz = 9.963u"pF"
        preamp_capacitance_at_1mhz = 7.08u"pF"
        preamp_gain_at_1mhz = 19.92u"dB"
        capacitance_factor = hydrophone_capacitance_at_1mhz/(hydrophone_capacitance_at_1mhz+preamp_capacitance_at_1mhz)
        system_sensitivity = uconvertrp(NoUnits, preamp_gain_at_1mhz) * sensitivity_at_1mhz * capacitance_factor
        conversion_factor = uconvert(u"Pa/V", 1/system_sensitivity)
        @test isapprox(v2p, conversion_factor, atol=tolerance_v2p)
    end

    @testset "onda hydrophone with preamplifier (logarithmic calculations)" begin
        v2p = volt_to_pressure(1e6, :Onda_HGL0200_2322, :Onda_AH2020_1238_20dB)
        sensitivity_at_1mhz = 4.11e-8u"V/Pa"
        hydrophone_capacitance_at_1mhz = 9.963u"pF"
        preamp_capacitance_at_1mhz = 7.08u"pF"
        preamp_gain_at_1mhz = 19.92u"dB"
        capacitance_factor = hydrophone_capacitance_at_1mhz/(hydrophone_capacitance_at_1mhz+preamp_capacitance_at_1mhz)
        system_sensitivity = preamp_gain_at_1mhz + ustrip(uconvertrp(u"dBV/Pa", sensitivity_at_1mhz))*u"dBV" + uconvertrp(u"dB", capacitance_factor)
        system_sensitivity = uconvertrp(u"V", system_sensitivity) / u"Pa"
        conversion_factor = uconvert(u"Pa/V", 1/system_sensitivity)
        @test isapprox(v2p, conversion_factor, atol=tolerance_v2p)
    end

    @testset "onda hydrophone with unknown preamplifier" begin
        @test_throws MethodError volt_to_pressure(1e6, :Onda_HGL0200_2322, :my_dummy)
    end

    @testset "volt_to_pressure_and_phase() with unknown preamp" begin
        @test_throws MethodError volt_to_pressure_and_phase(1e6, :Onda_HGL0200_2322, :my_dummy)
    end


    @testset "enforce_hertz_unit()" begin
        @test enforce_hertz_unit(2.0u"mHz") == 0.002u"Hz"
        @test enforce_hertz_unit(2u"Hz")    == 2u"Hz"
        @test enforce_hertz_unit(2.2u"MHz") == 2.2e6u"Hz"
        @test_throws ErrorException enforce_hertz_unit(2.2u"m")
        @test enforce_hertz_unit(2)     == 2u"Hz"
        @test enforce_hertz_unit(2.2e6) == 2.2e6u"Hz"
    end


    @testset "interp()" begin
        f_axis = collect(0:1:10)
        values = f_axis

        f0_0 = 0.01
        @test interp(f_axis, values, f0_0) == 0.01
        f0_1 = 0.5
        @test interp(f_axis, values, f0_1) == 0.5
        f0_low = -0.1
        @test_throws "f0 must be between $f_axis[1]-$f_axis[end]" interp(f_axis, values, f0_low)
        f0_high = 10.1
        @test_throws "f0 must be between $f_axis[1]-$f_axis[end]" interp(f_axis, values, f0_high)
    end
end
