"""DuckieDai Final Velocity prototype for TI-84 Plus CE Python.

Standalone source. Not yet verified on a physical calculator.
"""


def duckiedai_intro(program_name, wait=True):
    """Show the same small opening as the Rectangle Area program."""
    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 is_finite_input(value):
    """Reject NaN, infinity, and magnitudes impractical for the calculator."""
    return value > -1e100 and value < 1e100


def number(prompt):
    """Allow positive, negative, or zero velocity and acceleration."""
    while True:
        try:
            value = float(input(prompt))
            if is_finite_input(value):
                return value
        except ValueError:
            pass
        print("Enter a valid number.")


def nonnegative_number(prompt):
    """Elapsed time can be zero, but not negative."""
    while True:
        value = number(prompt)
        if value >= 0:
            return value
        print("Enter 0 or above.")


def final_velocity(initial_velocity, acceleration, time):
    """Return vf = vi + a*t for constant acceleration in SI units."""
    if not is_finite_input(initial_velocity) or not is_finite_input(acceleration):
        raise ValueError("velocity and acceleration must be finite")
    if not is_finite_input(time) or time < 0:
        raise ValueError("time must be nonnegative and finite")
    velocity = initial_velocity + acceleration * time
    if not is_finite_input(velocity):
        raise ValueError("final velocity is too large")
    return velocity


def main():
    duckiedai_intro("FINAL VELOCITY")
    print("FINAL VELOCITY")
    print("Formula: vf = vi + a*t")
    print()
    vi = number("Initial v (m/s): ")
    acceleration = number("Accel (m/s^2): ")
    time = nonnegative_number("Time (s): ")
    vf = final_velocity(vi, acceleration, time)
    print()
    print("vf = " + str(vi) + " +")
    print("(" + str(acceleration) + " x " + str(time) + ")")
    print("Final velocity =")
    print(str(vf) + " m/s")
    print()
    print("DuckieDai says: done!")


# TI-Python imports a selected AppVar instead of setting __name__ to main.
main()
