"""DuckieDai Shoelace Area 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 number(prompt):
    """Allow positive, negative, or zero coordinates."""
    while True:
        try:
            value = float(input(prompt))

            if value > -1e100 and value < 1e100:
                return value

        except ValueError:
            pass

        print("Enter a valid number.")


def vertex_count():
    """Require at least three polygon vertices."""
    while True:
        try:
            count = int(input("Number of vertices: "))

            if count >= 3:
                return count

        except ValueError:
            pass

        print("Enter 3 or more.")


def shoelace_area(x_values, y_values):
    """Calculate polygon area with the shoelace formula."""
    total = 0
    n = len(x_values)

    for i in range(n):

        next_i = (i + 1) % n

        total += (
            x_values[i] * y_values[next_i]
            - x_values[next_i] * y_values[i]
        )

    return abs(total) / 2


def main():
    duckiedai_intro("SHOELACE AREA")

    print("SHOELACE FORMULA")
    print("POLYGON AREA")
    print()
    print("Enter vertices in")
    print("order around polygon.")
    print()
    print("Clockwise OR")
    print("counterclockwise.")
    print()

    count = vertex_count()

    x_values = []
    y_values = []

    print()

    for i in range(count):

        print("POINT " + str(i + 1))

        x = number("x: ")
        y = number("y: ")

        x_values.append(x)
        y_values.append(y)

        print()

    area = shoelace_area(
        x_values,
        y_values
    )

    print("Using:")
    print("Shoelace Formula")
    print()
    print("Vertices: " + str(count))
    print()

    print("ANSWER")
    print("Area = " + str(area))
    print("square units")

    print()
    print("DuckieDai says: done!")


# TI-Python imports the selected AppVar rather than
# setting __name__ to main.
main()
