import os
from PIL import Image, ImageDraw

def create_icon(name, draw_func):
    img = Image.new('RGBA', (16, 16), (255, 255, 255, 0))
    draw = ImageDraw.Draw(img)
    draw_func(draw)
    
    os.makedirs('icons', exist_ok=True)
    img.save(f'icons/{name}.png')

# 1. new_file
def draw_new_file(draw):
    draw.rectangle([3, 2, 11, 14], outline="black", fill="white")
    draw.line([7, 5, 7, 11], fill="green", width=2)
    draw.line([4, 8, 10, 8], fill="green", width=2)
create_icon('menu_new_file', draw_new_file)

# 2. new_folder
def draw_new_folder(draw):
    draw.polygon([(2,4), (6,4), (8,6), (14,6), (14,13), (2,13)], outline="black", fill="#FFD700")
    draw.line([8, 7, 8, 11], fill="green", width=2)
    draw.line([6, 9, 10, 9], fill="green", width=2)
create_icon('menu_new_folder', draw_new_folder)

# 3. open (folder)
def draw_open(draw):
    draw.polygon([(2,4), (6,4), (8,6), (14,6), (14,12), (2,12)], outline="black", fill="#FFD700")
    draw.polygon([(4,14), (16,14), (13,7), (1,7)], outline="black", fill="#FFF8DC")
create_icon('menu_open', draw_open)

# 4. save (floppy)
def draw_save(draw):
    draw.rectangle([2, 2, 14, 14], outline="black", fill="#4169E1")
    draw.rectangle([4, 2, 12, 6], outline="black", fill="white")
    draw.rectangle([5, 9, 11, 14], outline="black", fill="gray")
create_icon('menu_save', draw_save)

# 5. save_as (floppy + pencil)
def draw_save_as(draw):
    draw.rectangle([2, 2, 14, 14], outline="black", fill="#4169E1")
    draw.rectangle([4, 2, 12, 6], outline="black", fill="white")
    # Pencil
    draw.line([8, 14, 14, 8], fill="yellow", width=2)
    draw.point([7, 15], fill="black")
create_icon('menu_save_as', draw_save_as)

# 6. rename (pencil)
def draw_rename(draw):
    draw.polygon([(2,14), (4,14), (13,5), (11,3), (2,12)], outline="black", fill="yellow")
    draw.polygon([(11,3), (13,5), (14,4), (12,2)], outline="black", fill="pink")
    draw.point([2, 14], fill="black")
create_icon('menu_rename', draw_rename)

# 7. delete (trash)
def draw_delete(draw):
    draw.rectangle([4, 5, 12, 14], outline="black", fill="gray")
    draw.rectangle([3, 3, 13, 5], outline="black", fill="darkgray")
    draw.line([7, 2, 9, 2], fill="black", width=2)
    draw.line([6, 6, 6, 12], fill="black")
    draw.line([8, 6, 8, 12], fill="black")
    draw.line([10, 6, 10, 12], fill="black")
create_icon('menu_delete', draw_delete)

# 8. poweroff
def draw_poweroff(draw):
    draw.arc([3, 3, 13, 13], start=120, end=60, fill="red", width=2)
    draw.line([8, 2, 8, 8], fill="red", width=2)
create_icon('menu_poweroff', draw_poweroff)

# 9. reboot
def draw_reboot(draw):
    draw.arc([3, 3, 13, 13], start=30, end=300, fill="orange", width=2)
    draw.polygon([(11,1), (13,4), (15,1)], fill="orange")
create_icon('menu_reboot', draw_reboot)

# 10. logoff
def draw_logoff(draw):
    draw.rectangle([2, 2, 8, 14], outline="black", fill="brown")
    draw.point([6, 8], fill="yellow")
    draw.line([9, 8, 14, 8], fill="blue", width=2)
    draw.polygon([(14,6), (16,8), (14,10)], fill="blue")
create_icon('menu_logoff', draw_logoff)

# --- NEW ICONS ---

# 11. parametres (gear)
def draw_parametres(draw):
    import math
    draw.ellipse([2, 2, 13, 13], outline="gray", width=2)
    draw.ellipse([6, 6, 9, 9], fill="gray")
    for angle in range(0, 360, 45):
        rad = math.radians(angle)
        x1 = 7.5 + 4 * math.cos(rad)
        y1 = 7.5 + 4 * math.sin(rad)
        x2 = 7.5 + 6 * math.cos(rad)
        y2 = 7.5 + 6 * math.sin(rad)
        draw.line([x1, y1, x2, y2], fill="gray", width=2)
create_icon('parametres', draw_parametres)

# 12. taskmgr (graph)
def draw_taskmgr(draw):
    draw.rectangle([2, 2, 14, 14], outline="black", fill="black")
    draw.line([3, 10, 6, 10, 8, 4, 10, 12, 12, 8, 14, 8], fill="green", width=1)
create_icon('taskmgr', draw_taskmgr)

# 13. editeur (code brackets)
def draw_editeur(draw):
    draw.rectangle([2, 2, 14, 14], outline="blue", fill="white")
    draw.line([6, 5, 3, 8, 6, 11], fill="blue", width=2)
    draw.line([10, 5, 13, 8, 10, 11], fill="blue", width=2)
create_icon('editeur', draw_editeur)

# 14. xterm (terminal prompt)
def draw_xterm(draw):
    draw.rectangle([1, 1, 15, 15], outline="gray", fill="black")
    draw.line([3, 4, 6, 7, 3, 10], fill="white", width=1)
    draw.line([7, 10, 10, 10], fill="white", width=1)
create_icon('xterm', draw_xterm)

# 15. visionneuse (picture)
def draw_visionneuse(draw):
    draw.rectangle([2, 2, 14, 14], outline="brown", fill="white")
    draw.polygon([(2,14), (6,8), (9,11), (11,9), (14,14)], fill="green")
    draw.ellipse([3, 3, 5, 5], fill="yellow")
create_icon('visionneuse', draw_visionneuse)

# 16. btn_apply (check mark)
def draw_apply(draw):
    draw.line([3, 8, 6, 13, 14, 3], fill="green", width=3)
create_icon('btn_apply', draw_apply)

# 17. btn_refresh (circular arrow)
def draw_refresh(draw):
    draw.arc([2, 2, 14, 14], start=30, end=300, fill="blue", width=2)
    draw.polygon([(12,1), (14,4), (16,1)], fill="blue")
create_icon('btn_refresh', draw_refresh)

# 18. btn_kill (X)
def draw_kill(draw):
    draw.line([3, 3, 13, 13], fill="red", width=3)
    draw.line([13, 3, 3, 13], fill="red", width=3)
create_icon('btn_kill', draw_kill)

# 19. btn_start (star)
def draw_start(draw):
    draw.polygon([(8,1), (10,6), (15,6), (11,9), (12,14), (8,11), (4,14), (5,9), (1,6), (6,6)], fill="gold", outline="orange")
create_icon('btn_start', draw_start)

# 20. btn_wifi
def draw_wifi(draw):
    draw.arc([1, 1, 15, 15], start=225, end=315, fill="black", width=2)
    draw.arc([4, 4, 12, 12], start=225, end=315, fill="black", width=2)
    draw.ellipse([7, 11, 9, 13], fill="black")
create_icon('btn_wifi', draw_wifi)

# 21. btn_usb
def draw_usb(draw):
    draw.polygon([(8,2), (4,6), (12,6)], fill="black")
    draw.rectangle([6, 6, 10, 10], fill="black")
    draw.line([2, 13, 14, 13], fill="black", width=2)
create_icon('btn_usb', draw_usb)

# 22. btn_help
def draw_help(draw):
    draw.ellipse([1, 1, 15, 15], outline="blue", width=2)
    draw.arc([5, 4, 11, 10], start=180, end=0, fill="blue", width=2)
    draw.line([11, 7, 11, 9], fill="blue", width=2)
    draw.line([11, 9, 8, 9], fill="blue", width=2)
    draw.line([8, 9, 8, 11], fill="blue", width=2)
    draw.ellipse([7, 12, 9, 14], fill="blue")
create_icon('btn_help', draw_help)

# 23. blocnotes
def draw_blocnotes(draw):
    draw.rectangle([2, 1, 14, 15], outline="black", fill="white")
    draw.rectangle([2, 1, 14, 4], outline="black", fill="blue")
    draw.line([4, 6, 12, 6], fill="gray", width=1)
    draw.line([4, 9, 10, 9], fill="gray", width=1)
    draw.line([4, 12, 12, 12], fill="gray", width=1)
create_icon('blocnotes', draw_blocnotes)

# 24. terminal
def draw_terminal(draw):
    draw.rectangle([1, 2, 15, 14], outline="darkgray", fill="black")
    draw.rectangle([1, 2, 15, 4], outline="darkgray", fill="gray")
    draw.line([3, 6, 6, 6], fill="lightgreen", width=1)
    draw.line([3, 8, 5, 8], fill="lightgreen", width=1)
create_icon('terminal', draw_terminal)

# 25. explorateur
def draw_explorateur(draw):
    draw.polygon([(1,3), (6,3), (8,5), (15,5), (15,13), (1,13)], outline="black", fill="#FFD700")
    draw.polygon([(3,6), (15,6), (13,13), (1,13)], outline="black", fill="#FFF8DC")
create_icon('explorateur', draw_explorateur)

# 26. navigateur
def draw_navigateur(draw):
    draw.ellipse([2, 2, 14, 14], outline="darkblue", fill="cyan")
    draw.ellipse([5, 2, 11, 14], outline="blue", fill=None)
    draw.line([2, 8, 14, 8], fill="blue", width=1)
    draw.line([4, 5, 12, 5], fill="blue", width=1)
    draw.line([4, 11, 12, 11], fill="blue", width=1)
create_icon('navigateur', draw_navigateur)

# 27. grimoire
def draw_grimoire(draw):
    draw.polygon([(4,1), (12,1), (14,3), (14,13), (12,15), (4,15), (2,13), (2,3)], outline="black", fill="#F5DEB3")
    draw.line([(5,4), (11,4)], fill="brown", width=1)
    draw.line([(5,6), (10,6)], fill="brown", width=1)
    draw.line([(5,8), (11,8)], fill="brown", width=1)
    draw.line([(5,10), (9,10)], fill="brown", width=1)
    draw.ellipse([7, 12, 9, 14], fill="red") # sceau
create_icon('menu_grimoire', draw_grimoire)

# 28. icon_folder
def draw_icon_folder(draw):
    draw.polygon([(1,3), (6,3), (8,5), (15,5), (15,13), (1,13)], outline="black", fill="#FFD700")
create_icon('icon_folder', draw_icon_folder)

# 29. icon_file_generic
def draw_icon_file_generic(draw):
    draw.polygon([(3,1), (9,1), (13,5), (13,15), (3,15)], outline="black", fill="white")
    draw.polygon([(9,1), (9,5), (13,5)], outline="black", fill="lightgray")
create_icon('icon_file_generic', draw_icon_file_generic)

# 30. icon_file_txt
def draw_icon_file_txt(draw):
    draw_icon_file_generic(draw)
    draw.line([(5,6), (11,6)], fill="gray", width=1)
    draw.line([(5,9), (11,9)], fill="gray", width=1)
    draw.line([(5,12), (9,12)], fill="gray", width=1)
create_icon('icon_file_txt', draw_icon_file_txt)

# 31. icon_file_py
def draw_icon_file_py(draw):
    draw_icon_file_generic(draw)
    draw.rectangle([5, 6, 8, 9], fill="#3776AB") # Bleu Python
    draw.rectangle([8, 9, 11, 12], fill="#FFD43B") # Jaune Python
create_icon('icon_file_py', draw_icon_file_py)

# 32. icon_file_img
def draw_icon_file_img(draw):
    draw_icon_file_generic(draw)
    draw.polygon([(4,14), (8,9), (12,14)], fill="green") # Montagne
    draw.ellipse([8, 6, 11, 9], fill="orange") # Soleil
create_icon('icon_file_img', draw_icon_file_img)

# 33. camera
def draw_camera(draw):
    draw.rectangle([2, 5, 14, 13], outline="black", fill="#555")
    draw.rectangle([4, 3, 8, 5], outline="black", fill="#888")
    draw.ellipse([5, 6, 11, 12], outline="black", fill="lightblue")
    draw.ellipse([7, 8, 9, 10], fill="white")
    draw.ellipse([11, 6, 13, 8], fill="yellow") # Flash
create_icon('camera', draw_camera)

# 34. btn_audio
def draw_audio(draw):
    draw.polygon([(4, 6), (8, 6), (12, 2), (12, 14), (8, 10), (4, 10)], fill="white", outline="black")
    draw.arc([10, 4, 14, 12], 270, 90, fill="lightblue")
    draw.arc([8, 2, 16, 14], 270, 90, fill="lightblue")
create_icon('btn_audio', draw_audio)

# 35. imprimante
def draw_printer(draw):
    # Paper
    draw.rectangle([8, 2, 16, 6], fill="white", outline="black")
    # Body
    draw.rectangle([4, 6, 20, 14], fill="gray", outline="black")
    # Output tray / Paper out
    draw.rectangle([8, 14, 16, 18], fill="white", outline="black")
    # Slot and lines
    draw.line([6, 10, 18, 10], fill="black", width=2)
    draw.line([9, 15, 15, 15], fill="black")
    draw.line([9, 17, 15, 17], fill="black")
create_icon('imprimante', draw_printer)

print("Icons generated.")

# --- EMOJI REPLACEMENTS ---

# 43. btn_search
def draw_btn_search(draw):
    draw.ellipse([3, 3, 10, 10], outline="black", width=2)
    draw.line([9, 9, 14, 14], fill="black", width=3)
create_icon('btn_search', draw_btn_search)

# 44. btn_print
def draw_btn_print(draw):
    draw.rectangle([3, 6, 13, 12], outline="black", fill="gray")
    draw.rectangle([5, 2, 11, 6], outline="black", fill="white")
    draw.rectangle([4, 12, 12, 15], outline="black", fill="white")
    draw.line([5, 13, 11, 13], fill="black")
    draw.line([5, 14, 11, 14], fill="black")
create_icon('btn_print', draw_btn_print)

# 45. btn_refresh
def draw_btn_refresh(draw):
    draw.arc([2, 2, 14, 14], start=45, end=315, fill="green", width=2)
    draw.polygon([(11, 1), (13, 4), (15, 1)], fill="green")
create_icon('btn_refresh', draw_btn_refresh)

# 46. btn_network
def draw_btn_network(draw):
    draw.ellipse([2, 2, 14, 14], outline="blue", width=2)
    draw.ellipse([5, 2, 11, 14], outline="blue")
    draw.line([2, 8, 14, 8], fill="blue")
    draw.line([4, 4, 12, 4], fill="blue")
    draw.line([4, 12, 12, 12], fill="blue")
create_icon('btn_network', draw_btn_network)

# 47. btn_disconnect
def draw_btn_disconnect(draw):
    draw.line([2, 8, 6, 8], fill="red", width=2)
    draw.line([10, 8, 14, 8], fill="red", width=2)
    draw.rectangle([4, 6, 6, 10], fill="red")
    draw.rectangle([10, 6, 12, 10], fill="red")
create_icon('btn_disconnect', draw_btn_disconnect)

# 48. btn_paste
def draw_btn_paste(draw):
    draw.rectangle([4, 4, 12, 14], outline="black", fill="white")
    draw.rectangle([6, 2, 10, 5], outline="black", fill="gray")
    draw.line([5, 7, 11, 7], fill="black")
    draw.line([5, 9, 11, 9], fill="black")
    draw.line([5, 11, 11, 11], fill="black")
create_icon('btn_paste', draw_btn_paste)

# 49. btn_camera
def draw_btn_camera(draw):
    draw.rectangle([2, 5, 14, 13], outline="black", fill="gray")
    draw.rectangle([5, 3, 11, 5], outline="black", fill="darkgray")
    draw.ellipse([6, 7, 10, 11], outline="black", width=1)
    draw.point([12, 7], fill="red")
create_icon('btn_camera', draw_btn_camera)

# 50. btn_home
def draw_btn_home(draw):
    draw.polygon([(8, 2), (2, 7), (14, 7)], outline="black", fill="brown")
    draw.rectangle([4, 7, 12, 14], outline="black", fill="white")
    draw.rectangle([7, 10, 9, 14], outline="black", fill="brown")
create_icon('btn_home', draw_btn_home)

# 51. btn_trash
def draw_btn_trash(draw):
    draw.rectangle([4, 5, 12, 14], outline="black", fill="gray")
    draw.rectangle([3, 3, 13, 5], outline="black", fill="darkgray")
    draw.line([7, 2, 9, 2], fill="black", width=2)
    draw.line([6, 6, 6, 12], fill="black")
    draw.line([8, 6, 8, 12], fill="black")
    draw.line([10, 6, 10, 12], fill="black")
create_icon('btn_trash', draw_btn_trash)

# 52. btn_arrow_left
def draw_btn_arrow_left(draw):
    draw.polygon([(8, 2), (2, 8), (8, 14)], fill="blue")
    draw.rectangle([8, 6, 14, 10], fill="blue")
create_icon('btn_arrow_left', draw_btn_arrow_left)

# 53. btn_rocket
def draw_btn_rocket(draw):
    draw.polygon([(8, 1), (5, 6), (11, 6)], outline="black", fill="red")
    draw.rectangle([5, 6, 11, 12], outline="black", fill="white")
    draw.polygon([(5, 12), (3, 15), (5, 15)], fill="red")
    draw.polygon([(11, 12), (13, 15), (11, 15)], fill="red")
    draw.ellipse([6, 7, 10, 11], outline="blue")
create_icon('btn_rocket', draw_btn_rocket)

