---
import {t} from "astro-i18n";
import {getCollection, type CollectionEntry} from "astro:content";
import PageLayout from "@layouts/PageLayout.astro";
import {Image} from "astro:assets";
import {l} from "@utils/util";

const imageMap = {
    "wg": await getRandomFromMode("wargear"),
    "mwg": await getRandomFromMode("miniwargear"),
    "as": await getRandomFromMode("airship"),
    "ws": await getRandomFromMode("warship"),
    "qg": await getRandomFromMode("quickgear"),
};

async function getRandomFromMode(mode: "wargear" | "airship" | "megawargear" | "microwargear" | "miniwargear" | "quickgear" | "streetfight" | "warship"): Promise<CollectionEntry<"publics">> {
    const publics = await getCollection("publics", entry => entry.data.gamemode.id === mode);

    return publics[Math.floor(Math.random() * publics.length)];
}

const modes = await getCollection("modes", entry => entry.data.main);
---

<PageLayout title={t("rules.page")}>
    {modes.map(value => (
        <div class="dark:bg-neutral-800 rounded-md p-4 border border-neutral-400 shadow-md my-4 flex flex-col
                 md:flex-row">
            <a href={l(`/publics/${imageMap[value.data.translationKey].id}`)}>
                <Image height="200" width="200" src={imageMap[value.data.translationKey].data.image}
                    alt={t("rules." + value.data.translationKey + ".title")} class="h-full aspect-square max-w-fit"></Image>
            </a>
            <div class="ml-4">
                <a href={l(`/rules/${value.id}`)}>
                    <h1 class="text-2xl font-bold">{t(value.data.translationKey + ".title")}</h1>
                    <div>{t("rules." + value.data.translationKey + ".description")}</div>
                </a>
                <div class="mt-2 flex flex-col">
                    <a href={l(`/publics/${value.id}`)} class="text-yellow-300 hover:underline w-fit">{t("rules.publics")}</a>
                    {value.data.ranked
                        ? <a href={l(`/ranked/${value.id}`)}
                            class="text-yellow-300 hover:underline w-fit">{t("rules.ranking")}</a>
                        : null}
                </div>
            </div>
        </div>))}
    <a href={l("/rangliste/MissileWars")}>MissileWars Rangliste</a>
</PageLayout>