import os
import json
from pathlib import Path
from typing import List, Dict
from requests import get
from functools import cache

GITLAB_API_READ_TOKEN = "glpat-5BCYCid1WaxxBgax-7VW"

WELCOME_PAGE = """
<h1>Bienvenue dans MaX !</h1>
<p>
    Vous pouvez personnaliser cette page en éditant le fichier content_html/fr/index.html
</p>
<p>
    Retrouvez la documentation de MaX à l'adresse suivante: 
    <a href="https://pdn-certic.pages.unicaen.fr/max-documentation/">
        https://pdn-certic.pages.unicaen.fr/max-documentation/
    </a>
</p>
"""

USER_MAX_DIR = os.getenv("CLIMAX_HOME") or Path(Path.home(), ".climax")
USER_MAX_DIR = Path(USER_MAX_DIR)
USER_MAX_DIR.mkdir(exist_ok=True)
CACHE_DIR = Path(USER_MAX_DIR, "cache")
CACHE_DIR.mkdir(exist_ok=True)
MAX_CONFIG_FILE = "config.xml"
BASEX_DISTRO = "https://files.basex.org/releases/11.7/BaseX117.zip"
SAXON_DISTRO = (
    "https://repo1.maven.org/maven2/net/sf/saxon/Saxon-HE/10.8/Saxon-HE-10.8.jar"
)

MAX_DISTRO = "https://git.unicaen.fr/pdn-certic/max-v2/-/archive/0.0.2-alpha/max-v2-0.0.2-alpha.zip"


CURRENT_MAX_DISTRO_DIR = os.path.splitext(os.path.basename(MAX_DISTRO))[0]
JAVA_DISTROS = {
    "darwin/arm64": "https://download.java.net/java/GA/jdk22.0.2/c9ecb94cd31b495da20a27d4581645e8/9/GPL/openjdk-22.0.2_macos-aarch64_bin.tar.gz",
    "linux/x86_64": "https://download.java.net/java/GA/jdk22.0.2/c9ecb94cd31b495da20a27d4581645e8/9/GPL/openjdk-22.0.2_linux-x64_bin.tar.gz",
    "windows/amd64": "https://download.java.net/java/GA/jdk22.0.2/c9ecb94cd31b495da20a27d4581645e8/9/GPL/openjdk-22.0.2_windows-x64_bin.zip",
}
WEB_PORT = 8080
WEB_HOST = "127.0.0.1"
BASEX_PORT = 1984
STOP_PORT = 8081


@cache
def max_releases() -> List[Dict]:
    try:
        response = get(
            "https://git.unicaen.fr/api/v4/projects/2429/releases",
            headers={"PRIVATE-TOKEN": GITLAB_API_READ_TOKEN},
        )
        if response.ok:
            return json.loads(response.content)
    except:  # noqa: E722
        pass

    with open(
        Path(os.path.dirname(os.path.realpath(__file__)), "max_releases.json")
    ) as f:
        return json.load(f)
