#!/usr/bin/python3

"""
Writes a ppm file for a solid green color.

Intended for zoom. Although Google Meet works fine with a picture, zoom doesn't even work with a solid green (tested with two
shades), a solid blue (tested with one shade), or a solid gray (tested with one shade).
"""

import sys


def usage(retval: int) -> None:
    """Output a usage message."""
    if retval == 0:
        write = sys.stdout.write
    else:
        write = sys.stderr.write

    write('Usage: {} --rgb 0 255 0 --width 1 --height 1 --help\n'.format(sys.argv[0]))
    write('\n')
    write('(The above example will produce a square, solid bright green picture file with dimensions 1x1)\n')

    sys.exit(retval)


def pad(number: int) -> str:
    """Convert an integer to a string, and pad the string to 4 columns."""
    return str(number).rjust(4)
    # string = str(number)
    # while len(string) < 4:
    #     string = string + ' '
    # return string


def main() -> None:
    """Generate the image."""
    colors_specified = False
    width = 1
    height = 1
    while sys.argv[1:]:
        if sys.argv[1] == '--rgb':
            red = int(sys.argv[2])
            green = int(sys.argv[3])
            blue = int(sys.argv[4])
            colors_specified = True
            del sys.argv[1]
            del sys.argv[1]
            del sys.argv[1]
        elif sys.argv[1] == '--width':
            width = int(sys.argv[2])
            del sys.argv[1]
        elif sys.argv[1] == '--height':
            height = int(sys.argv[2])
            del sys.argv[1]
        elif sys.argv[1] in ('-h', '--help'):
            usage(0)
        else:
            print('{}: unrecognized option: {}'.format(sys.argv[0], sys.argv[1]), file=sys.stderr)
            usage(1)
        del sys.argv[1]

    if not colors_specified or not (0 <= red <= 255 and 0 <= green <= 255 and 0 <= blue <= 255):
        print('{}: --rgb red green blue is a required option.  The 3 colors must also be between 0 and 255'.format(sys.argv[0]))
        usage(1)

    colors = 256
    print('P3')
    print('%d %d' % (width, height))
    print(colors - 1)
    for i in range(0, height):
        sys.stderr.write('{} of {}\n'.format(i, height))
        for j in range(0, width):
            sys.stdout.write('{}{}{}'.format(pad(red), pad(green), pad(blue)+' '))
        sys.stdout.write('\n')


main()