"""
Procedural Historical Soldier Icon Generator (Episode 01: Cannae 216 BC)
Lead Cartographer & Tactical Map Artist: Marcus Sterling
Generates 6 historically authentic, alpha-transparent soldier tokens (128x128 & 256x256)
for Roman and Carthaginian forces.
"""

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

ICONS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "episodes", "EP01_Cannae_216BC", "02_Map_Data", "unit_icons"))
os.makedirs(ICONS_DIR, exist_ok=True)

def create_base_canvas(size=256):
    return Image.new("RGBA", (size, size), (0, 0, 0, 0))

def draw_token_base(draw, center, radius, border_color, fill_color):
    cx, cy = center
    # Outer glow / shadow
    draw.ellipse([cx - radius - 4, cy - radius - 4, cx + radius + 4, cy + radius + 4], fill=(0, 0, 0, 160))
    # Outer rim
    draw.ellipse([cx - radius, cy - radius, cx + radius, cy + radius], fill=fill_color, outline=border_color, width=4)
    # Inner ring
    draw.ellipse([cx - radius + 8, cy - radius + 8, cx + radius - 8, cy + radius - 8], outline=(border_color[0], border_color[1], border_color[2], 120), width=1)

def generate_roman_legionary():
    """Bộ binh nặng La Mã: Mũ Montefortino, khiên Scutum đỏ cong lớn biểu tượng sấm sét, giáp lưới Lorica Hamata"""
    img = create_base_canvas()
    draw = ImageDraw.Draw(img)
    cx, cy = 128, 128
    
    # Base Token: Roman Crimson
    draw_token_base(draw, (cx, cy), 110, (212, 175, 55, 255), (139, 30, 30, 240))
    
    # Body / Lorica Hamata (Chainmail armor chest)
    draw.polygon([(100, 130), (156, 130), (166, 210), (90, 210)], fill=(160, 165, 175, 255), outline=(90, 95, 105, 255), width=2)
    # Chainmail texture lines
    for y in range(145, 205, 12):
        draw.line([(96, y), (160, y)], fill=(120, 125, 135, 255), width=2)
        
    # Head & Helmet (Montefortino bronze helmet)
    draw.ellipse([108, 65, 148, 115], fill=(212, 175, 55, 255), outline=(160, 120, 30, 255), width=2)
    # Cheek guards (Paragnathides)
    draw.polygon([(112, 95), (116, 125), (124, 118)], fill=(195, 155, 45, 255))
    draw.polygon([(144, 95), (140, 125), (132, 118)], fill=(195, 155, 45, 255))
    # Helmet apex knob & red crest horsehair
    draw.ellipse([124, 58, 132, 66], fill=(212, 175, 55, 255))
    draw.arc([115, 38, 141, 75], start=200, end=340, fill=(220, 40, 40, 255), width=5)
    
    # Scutum Shield (Curved Rectangular Red Shield held on left arm)
    # Draw curved scutum
    draw.rounded_rectangle([(42, 105), (105, 215)], radius=12, fill=(185, 28, 28, 255), outline=(212, 175, 55, 255), width=3)
    # Shield Boss (Bronze Umbo)
    draw.ellipse([64, 150, 84, 170], fill=(212, 175, 55, 255), outline=(140, 100, 20, 255), width=2)
    # Golden Eagle Wings / Lightning bolt on shield
    draw.line([(52, 140), (74, 155)], fill=(245, 215, 115, 255), width=3)
    draw.line([(74, 165), (52, 180)], fill=(245, 215, 115, 255), width=3)
    draw.line([(96, 140), (74, 155)], fill=(245, 215, 115, 255), width=3)
    draw.line([(74, 165), (96, 180)], fill=(245, 215, 115, 255), width=3)
    
    # Pilum / Gladius on right hand
    draw.line([(155, 80), (195, 220)], fill=(180, 185, 195, 255), width=4)
    # Pilum spearhead
    draw.polygon([(155, 80), (150, 70), (160, 72)], fill=(210, 215, 225, 255))
    
    # Banner Text
    draw.rectangle([(68, 215), (188, 235)], fill=(20, 20, 25, 230), outline=(212, 175, 55, 255), width=1)
    draw.text((78, 218), "LEGIO ROMANA", fill=(245, 215, 115, 255))
    
    out_file = os.path.join(ICONS_DIR, "roman_legionary.png")
    img.save(out_file)
    print(f"[ICON] Generated Roman Legionary: {out_file}")
    return out_file

def generate_numidian_cavalry():
    """Kỵ binh nhẹ Numidia: Chiến binh Bắc Phi da màu cưỡi tuấn mã Berber không yên, phi tiêu javelin"""
    img = create_base_canvas()
    draw = ImageDraw.Draw(img)
    cx, cy = 128, 128
    
    # Base Token: Royal Navy Blue / Gold rim
    draw_token_base(draw, (cx, cy), 110, (212, 175, 55, 255), (26, 54, 93, 240))
    
    # Berber Horse Head & Neck Profile
    horse_pts = [
        (60, 200), (95, 140), (130, 110), (155, 115), 
        (165, 135), (145, 150), (140, 175), (115, 215)
    ]
    draw.polygon(horse_pts, fill=(115, 80, 50, 255), outline=(70, 45, 25, 255), width=2)
    # Horse mane & ear
    draw.polygon([(128, 105), (134, 88), (142, 108)], fill=(50, 35, 20, 255))
    draw.line([(100, 135), (125, 112)], fill=(40, 25, 15, 255), width=3)
    
    # Numidian Rider Torso (Dark skin, short white/ochre tunic)
    draw.polygon([(135, 80), (175, 80), (185, 145), (145, 145)], fill=(210, 195, 170, 255), outline=(150, 130, 105, 255), width=2)
    # Head (Dark bronze tone, braided hair)
    draw.ellipse([142, 45, 172, 78], fill=(135, 90, 60, 255), outline=(85, 50, 30, 255), width=2)
    draw.arc([140, 43, 174, 65], start=180, end=360, fill=(35, 25, 20, 255), width=5)
    
    # Small Round Hide Shield (Caetra / Pelte)
    draw.ellipse([105, 95, 155, 145], fill=(180, 140, 80, 255), outline=(212, 175, 55, 255), width=3)
    draw.ellipse([124, 114, 136, 126], fill=(70, 45, 25, 255))
    
    # Javelins / Flying Darts (3 phóng lao nhọn chỉ thẳng về phía trước)
    draw.line([(165, 75), (225, 45)], fill=(195, 200, 210, 255), width=3)
    draw.line([(168, 80), (220, 55)], fill=(195, 200, 210, 255), width=3)
    draw.line([(162, 85), (215, 65)], fill=(195, 200, 210, 255), width=3)
    # Spearheads
    draw.polygon([(225, 45), (218, 43), (222, 50)], fill=(240, 240, 245, 255))
    draw.polygon([(220, 55), (213, 53), (217, 60)], fill=(240, 240, 245, 255))
    
    # Banner Text
    draw.rectangle([(68, 215), (188, 235)], fill=(20, 20, 25, 230), outline=(212, 175, 55, 255), width=1)
    draw.text((78, 218), "NUMIDIA CAVALRY", fill=(245, 215, 115, 255))
    
    out_file = os.path.join(ICONS_DIR, "numidian_cavalry.png")
    img.save(out_file)
    print(f"[ICON] Generated Numidian Cavalry: {out_file}")
    return out_file

def generate_gallic_warrior():
    """Chiến binh Celt/Gallic: Mình trần râu tóc dài xơ xác, vòng cổ Torc vàng, gươm Spatha, khiên bầu dục họa tiết Celtic"""
    img = create_base_canvas()
    draw = ImageDraw.Draw(img)
    cx, cy = 128, 128
    
    # Base Token: Carthage Navy / Amber
    draw_token_base(draw, (cx, cy), 110, (230, 160, 40, 255), (42, 60, 80, 240))
    
    # Torso (Bare chest, muscular, war paint blue woad marks)
    draw.polygon([(95, 125), (160, 125), (150, 205), (105, 205)], fill=(225, 190, 165, 255), outline=(160, 125, 100, 255), width=2)
    # Blue woad body paint swirls
    draw.arc([110, 140, 145, 175], start=30, end=210, fill=(45, 115, 185, 255), width=3)
    draw.line([(120, 180), (140, 195)], fill=(45, 115, 185, 255), width=2)
    
    # Golden Torc around neck
    draw.arc([112, 116, 144, 134], start=0, end=180, fill=(245, 215, 55, 255), width=4)
    draw.ellipse([110, 122, 116, 128], fill=(245, 215, 55, 255))
    draw.ellipse([140, 122, 146, 128], fill=(245, 215, 55, 255))
    
    # Head & Long Reddish Hair / Moustache
    draw.ellipse([110, 60, 146, 105], fill=(225, 190, 165, 255))
    # Hair & Beard
    draw.arc([102, 52, 154, 115], start=160, end=380, fill=(185, 75, 30, 255), width=6)
    # Long Moustache
    draw.arc([114, 88, 142, 105], start=0, end=180, fill=(185, 75, 30, 255), width=3)
    
    # Oval Celtic Wooden Shield (Green/Gold with vertical rib)
    draw.ellipse([45, 95, 105, 215], fill=(45, 95, 65, 255), outline=(212, 175, 55, 255), width=3)
    draw.line([(75, 95), (75, 215)], fill=(212, 175, 55, 255), width=2)
    draw.ellipse([67, 145, 83, 165], fill=(180, 130, 40, 255), outline=(245, 215, 55, 255), width=2)
    
    # Long Iron Spatha Sword held high
    draw.line([(155, 135), (205, 55)], fill=(200, 205, 215, 255), width=4)
    # Crossguard & Pommel
    draw.line([(148, 130), (162, 140)], fill=(180, 140, 40, 255), width=3)
    
    # Banner Text
    draw.rectangle([(68, 215), (188, 235)], fill=(20, 20, 25, 230), outline=(212, 175, 55, 255), width=1)
    draw.text((80, 218), "CELT & GAUL INF", fill=(245, 215, 115, 255))
    
    out_file = os.path.join(ICONS_DIR, "celtic_gallic_warrior.png")
    img.save(out_file)
    print(f"[ICON] Generated Gallic Warrior: {out_file}")
    return out_file

def generate_carthaginian_veteran():
    """Cựu binh Carthage/Libya: Giáp vảy đồng lấy từ Trasimene, mũ Phrygian lông trắng, khiên tròn Aspis, giáo dài"""
    img = create_base_canvas()
    draw = ImageDraw.Draw(img)
    cx, cy = 128, 128
    
    # Base Token: Carthage Navy / Gold
    draw_token_base(draw, (cx, cy), 110, (212, 175, 55, 255), (18, 38, 72, 245))
    
    # Bronze Scale Armor (Trasimene trophy armor)
    draw.polygon([(100, 125), (156, 125), (165, 205), (92, 205)], fill=(195, 145, 55, 255), outline=(130, 95, 30, 255), width=2)
    # Bronze scales
    for y in range(138, 200, 12):
        for x in range(104, 154, 12):
            draw.arc([x, y, x + 10, y + 8], start=0, end=180, fill=(140, 100, 30, 255), width=2)
            
    # Helmet (Phrygian / Hellenistic bronze with white crest)
    draw.polygon([(110, 80), (146, 80), (140, 115), (114, 115)], fill=(195, 145, 55, 255))
    draw.polygon([(122, 50), (145, 55), (135, 80), (120, 80)], fill=(195, 145, 55, 255))
    # White horsehair crest
    draw.arc([115, 32, 155, 75], start=190, end=350, fill=(245, 245, 250, 255), width=6)
    
    # Large Round Aspis Shield (Carthage Tanit emblem)
    draw.ellipse([42, 100, 112, 215], fill=(30, 48, 85, 255), outline=(212, 175, 55, 255), width=3)
    # Tanit golden symbol on shield
    draw.ellipse([70, 135, 84, 149], fill=(212, 175, 55, 255))
    draw.line([(62, 155), (92, 155)], fill=(212, 175, 55, 255), width=3)
    draw.polygon([(77, 155), (65, 185), (89, 185)], fill=(212, 175, 55, 255))
    
    # Long Dory Spear
    draw.line([(155, 45), (195, 220)], fill=(175, 140, 95, 255), width=4)
    draw.polygon([(155, 45), (150, 32), (160, 35)], fill=(225, 230, 240, 255))
    
    # Banner Text
    draw.rectangle([(68, 215), (188, 235)], fill=(20, 20, 25, 230), outline=(212, 175, 55, 255), width=1)
    draw.text((78, 218), "LIBYAN VETERANS", fill=(245, 215, 115, 255))
    
    out_file = os.path.join(ICONS_DIR, "carthaginian_veteran.png")
    img.save(out_file)
    print(f"[ICON] Generated Carthaginian Veteran: {out_file}")
    return out_file

def generate_iberian_falcata():
    """Chiến binh Iberia: Áo chẽn trắng viền tím, mũ da sinew, khiên tròn caetra, kiếm cong Falcata"""
    img = create_base_canvas()
    draw = ImageDraw.Draw(img)
    cx, cy = 128, 128
    
    # Base Token: Spanish Bronze / Ochre
    draw_token_base(draw, (cx, cy), 110, (212, 175, 55, 255), (60, 45, 35, 240))
    
    # White tunic with purple border
    draw.polygon([(100, 125), (156, 125), (165, 205), (92, 205)], fill=(240, 240, 245, 255), outline=(105, 35, 85, 255), width=3)
    draw.line([(96, 175), (160, 175)], fill=(120, 35, 95, 255), width=4)
    
    # Head & Sinew Leather Cap
    draw.ellipse([112, 65, 146, 110], fill=(225, 190, 160, 255))
    draw.arc([108, 60, 150, 95], start=180, end=360, fill=(75, 55, 45, 255), width=5)
    
    # Small Round Caetra Shield
    draw.ellipse([48, 110, 108, 195], fill=(130, 90, 50, 255), outline=(212, 175, 55, 255), width=3)
    draw.ellipse([70, 144, 86, 160], fill=(212, 175, 55, 255))
    
    # Legendary Curved Falcata Blade
    # Curved blade shape
    falcata_pts = [
        (150, 140), (165, 115), (185, 85), (200, 75), 
        (195, 95), (175, 125), (155, 150)
    ]
    draw.polygon(falcata_pts, fill=(215, 220, 230, 255), outline=(120, 125, 135, 255), width=1)
    # Horse-head hilt
    draw.ellipse([144, 145, 154, 155], fill=(195, 145, 50, 255))
    
    # Banner Text
    draw.rectangle([(68, 215), (188, 235)], fill=(20, 20, 25, 230), outline=(212, 175, 55, 255), width=1)
    draw.text((80, 218), "IBERIAN FALCATA", fill=(245, 215, 115, 255))
    
    out_file = os.path.join(ICONS_DIR, "iberian_falcata.png")
    img.save(out_file)
    print(f"[ICON] Generated Iberian Falcata: {out_file}")
    return out_file

def generate_roman_cavalry():
    """Kỵ binh La Mã (Equites): Quý tộc La Mã đội mũ giáp Hy Lạp-La Mã, khiên tròn Parma, giáo dài Hasta"""
    img = create_base_canvas()
    draw = ImageDraw.Draw(img)
    cx, cy = 128, 128
    
    # Base Token: Roman Crimson
    draw_token_base(draw, (cx, cy), 110, (212, 175, 55, 255), (139, 30, 30, 240))
    
    # Roman Patrician Cavalry Torso (Muscled Cuirass bronze giáp cơ bắp)
    draw.polygon([(100, 125), (156, 125), (165, 205), (92, 205)], fill=(195, 145, 55, 255), outline=(130, 95, 30, 255), width=2)
    # Cuirass muscle lines
    draw.arc([110, 140, 145, 170], start=0, end=180, fill=(150, 105, 35, 255), width=2)
    
    # Attic Helmet with purple/red feather
    draw.ellipse([110, 65, 148, 112], fill=(212, 175, 55, 255), outline=(150, 110, 30, 255), width=2)
    draw.line([(129, 65), (129, 45)], fill=(210, 40, 40, 255), width=5)
    
    # Round Parma Shield (Red & Gold)
    draw.ellipse([45, 105, 115, 200], fill=(165, 25, 25, 255), outline=(212, 175, 55, 255), width=3)
    draw.ellipse([70, 142, 90, 162], fill=(212, 175, 55, 255))
    
    # Hasta Cavalry Spear
    draw.line([(155, 55), (195, 220)], fill=(160, 125, 80, 255), width=4)
    draw.polygon([(155, 55), (150, 40), (160, 43)], fill=(225, 230, 240, 255))
    
    # Banner Text
    draw.rectangle([(68, 215), (188, 235)], fill=(20, 20, 25, 230), outline=(212, 175, 55, 255), width=1)
    draw.text((80, 218), "ROMAN EQUITES", fill=(245, 215, 115, 255))
    
    out_file = os.path.join(ICONS_DIR, "roman_cavalry.png")
    img.save(out_file)
    print(f"[ICON] Generated Roman Equites: {out_file}")
    return out_file

if __name__ == "__main__":
    generate_roman_legionary()
    generate_numidian_cavalry()
    generate_gallic_warrior()
    generate_carthaginian_veteran()
    generate_iberian_falcata()
    generate_roman_cavalry()
    print("[SUCCESS] All 6 Historical Soldier Unit Icons generated successfully!")
