"""DuckieDai Potential Energy for TI-84 Plus CE Python."""


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 positive_number(prompt):
    """Ask until a positive finite number is entered."""
    while True:
        try:
            value = float(input(prompt))

            if value > 0 and value < 1e100:
                return value

        except ValueError:
            pass

        print("Enter a number above 0.")


def nonnegative_number(prompt):
    """Allow zero or positive values."""
    while True:
        try:
            value = float(input(prompt))

            if value >= 0 and value < 1e100:
                return value

        except ValueError:
            pass

        print("Enter 0 or greater.")


def show_answer(name, value, unit):
    input("Press enter for answer ")
    print("\n" * 6)
    print("ANSWER")
    print(name + " = " + str(value))
    print(unit)


def main():
    duckiedai_intro("POTENTIAL ENERGY")

    g = 9.8

    print("GRAVITATIONAL PE")
    print()
    print("Formula: PE = m*g*h")
    print("Using g = 9.8 m/s^2")
    input("Press enter for menu ")
    print("\n" * 6)
    print()
    print("What do you need?")
    print()
    print("1. Potential Energy")
    print("2. Mass")
    print("3. Height")
    print()

    choice = input("Choose 1-3: ")

    print()

    # ==========================================
    # 1. SOLVE POTENTIAL ENERGY
    # PE = m*g*h
    # ==========================================

    if choice == "1":

        mass = positive_number("Mass (kg): ")
        height = nonnegative_number("Height (m): ")

        pe = mass * g * height

        print()
        print("Using:")
        print("PE = m*g*h")
        print()
        print("PE = " + str(mass))
        print("x " + str(g))
        print("x " + str(height))

        show_answer(
            "PE",
            pe,
            "joules"
        )

    # ==========================================
    # 2. SOLVE MASS
    # m = PE / (g*h)
    # ==========================================

    elif choice == "2":

        pe = nonnegative_number("PE (J): ")
        height = positive_number("Height (m): ")

        mass = pe / (g * height)

        print()
        print("Using:")
        print("PE = m*g*h")
        print()
        print("Rearranged:")
        print("m = PE/(g*h)")

        show_answer(
            "m",
            mass,
            "kg"
        )

    # ==========================================
    # 3. SOLVE HEIGHT
    # h = PE / (m*g)
    # ==========================================

    elif choice == "3":

        pe = nonnegative_number("PE (J): ")
        mass = positive_number("Mass (kg): ")

        height = pe / (mass * g)

        print()
        print("Using:")
        print("PE = m*g*h")
        print()
        print("Rearranged:")
        print("h = PE/(m*g)")

        show_answer(
            "h",
            height,
            "meters"
        )

    else:
        print("Invalid choice.")

    print()
    print("DuckieDai says: done!")


# TI-Python imports the selected AppVar rather than
# setting __name__ to main.
main()
