package quantity

import "base:intrinsics"

PASCALS_PER_TORR :: 101325.0 / 760.0
KILO_PASCALS_PER_PSI :: 6.894757293168364

//----- Pascals ----------------------------------
Pascals :: struct($V: typeid) where intrinsics.type_is_numeric(V) {
	v: V,
}

@(private = "file")
pascals_to_kilo_pascals :: #force_inline proc "contextless" (
	pascals: Pascals($V),
) -> Kilo_Pascals(V) where intrinsics.type_is_numeric(V) {
	return Kilo_Pascals(V){pascals.v / KILO}
}

@(private = "file")
pascals_to_torr :: #force_inline proc "contextless" (
	pascals: Pascals($V),
) -> Torr(V) where intrinsics.type_is_float(V) {
	return Torr(V){pascals.v / PASCALS_PER_TORR}
}

//----- Kilopascals ----------------------------------
Kilo_Pascals :: struct($V: typeid) where intrinsics.type_is_numeric(V) {
	v: V,
}

@(private = "file")
kilo_pascals_to_pascals :: #force_inline proc "contextless" (
	kilo_pascals: Kilo_Pascals($V),
) -> Pascals(V) where intrinsics.type_is_numeric(V) {
	return Pascals(V){kilo_pascals.v * KILO}
}

kilo_pascals_to_psi :: #force_inline proc "contextless" (
    kilo_pascals: Kilo_Pascals($V),
) -> Psi(V) where intrinsics.type_is_float(V) {
	return Psi(V){kilo_pascals.v / KILO_PASCALS_PER_PSI}
}

//----- Torr ----------------------------------
Torr :: struct($V: typeid) where intrinsics.type_is_numeric(V) {
	v: V,
}

@(private = "file")
torr_to_pascals :: #force_inline proc "contextless" (
	torr: Torr($V),
) -> Pascals(V) where intrinsics.type_is_float(V) {
	return Pascals(V){torr.v * PASCALS_PER_TORR}
}

//----- PSI ----------------------------------
Psi :: struct($V: typeid) where intrinsics.type_is_numeric(V) {
	v: V,
}

psi_to_kilo_pascals :: #force_inline proc "contextless" (
	psi: Psi($V),
) -> Kilo_Pascals(V) where intrinsics.type_is_float(V) {
	return Kilo_Pascals(V){psi.v * KILO_PASCALS_PER_PSI}
}

// ---------------------------------------------------------------------------------------------------------------------
// ----- Conversion Overloads ------------------------
// ---------------------------------------------------------------------------------------------------------------------
to_pascals :: proc {
	kilo_pascals_to_pascals,
	torr_to_pascals,
}

to_kilo_pascals :: proc {
	pascals_to_kilo_pascals,
	psi_to_kilo_pascals,
}

to_torr :: proc {
    pascals_to_torr,
}

to_psi :: proc {
    kilo_pascals_to_psi,
}


// ---------------------------------------------------------------------------------------------------------------------
// ----- Tests ------------------------
// ---------------------------------------------------------------------------------------------------------------------
import "core:testing"

@(test)
test_pascals_to_kilo_pascals :: proc(t: ^testing.T) {
	pascals := Pascals(int){1000}
	kilo_pascals := to_kilo_pascals(pascals)

	testing.expect_value(t, kilo_pascals, Kilo_Pascals(int){1})
}

@(test)
test_kilo_pascals_to_pascals :: proc(t: ^testing.T) {
	kilo_pascals := Kilo_Pascals(int){1}
	pascals := to_pascals(kilo_pascals)

	testing.expect_value(t, pascals, Pascals(int){1000})
}

@(test)
test_pascals_to_torr :: proc(t: ^testing.T) {
    pascals := Pascals(f32){1000}
    torr := to_torr(pascals)

    testing.expect(t, torr.v > 7.49 && torr.v < 7.51)
}

@(test)
test_torr_to_pascals :: proc(t: ^testing.T) {
    torr := Torr(f32){7.5}
    pascals := to_pascals(torr)

    testing.expect(t, pascals.v > 999.91 && pascals.v < 999.92)
}

@(test)
test_psi_kilo_pascals :: proc(t: ^testing.T) {
    psi := Psi(f32){2.5}
    kilo_pascals := Kilo_Pascals(f32){17.23689323292091}

    testing.expect(t, to_kilo_pascals(psi).v > 17.22 && to_kilo_pascals(psi).v < 17.24)
    testing.expect(t, to_psi(kilo_pascals).v > 2.49 && to_psi(kilo_pascals).v < 2.51)
}
