from flask import Flask, render_template, send_file
from gen import generate_map
from datetime import datetime
import zipfile
import os
import io
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

app = Flask(__name__)

if __name__ == "__main__":
    app.run(debug=True)

@app.route("/download-all")
def download_all_images():
    buffer = io.BytesIO()
    with zipfile.ZipFile(buffer, "w") as z:
        for filename in os.listdir("static"):
            if filename.endswith(".png") and filename.startswith("out"):
                z.write(os.path.join("static", filename), arcname=filename)
    buffer.seek(0)

    # Create a timestamp like "2025-07-06_22-45"
    timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M")
    zip_name = f"weather_maps_{timestamp}.zip"

    return send_file(
        buffer,
        mimetype="application/zip",
        as_attachment=True,
        download_name=zip_name
    )

@app.route("/")
def index():
    maps = [
    
        #QLD TODAY
        
        {"id": "qState", "day": "today", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_stateToday.png"), "qState", output_path=os.path.join(BASE_DIR, "static/outQ_stateToday.png"))},
        {"id": "qFarNorth", "day": "today", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_farNorthToday.png"), "qFarNorth", output_path=os.path.join(BASE_DIR, "static/outQ_farNorthToday.png"))},
        {"id": "qNorth", "day": "today", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_northToday.png"), "qNorth", output_path=os.path.join(BASE_DIR, "static/outQ_northToday.png"))},
        {"id": "qCentral", "day": "today", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_centralToday.png"), "qCentral", output_path=os.path.join(BASE_DIR, "/var/www/html/wgfx/static/outQ_centralToday.png"))},
        {"id": "qWideBay", "day": "today", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_wideBayToday.png"), "qWideBay", output_path=os.path.join(BASE_DIR, "static/outQ_wideBayToday.png"))},
        {"id": "qSunshine", "day": "today", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_sunshineToday.png"), "qSunshine", output_path=os.path.join(BASE_DIR, "static/outQ_sunshineToday.png"))},

        # #QLD TOMORROW

        {"id": "qStateTT", "day": "tomorrow", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_stateTomorrow.png"), "qState", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outQ_stateTomorrow.png"))},
        {"id": "qFarNorthTT", "day": "tomorrow", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_farNorthTomorrow.png"), "qFarNorth", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outQ_farNorthTomorrow.png"))},
        {"id": "qNorthTT", "day": "tomorrow", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_northTomorrow.png"), "qNorth", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outQ_northTomorrow.png"))},
        {"id": "qCentralTT", "day": "tomorrow", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_centralTomorrow.png"), "qCentral", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outQ_centralTomorrow.png"))},
        {"id": "qWideBayTT", "day": "tomorrow", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_wideBayTomorrow.png"), "qWideBay", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outQ_wideBayTomorrow.png"))},
        {"id": "qSunshineTT", "day": "tomorrow", "image": generate_map(os.path.join(BASE_DIR, "data/IDQ11295.xml"), os.path.join(BASE_DIR, "static/baseQ_sunshineTomorrow.png"), "qSunshine", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outQ_sunshineTomorrow.png"))},

        # #NSW TODAY

        # {"id": "nState", "image": generate_map("IDN11060.xml", "static/baseN_stateToday.png", "nState", output_path=os.path.join(BASE_DIR, "static/outN_stateToday.png")},
        # {"id": "nIllawara", "image": generate_map("IDN11060.xml", "static/baseN_illawaraToday.png", "nIllawara", output_path=os.path.join(BASE_DIR, "static/outN_illawaraToday.png")},
        # {"id": "nCapital", "image": generate_map("IDN11060.xml", "static/baseN_capitalToday.png", "nCapital", output_path=os.path.join(BASE_DIR, "static/outN_capitalToday.png")},
        # {"id": "nOrange", "image": generate_map("IDN11060.xml", "static/baseN_orangeToday.png", "nOrange", output_path=os.path.join(BASE_DIR, "static/outN_orangeToday.png")},
        # {"id": "nRiverina", "image": generate_map("IDN11060.xml", "static/baseN_riverinaToday.png", "nRiverina", output_path=os.path.join(BASE_DIR, "static/outN_riverinaToday.png")},

        # #NSW TOMORROW

        # {"id": "nStateTT", "image": generate_map("IDN11060.xml", "static/baseN_stateTomorrow.png", "nStateTT", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outN_stateTomorrow.png")},
        # {"id": "nIllawaraTT", "image": generate_map("IDN11060.xml", "static/baseN_illawaraTomorrow.png", "nIllawaraTT", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outN_illawaraTomorrow.png")},
        # {"id": "nCapitalTT", "image": generate_map("IDN11060.xml", "static/baseN_capitalTomorrow.png", "nCapitalTT", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outN_capitalTomorrow.png")},
        # {"id": "nOrangeTT", "image": generate_map("IDN11060.xml", "static/baseN_orangeTomorrow.png", "nOrangeTT", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outN_orangeTomorrow.png")},
        # {"id": "nRiverinaTT", "image": generate_map("IDN11060.xml", "static/baseN_riverinaTomorrow.png", "nRiverinaTT", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outN_riverinaTomorrow.png")},

        # #VIC TODAY

        # {"id": "vState", "image": generate_map("IDV10753.xml", "static/baseV_stateToday.png", "vState", output_path=os.path.join(BASE_DIR, "static/outV_stateToday.png")},
        # {"id": "vBorder", "image": generate_map("IDV10753.xml", "static/baseV_borderToday.png", "vBorder", output_path=os.path.join(BASE_DIR, "static/outV_borderToday.png")},
        # {"id": "vBallarat", "image": generate_map("IDV10753.xml", "static/baseV_ballaratToday.png", "vBallarat", output_path=os.path.join(BASE_DIR, "static/outV_ballaratToday.png")},
        # {"id": "vBendigo", "image": generate_map("IDV10753.xml", "static/baseV_bendigoToday.png", "vBendigo", output_path=os.path.join(BASE_DIR, "static/outV_bendigoToday.png")},
        # {"id": "vGippsland", "image": generate_map("IDV10753.xml", "static/baseV_gippslandToday.png", "vGippsland", output_path=os.path.join(BASE_DIR, "static/outV_gippslandToday.png")},

        # #VIC TOMORROW

        # {"id": "vStateTT", "image": generate_map("IDV10753.xml", "static/baseV_stateTomorrow.png", "vStateTT", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outV_stateTomorrow.png")},
        # {"id": "vBorderTT", "image": generate_map("IDV10753.xml", "static/baseV_borderTomorrow.png", "vBorderTT", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outV_borderTomorrow.png")},
        # {"id": "vBallaratTT", "image": generate_map("IDV10753.xml", "static/baseV_ballaratTomorrow.png", "vBallaratTT", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outV_ballaratTomorrow.png")},
        # {"id": "vBendigoTT", "image": generate_map("IDV10753.xml", "static/baseV_bendigoTomorrow.png", "vBendigoTT", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outV_bendigoTomorrow.png")},
        # {"id": "vGippslandTT", "image": generate_map("IDV10753.xml", "static/baseV_gippslandTomorrow.png", "vGippslandTT", forecast_index="1", output_path=os.path.join(BASE_DIR, "static/outV_gippslandTomorrow.png")},
 
 ]
    return render_template("index.html", maps=maps)

@app.route("/map")
def map():
    return send_file("static/outImg.png", mimetype="image/png")
