import argparse
import random
from PIL import Image, ImageDraw, ImageFont


def create_image_with_text(file_name: str, text: str, width: int, height: int) -> None:
    image = Image.new("RGB", (width, height), color="white")
    draw = ImageDraw.Draw(image)

    try:
        font = ImageFont.truetype("arial.ttf", 24)
    except OSError:
        font = ImageFont.load_default()

    bbox = draw.textbbox((0, 0), text, font=font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    x = (width - text_width) // 2
    y = (height - text_height) // 2

    draw.text((x, y), text, fill="black", font=font)
    image.save(file_name)


def superimpose_text_on_image(file_name: str, text: str) -> None:
    image = Image.open(file_name).convert("RGBA")
    draw = ImageDraw.Draw(image)

    try:
        font = ImageFont.truetype("arial.ttf", 24)
    except OSError:
        font = ImageFont.load_default()

    bbox = draw.textbbox((0, 0), text, font=font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    x = (image.width - text_width) // 2
    y = (image.height - text_height) // 2

    draw.text((x, y), text, fill="black", font=font)
    image.save(file_name)


def create_image_with_random_shapes(file_name: str, width: int, height: int) -> None:
    image = Image.new("RGBA", (width, height), color="white")
    draw = ImageDraw.Draw(image, "RGBA")

    num_shapes = random.randint(5, 15)

    for _ in range(num_shapes):
        shape_type = random.choice(["rectangle", "ellipse"])

        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)
        opacity = random.randint(50, 200)
        color = (r, g, b, opacity)

        size = random.randint(20, min(width, height) // 2)
        x1 = random.randint(0, width - size)
        y1 = random.randint(0, height - size)
        x2 = x1 + size
        y2 = y1 + random.randint(20, size)

        if shape_type == "rectangle":
            draw.rectangle([x1, y1, x2, y2], fill=color)
        else:
            draw.ellipse([x1, y1, x2, y2], fill=color)

    image = image.convert("RGB")
    image.save(file_name)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Image tools")
    subparsers = parser.add_subparsers(dest="command", required=True)

    create_text_parser = subparsers.add_parser("create_text", help="Create image with text")
    create_text_parser.add_argument("file_name", help="Output file name")
    create_text_parser.add_argument("text", help="Text to display")
    create_text_parser.add_argument("width", type=int, help="Image width")
    create_text_parser.add_argument("height", type=int, help="Image height")

    superimpose_parser = subparsers.add_parser("superimpose", help="Superimpose text on image")
    superimpose_parser.add_argument("file_name", help="Image file to modify")
    superimpose_parser.add_argument("text", help="Text to superimpose")

    shapes_parser = subparsers.add_parser("shapes", help="Create image with random shapes")
    shapes_parser.add_argument("file_name", help="Output file name")
    shapes_parser.add_argument("width", type=int, help="Image width")
    shapes_parser.add_argument("height", type=int, help="Image height")

    args = parser.parse_args()

    if args.command == "create_text":
        create_image_with_text(args.file_name, args.text, args.width, args.height)
    elif args.command == "superimpose":
        superimpose_text_on_image(args.file_name, args.text)
    elif args.command == "shapes":
        create_image_with_random_shapes(args.file_name, args.width, args.height)
