import xml.etree.ElementTree as ET
from PIL import Image, ImageDraw, ImageFont
from coords import TOWN_COORDS

def parse_weather_data(xml_file, coord_set, forecast_index="0"):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    data = []

    for area in root.findall(".//area[@type='location']"):
        town = area.attrib.get("description", "Unknown")
        if town not in coord_set:
            continue

        forecast = area.find(f"forecast-period[@index='{forecast_index}']")
        if forecast is None:
            continue  
            
        precis_elem = forecast.find("text[@type='precis']")
        precis = precis_elem.text if precis_elem is not None else ""

        icon = forecast.find("element[@type='forecast_icon_code']").text
        tmax = forecast.find("element[@type='air_temperature_maximum']").text

        tmin = None
        if forecast_index != "0":
        
            tmin_elem = forecast.find("element[@type='air_temperature_minimum']")
            if tmin_elem is not None:
                tmin = tmin_elem.text

        print(f"{town}: {tmin} {tmax} {precis}")

        data.append({
            "name": town,
            "x": coord_set[town][0],
            "y": coord_set[town][1],
            "icon": icon,
            "precis": precis,
            "tmin": tmin,
            "tmax": tmax
        })

    return data

    print(f"🧊 {map_id}: Parsed {len(weather_data)} towns for forecast index {forecast_index}")
    for entry in weather_data:
        print(f" - {entry['name']}: Tmin={entry['tmin']} Tmax={entry['tmax']}")

def resolve_icon_filename(code, precis):
    p = precis.lower()

    if code == "3":
        if "mostly sunny" in p:
            return "3a.png"
        elif "partly cloudy" in p:
            return "3b.png"
        elif "mostly cloudy" in p:
            return "3c.png"
    return f"{code}.png"

def generate_map(xml_path, base_image_path, map_id=None, forecast_index="0", output_path=None):
    coord_set = TOWN_COORDS.get(map_id, {})
    weather_data = parse_weather_data(xml_path, coord_set, forecast_index)
    base = Image.open(base_image_path).convert("RGBA")
    draw = ImageDraw.Draw(base)
    font = ImageFont.truetype("/var/www/html/wgfx/static/fonts/barlow.ttf", 46)
    
    for entry in weather_data:
        tmin_label = f"{entry['tmin']}"
        tmax_label = f"{entry['tmax']}"
        
        try:
            filename = resolve_icon_filename(entry["icon"], entry["precis"])
            icon_path = f"/var/www/html/wgfx/static/icons/{filename}"
            icon_img = Image.open(icon_path).convert("RGBA").resize((70,70))
            icon_offset = 175 if entry["tmin"] else 100
            base.paste(icon_img, (entry["x"] + icon_offset, entry["y"] + 5), icon_img)
            
        except Exception as e:
            print(f"⚠️ Could not load icon {entry['icon']} for {entry['name']}: {e}")

        # Positioning
        label_box_width = 133
        half = label_box_width / 2

        # Draw tmin
        if entry["tmin"]:
            tmin_label = str(entry["tmin"])
            tmin_width = draw.textbbox((0, 0), tmin_label, font=font)[2]
            tmin_x = entry["x"] + half - (tmin_width / 2) - 33
            draw.text((tmin_x + 22, entry["y"] + 8), tmin_label, font=font, fill="#0a0963")

        # Draw tmax
        if entry["tmax"]:
            tmax_label = str(entry["tmax"])
            tmax_width = draw.textbbox((0, 0), tmax_label, font=font)[2]
            tmax_x = entry["x"] + 16 + half - (tmax_width / 2) + 33 if entry["tmin"] else entry["x"] + 33
            draw.text((tmax_x, entry["y"] + 8), tmax_label, font=font, fill="#ff6229")
            
    if output_path is None:
        output_path = "static/e.png"
    base.save(output_path)
    return output_path
