"""DuckieDai Centripetal Force 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 positive_number(prompt):
    """Require a value greater than zero."""
    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("CENTRIPETAL")

    print("CENTRIPETAL FORCE")
    print()
    print("Formula:")
    print("Fc = m*v^2/r")
    input("Press enter for menu ")
    print("\n" * 6)
    print()
    print("What do you need?")
    print()
    print("1. Centripetal force")
    print("2. Mass")
    print("3. Speed")
    print("4. Radius")
    print()

    choice = input("Choose 1-4: ")

    print()

    # ==========================================
    # 1. SOLVE CENTRIPETAL FORCE
    #
    # Fc = m*v^2/r
    # ==========================================

    if choice == "1":

        mass = positive_number("Mass (kg): ")
        speed = nonnegative_number("Speed (m/s): ")
        radius = positive_number("Radius (m): ")

        force = (
            mass * speed ** 2 /
            radius
        )

        print()
        print("Using:")
        print("Fc = m*v^2/r")
        print()
        print("Fc = " + str(mass))
        print("x " + str(speed) + "^2")
        print("/ " + str(radius))

        show_answer(
            "Fc",
            force,
            "newtons"
        )

    # ==========================================
    # 2. SOLVE MASS
    #
    # m = Fc*r/v^2
    # ==========================================

    elif choice == "2":

        force = positive_number("Force (N): ")
        radius = positive_number("Radius (m): ")
        speed = positive_number("Speed (m/s): ")

        mass = (
            force * radius /
            (speed ** 2)
        )

        print()
        print("Using:")
        print("Fc = m*v^2/r")
        print()
        print("Rearranged:")
        print("m = Fc*r/v^2")

        show_answer(
            "m",
            mass,
            "kg"
        )

    # ==========================================
    # 3. SOLVE SPEED
    #
    # v = sqrt(Fc*r/m)
    # ==========================================

    elif choice == "3":

        force = nonnegative_number("Force (N): ")
        radius = positive_number("Radius (m): ")
        mass = positive_number("Mass (kg): ")

        speed = sqrt(
            force * radius /
            mass
        )

        print()
        print("Using:")
        print("Fc = m*v^2/r")
        print()
        print("Rearranged:")
        print("v = sqrt(Fc*r/m)")

        show_answer(
            "v",
            speed,
            "m/s"
        )

    # ==========================================
    # 4. SOLVE RADIUS
    #
    # r = m*v^2/Fc
    # ==========================================

    elif choice == "4":

        mass = positive_number("Mass (kg): ")
        speed = positive_number("Speed (m/s): ")
        force = positive_number("Force (N): ")

        radius = (
            mass * speed ** 2 /
            force
        )

        print()
        print("Using:")
        print("Fc = m*v^2/r")
        print()
        print("Rearranged:")
        print("r = m*v^2/Fc")

        show_answer(
            "r",
            radius,
            "meters"
        )

    else:
        print("Invalid choice.")

    print()
    print("DuckieDai says: done!")


# TI-Python imports the selected AppVar rather than
# setting __name__ to main.
main()
