"""DuckieDai Ohm's Law & Power for TI-84 Plus CE Python."""

from math import sqrt


def duckiedai_intro(program_name, wait=True):
    """Show the reusable DuckieDai program opening."""
    title = program_name[:16]
    empty = 16 - len(title)
    left = empty // 2
    right = empty - left

    print("+----------------------+")
    print("|   " + " " * left + title + " " * right + "   |")
    print("|                      |")
    print("|       __             |")
    print("|   ___(o )>  quack!   |")
    print("|   \\ <_. )           |")
    print("|    `---'             |")
    print("|     by DuckieDai     |")
    print("+----------------------+")
    print("Loading...")

    if wait:
        input("Press enter to start ")
        print("\n" * 6)


def optional_number(prompt):
    """Return a nonnegative number or None if blank."""
    while True:
        raw = input(prompt)

        if raw == "":
            return None

        try:
            value = float(raw)

            if value >= 0 and value < 1e100:
                return value

        except ValueError:
            pass

        print("Enter 0 or greater")
        print("or leave blank.")


def show_results(v, i, r, p):
    input("Press enter for results ")
    print("\n" * 6)
    print("RESULTS")
    print()
    print("Voltage:")
    print(str(v) + " V")
    print()
    print("Current:")
    print(str(i) + " A")
    input("Press enter for more ")
    print("\n" * 6)
    print("RESULTS CONTINUED")
    print()
    print("Resistance:")
    print(str(r) + " ohms")
    print()
    print("Power:")
    print(str(p) + " W")


def main():
    duckiedai_intro("OHMS LAW")

    print("OHM'S LAW & POWER")
    print()
    print("V = I*R")
    print("P = I*V")
    print("P = I^2*R")
    print("P = V^2/R")
    input("Press enter for inputs ")
    print("\n" * 6)
    print()
    print("Enter EXACTLY TWO")
    print("known values.")
    print()
    print("Leave unknowns blank.")
    print()

    v = optional_number("Voltage V: ")
    i = optional_number("Current A: ")
    r = optional_number("Resistance ohm: ")
    p = optional_number("Power W: ")

    known = 0

    if v is not None:
        known += 1
    if i is not None:
        known += 1
    if r is not None:
        known += 1
    if p is not None:
        known += 1

    print()

    if known != 2:
        print("Enter exactly two")
        print("known values.")

    # ==========================================
    # VOLTAGE + CURRENT
    #
    # R = V/I
    # P = V*I
    # ==========================================

    elif v is not None and i is not None:

        if i == 0:

            print("Cannot determine R")
            print("when current is 0.")

        else:

            r = v / i
            p = v * i

            print("Using:")
            print("R = V/I")
            print("P = V*I")

            show_results(v, i, r, p)

    # ==========================================
    # VOLTAGE + RESISTANCE
    #
    # I = V/R
    # P = V^2/R
    # ==========================================

    elif v is not None and r is not None:

        if r == 0:

            print("Resistance cannot")
            print("be zero.")

        else:

            i = v / r
            p = v ** 2 / r

            print("Using:")
            print("I = V/R")
            print("P = V^2/R")

            show_results(v, i, r, p)

    # ==========================================
    # CURRENT + RESISTANCE
    #
    # V = I*R
    # P = I^2*R
    # ==========================================

    elif i is not None and r is not None:

        if r == 0:

            print("Resistance cannot")
            print("be zero.")

        else:

            v = i * r
            p = i ** 2 * r

            print("Using:")
            print("V = I*R")
            print("P = I^2*R")

            show_results(v, i, r, p)

    # ==========================================
    # POWER + VOLTAGE
    #
    # I = P/V
    # R = V^2/P
    # ==========================================

    elif p is not None and v is not None:

        if v == 0 or p == 0:

            print("Need nonzero V")
            print("and P to solve.")

        else:

            i = p / v
            r = v ** 2 / p

            print("Using:")
            print("I = P/V")
            print("R = V^2/P")

            show_results(v, i, r, p)

    # ==========================================
    # POWER + CURRENT
    #
    # V = P/I
    # R = P/I^2
    # ==========================================

    elif p is not None and i is not None:

        if i == 0:

            print("Current cannot")
            print("be zero.")

        else:

            v = p / i
            r = p / (i ** 2)

            print("Using:")
            print("V = P/I")
            print("R = P/I^2")

            show_results(v, i, r, p)

    # ==========================================
    # POWER + RESISTANCE
    #
    # I = sqrt(P/R)
    # V = sqrt(P*R)
    # ==========================================

    elif p is not None and r is not None:

        if r == 0:

            print("Resistance cannot")
            print("be zero.")

        else:

            i = sqrt(p / r)
            v = sqrt(p * r)

            print("Using:")
            print("I = sqrt(P/R)")
            print("V = sqrt(P*R)")

            show_results(v, i, r, p)

    else:
        print("Could not solve.")
        print("Check your values.")

    print()
    print("DuckieDai says: done!")


# TI-Python imports the selected AppVar rather than
# setting __name__ to main.
main()
