#!/usr/bin/python3
"""Dessine les fonds d'écran fournis avec P2i OS (dégradés aux couleurs P2i).
Lancé une fois, à la construction du système : creer-fonds DOSSIER"""
import os
import sys

os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
from PySide6.QtCore import QPointF, Qt
from PySide6.QtGui import (QColor, QGuiApplication, QImage, QLinearGradient,
                           QPainter, QRadialGradient)

FONDS = {
    "bleu-nuit": ("#0b2447", "#061226", [("#1976d2", 0.15, 0.2, 0.75, 150), ("#1e88e5", 0.9, 0.05, 0.5, 90)]),
    "aurore": ("#1a1042", "#0b2447", [("#e91e63", 0.2, 0.9, 0.7, 120), ("#ffb300", 0.85, 0.75, 0.55, 110), ("#1e88e5", 0.6, 0.1, 0.6, 90)]),
    "ocean": ("#01579b", "#002b4d", [("#26c6da", 0.8, 0.85, 0.7, 130), ("#1e88e5", 0.1, 0.1, 0.6, 120)]),
    "foret": ("#0f3d2e", "#061a14", [("#66bb6a", 0.85, 0.2, 0.6, 110), ("#26a69a", 0.15, 0.85, 0.7, 110)]),
    "ardoise": ("#37474f", "#1c262b", [("#90a4ae", 0.8, 0.15, 0.6, 90), ("#1976d2", 0.1, 0.9, 0.6, 70)]),
    "sable": ("#f1e3cf", "#d9bf9c", [("#ffb300", 0.85, 0.15, 0.6, 90), ("#ffffff", 0.15, 0.3, 0.6, 150)]),
    "clair": ("#eef3fb", "#cfdcf0", [("#1e88e5", 0.9, 0.9, 0.7, 70), ("#ffffff", 0.2, 0.2, 0.6, 200)]),
}


def main():
    dossier = sys.argv[1]
    os.makedirs(dossier, exist_ok=True)
    app = QGuiApplication(sys.argv[:1])  # noqa: F841
    l, h = 2560, 1440
    for nom, (haut, bas, halos) in FONDS.items():
        image = QImage(l, h, QImage.Format.Format_RGB32)
        p = QPainter(image)
        fond = QLinearGradient(0, 0, l * 0.3, h)
        fond.setColorAt(0, QColor(haut))
        fond.setColorAt(1, QColor(bas))
        p.fillRect(0, 0, l, h, fond)
        for couleur, x, y, rayon, alpha in halos:
            halo = QRadialGradient(QPointF(x * l, y * h), rayon * l)
            c = QColor(couleur)
            c.setAlpha(alpha)
            halo.setColorAt(0, c)
            c.setAlpha(0)
            halo.setColorAt(1, c)
            p.setPen(Qt.PenStyle.NoPen)
            p.fillRect(0, 0, l, h, halo)
        p.end()
        image.save(os.path.join(dossier, nom + ".png"))
    return 0


sys.exit(main())
