# Python's statement forms, each with its meaning: chained assignment, `assert` with a
# message, `case` guards, `range` with a step, the walrus, nested functions with `nonlocal`,
# and a parameter the function assigns
from enum import Enum

from verada import decreases, ensures, invariant, old, requires

from shapes import Circle, Square, size


class Colour(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3


def weight(c: Colour, bright: bool) -> int:
    match c:
        case Colour.RED if bright:
            return 10
        case Colour.RED | Colour.GREEN:
            return 20
        case _:
            return 30


# a parameter assigned is the function's own copy: the caller's value is unchanged
def countdown(n: int) -> int:
    requires(n >= 0 and n <= 100)
    total = 0
    n = n * 2
    while n > 0:
        invariant(n <= 200 and total >= 0 and total <= 20000 - 100 * n)
        decreases(n)
        total += n
        n -= 3
    return total


def next_value(k: int) -> int:
    if k > 5:
        return 0
    return k + 2


def main() -> None:
    a = b = 4
    print(a, b)
    a += 1
    print(a, b)
    assert a > b, "a grew"
    for i in range(10, 0, -3):
        print("down", i)
    for i in range(1, 8, 2):
        print("up", i)
    for i in range(5, 5, 1):
        print("never", i)
    print(size(Circle(r=12)), size(Circle(r=1)), size(Square(side=9)), size(Square(side=2)))
    print(weight(Colour.RED, True), weight(Colour.RED, False), weight(Colour.BLUE, True))
    start = 4
    print(countdown(start), start)
    k = 0
    while (m := next_value(k)) > 0:
        print("m", m)
        k = m
    if (q := next_value(1)) > 2:
        print("q", q)
    elif (z := next_value(q)) > 1:
        print("z", z)
    values: list[int] = [1, 7, 3, 9]
    assert len(values) == 4
    big: list[int] = [w for i in range(4) if (w := values[i] * 2) > 5]
    for w in big:
        print("big", w)

    count = 0

    def bump(by: int) -> None:
        nonlocal count
        requires(by >= 0 and by <= 10 and count >= 0 and count <= 50)
        ensures(count == old(count) + by)
        count += by

    def twice(x: int) -> int:
        requires(x >= 0 and x <= 100 and count >= 0 and count <= 100)
        return x * 2 + count

    bump(3)
    bump(4)
    print(count, twice(5))


if __name__ == "__main__":
    main()
