import pygame
import random
import os

pygame.init()
WIDTH, HEIGHT = 950, 680
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Fashion Dress Up Game")
clock = pygame.time.Clock()

# Colors
BG_COLOR = (230, 235, 245)
PANEL_COLOR = (255, 255, 255)
BUTTON_NORMAL = (70, 135, 200)
BUTTON_HOVER = (40, 105, 170)
SAVE_BTN_COLOR = (30, 170, 80)
TEXT_COLOR = (15, 15, 15)
WHITE = (255, 255, 255)

# Font
try:
    font_title = pygame.font.SysFont("Arial", 32)
    font_btn = pygame.font.SysFont("Arial", 20)
except:
    font_title = pygame.font.Font(None, 32)
    font_btn = pygame.font.Font(None, 20)

# ---------------------- Clothing Data ----------------------
hair_list = ["Long Hair", "Short Hair", "Ponytail", "Curly Hair"]
top_list = ["T-shirt", "Sweater", "Dress", "Blouse"]
pants_list = ["Jeans", "Skirt", "Shorts", "Wide Pants"]
shoe_list = ["Sneakers", "Heels", "Sandals", "Boots"]
bag_list = ["Handbag", "Backpack", "Clutch", "No Bag"]
skin_list = [(252, 224, 204), (230, 190, 160), (200, 155, 120), (160, 115, 85)]
bg_list = [(230, 235, 245), (220, 235, 220), (235, 222, 230), (225, 225, 240)]

# Current Selection
selected_hair = 0
selected_top = 0
selected_pants = 0
selected_shoes = 0
selected_bag = 0
selected_skin = 0
selected_bg = 0

# Button Rectangles
btn_hair = pygame.Rect(660, 90, 220, 52)
btn_top = pygame.Rect(660, 150, 220, 52)
btn_pants = pygame.Rect(660, 210, 220, 52)
btn_shoes = pygame.Rect(660, 270, 220, 52)
btn_bag = pygame.Rect(660, 330, 220, 52)
btn_skin = pygame.Rect(660, 390, 220, 52)
btn_bg = pygame.Rect(660, 450, 220, 52)
btn_reset = pygame.Rect(660, 510, 220, 52)
btn_save = pygame.Rect(660, 570, 220, 52)

char_center_x = 300
char_top_y = 100

def draw_button(rect, text, hover, custom_color=None):
    if custom_color:
        color = custom_color
    else:
        color = BUTTON_HOVER if hover else BUTTON_NORMAL
    pygame.draw.rect(screen, color, rect, border_radius=10)
    txt = font_btn.render(text, True, WHITE)
    screen.blit(txt, (rect.centerx - txt.get_width()//2, rect.centery - txt.get_height()//2))

def save_outfit_screenshot():
    if not os.path.exists("dressup_saves"):
        os.mkdir("dressup_saves")
    save_name = f"dressup_saves/outfit_{random.randint(1000,9999)}.png"
    pygame.image.save(screen, save_name)
    print(f"Saved: {save_name}")

running = True
while running:
    current_bg = bg_list[selected_bg]
    screen.fill(current_bg)
    mx, my = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = event.pos
            if btn_hair.collidepoint(pos):
                selected_hair = (selected_hair + 1) % len(hair_list)
            if btn_top.collidepoint(pos):
                selected_top = (selected_top + 1) % len(top_list)
            if btn_pants.collidepoint(pos):
                selected_pants = (selected_pants + 1) % len(pants_list)
            if btn_shoes.collidepoint(pos):
                selected_shoes = (selected_shoes + 1) % len(shoe_list)
            if btn_bag.collidepoint(pos):
                selected_bag = (selected_bag + 1) % len(bag_list)
            if btn_skin.collidepoint(pos):
                selected_skin = (selected_skin + 1) % len(skin_list)
            if btn_bg.collidepoint(pos):
                selected_bg = (selected_bg + 1) % len(bg_list)
            if btn_reset.collidepoint(pos):
                selected_hair=selected_top=selected_pants=selected_shoes=selected_bag=selected_skin=selected_bg=0
            if btn_save.collidepoint(pos):
                save_outfit_screenshot()

    # Title
    title = font_title.render("Fashion Dress Up", True, TEXT_COLOR)
    screen.blit(title, (30, 25))

    # Character Base Skin
    skin_color = skin_list[selected_skin]
    pygame.draw.circle(screen, skin_color, (char_center_x, char_top_y + 40), 38)

    # Hair
    hair_name = hair_list[selected_hair]
    if hair_name == "Long Hair":
        pygame.draw.ellipse(screen, (60, 35, 20), (char_center_x - 42, char_top_y, 84, 95))
    elif hair_name == "Short Hair":
        pygame.draw.circle(screen, (60, 35, 20), (char_center_x, char_top_y + 22), 40)
    elif hair_name == "Ponytail":
        pygame.draw.circle(screen, (60, 35, 20), (char_center_x, char_top_y + 22), 36)
        pygame.draw.rect(screen, (60, 35, 20), (char_center_x + 30, char_top_y + 18, 22, 65))
    elif hair_name == "Curly Hair":
        pygame.draw.circle(screen, (60, 35, 20), (char_center_x, char_top_y + 20), 44)

    # Top
    top_name = top_list[selected_top]
    if top_name == "T-shirt":
        pygame.draw.rect(screen, (40, 130, 210), (char_center_x - 35, char_top_y + 80, 70, 85), border_radius=8)
    elif top_name == "Sweater":
        pygame.draw.rect(screen, (220, 80, 80), (char_center_x - 38, char_top_y + 80, 76, 90), border_radius=8)
    elif top_name == "Dress":
        pygame.draw.polygon(screen, (140, 70, 180), [
            (char_center_x - 36, char_top_y + 80),
            (char_center_x + 36, char_top_y + 80),
            (char_center_x + 48, char_top_y + 180),
            (char_center_x - 48, char_top_y + 180)
        ])
    elif top_name == "Blouse":
        pygame.draw.rect(screen, (240, 160, 190), (char_center_x - 36, char_top_y + 80, 72, 82), border_radius=8)

    # Bottom (skip if wearing dress)
    if top_name != "Dress":
        pant_name = pants_list[selected_pants]
        if pant_name == "Jeans":
            pygame.draw.rect(screen, (30, 70, 140), (char_center_x - 32, char_top_y + 165, 28, 75))
            pygame.draw.rect(screen, (30, 70, 140), (char_center_x + 4, char_top_y + 165, 28, 75))
        elif pant_name == "Skirt":
            pygame.draw.polygon(screen, (220, 120, 160), [
                (char_center_x - 34, char_top_y + 165),
                (char_center_x + 34, char_top_y + 165),
                (char_center_x + 42, char_top_y + 215),
                (char_center_x - 42, char_top_y + 215)
            ])
        elif pant_name == "Shorts":
            pygame.draw.rect(screen, (70, 160, 90), (char_center_x - 32, char_top_y + 165, 64, 42), border_radius=6)
        elif pant_name == "Wide Pants":
            pygame.draw.rect(screen, (110, 90, 130), (char_center_x - 34, char_top_y + 165, 30, 80))
            pygame.draw.rect(screen, (110, 90, 130), (char_center_x + 2, char_top_y + 165, 30, 80))

    # Shoes
    shoe_name = shoe_list[selected_shoes]
    shoe_y = char_top_y + 240
    if shoe_name == "Sneakers":
        pygame.draw.ellipse(screen, (50,50,50), (char_center_x-32, shoe_y, 26,14))
        pygame.draw.ellipse(screen, (50,50,50), (char_center_x+6, shoe_y, 26,14))
    elif shoe_name == "Heels":
        pygame.draw.polygon(screen, (120,40,60), [(char_center_x-32,shoe_y),(char_center_x-8,shoe_y),(char_center_x-16,shoe_y+16)])
        pygame.draw.polygon(screen, (120,40,60), [(char_center_x+6,shoe_y),(char_center_x+30,shoe_y),(char_center_x+18,shoe_y+16)])
    elif shoe_name == "Sandals":
        pygame.draw.ellipse(screen, (230,180,100), (char_center_x-30, shoe_y, 22,12))
        pygame.draw.ellipse(screen, (230,180,100), (char_center_x+8, shoe_y, 22,12))
    elif shoe_name == "Boots":
        pygame.draw.rect(screen, (35,35,35), (char_center_x-30, shoe_y-12,24,26), border_radius=4)
        pygame.draw.rect(screen, (35,35,35), (char_center_x+6, shoe_y-12,24,26), border_radius=4)

    # Bag
    bag_name = bag_list[selected_bag]
    if bag_name != "No Bag":
        if bag_name == "Handbag":
            pygame.draw.rect(screen, (110,70,40), (char_center_x+42, char_top_y+110,26,36), border_radius=4)
        elif bag_name == "Backpack":
            pygame.draw.ellipse(screen, (70,90,140), (char_center_x-52, char_top_y+95,32,44))
        elif bag_name == "Clutch":
            pygame.draw.rect(screen, (160,30,60), (char_center_x+44, char_top_y+130,22,14), border_radius=3)

    # Draw All Buttons
    draw_button(btn_hair, f"Hair: {hair_list[selected_hair]}", btn_hair.collidepoint(mx,my))
    draw_button(btn_top, f"Top: {top_list[selected_top]}", btn_top.collidepoint(mx,my))
    draw_button(btn_pants, f"Bottom: {pants_list[selected_pants]}", btn_pants.collidepoint(mx,my))
    draw_button(btn_shoes, f"Shoes: {shoe_list[selected_shoes]}", btn_shoes.collidepoint(mx,my))
    draw_button(btn_bag, f"Bag: {bag_list[selected_bag]}", btn_bag.collidepoint(mx,my))
    draw_button(btn_skin, "Change Skin Tone", btn_skin.collidepoint(mx,my))
    draw_button(btn_bg, "Change Background", btn_bg.collidepoint(mx,my))
    draw_button(btn_reset, "Reset All Outfit", btn_reset.collidepoint(mx,my))
    draw_button(btn_save, "Save Outfit Screenshot", btn_save.collidepoint(mx,my), SAVE_BTN_COLOR)

    # Outfit Info Text
    info = font_btn.render(f"Outfit Saved to folder dressup_saves after clicking save button", True, TEXT_COLOR)
    screen.blit(info, (30, 620))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()