import math
import sys
import os
from PIL import Image, ImageDraw, ImageFont

# Set font path to the local Roboto-Bold font
font_path = 'scratch/Roboto-Bold.ttf'

def draw_gear(draw, cx, cy, r_out, r_in, teeth_count, teeth_h, teeth_w, color):
    draw.ellipse([cx - r_out, cy - r_out, cx + r_out, cy + r_out], fill=color)
    for i in range(teeth_count):
        angle = i * (2 * math.pi / teeth_count)
        x0 = cx + r_out * math.cos(angle)
        y0 = cy + r_out * math.sin(angle)
        tx = -math.sin(angle)
        ty = math.cos(angle)
        nx = math.cos(angle)
        ny = math.sin(angle)
        p1 = (x0 - (teeth_w/2) * tx, y0 - (teeth_w/2) * ty)
        p2 = (x0 + (teeth_w/2) * tx, y0 + (teeth_w/2) * ty)
        p3 = (x0 + teeth_h * nx + (teeth_w/3) * tx, y0 + teeth_h * ny + (teeth_w/3) * ty)
        p4 = (x0 + teeth_h * nx - (teeth_w/3) * tx, y0 + teeth_h * ny - (teeth_w/3) * ty)
        draw.polygon([p1, p2, p3, p4], fill=color)
    draw.ellipse([cx - r_in, cy - r_in, cx + r_in, cy + r_in], fill=(255, 255, 255, 255))

def draw_car_large(draw, color):
    # Center: (765, 205)
    # Cabin
    draw.polygon([(740, 155), (790, 155), (809, 198), (721, 198)], fill=color)
    # Windshield
    draw.polygon([(743, 161), (787, 161), (802, 194), (728, 194)], fill=(255, 255, 255, 255))
    # Windshield divider
    draw.rectangle([763, 161, 767, 194], fill=color)
    # Main body
    draw.polygon([
        (712, 195), (818, 195),
        (827, 204), (827, 246),
        (818, 255), (712, 255),
        (703, 246), (703, 204)
    ], fill=color)
    # Headlights
    r_head = 9
    draw.ellipse([722 - r_head, 222 - r_head, 722 + r_head, 222 + r_head], fill=(255, 255, 255, 255))
    draw.ellipse([808 - r_head, 222 - r_head, 808 + r_head, 222 + r_head], fill=(255, 255, 255, 255))
    # Grille
    draw.rectangle([740, 222, 790, 232], fill=(255, 255, 255, 255))
    # Side mirrors
    draw.polygon([(694, 190), (703, 195), (703, 203), (694, 198)], fill=color)
    draw.polygon([(836, 190), (827, 195), (827, 203), (836, 198)], fill=color)

def create_app_logo(base_logo_path, text, output_path):
    im = Image.open(base_logo_path)
    draw = ImageDraw.Draw(im)
    color = (29, 32, 47, 255)
    
    font_size = 90
    font = ImageFont.truetype(font_path, font_size)
    
    # Get text bounding box
    bbox = draw.textbbox((0, 0), text, font=font)
    tw = bbox[2] - bbox[0]
    th = bbox[3] - bbox[1]
    
    tx = 512 - (tw / 2)
    ty = 887 - (th / 2) - bbox[1]
    
    draw.text((tx, ty), text, font=font, fill=color)
    im.save(output_path)
    print(f'Saved logo with text \"{text}\" to {output_path}. BBox: X={tx} to {tx+tw}, Y={ty+bbox[1]} to {ty+bbox[3]}')

# Create Admin logo
print('Creating Admin Logo...')
im_admin = Image.open('scratch/logo_s_only.png')
draw_admin = ImageDraw.Draw(im_admin)
color = (29, 32, 47, 255)
draw_gear(draw_admin, cx=765, cy=205, r_out=52, r_in=20, teeth_count=8, teeth_h=16, teeth_w=20, color=color)
im_admin.save('scratch/admin_base.png')
create_app_logo('scratch/admin_base.png', 'ADMIN', 'scratch/admin_logo_final.png')

# Create Service logo
print('Creating Service Logo...')
im_service = Image.open('scratch/logo_s_only.png')
draw_service = ImageDraw.Draw(im_service)
draw_car_large(draw_service, color)
im_service.save('scratch/service_base.png')
create_app_logo('scratch/service_base.png', 'SERVICE', 'scratch/service_logo_final.png')

print('All logos compiled successfully.')
