<!--
  -   This file is a part of the SteamWar software.
  -
  -   Copyright (C) 2023 SteamWar.de-Serverteam
  -
  -   This program is free software: you can redistribute it and/or modify
  -   it under the terms of the GNU Affero General Public License as published by
  -   the Free Software Foundation, either version 3 of the License, or
  -   (at your option) any later version.
  -
  -   This program is distributed in the hope that it will be useful,
  -   but WITHOUT ANY WARRANTY; without even the implied warranty of
  -   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  -   GNU Affero General Public License for more details.
  -
  -   You should have received a copy of the GNU Affero General Public License
  -   along with this program.  If not, see <https://www.gnu.org/licenses/>.
  -->

<script lang="ts">
    import {Button, Modal} from "flowbite-svelte";
    import type {ExtendedEvent} from "@type/event.ts";
    import FightEditPart from "../../../components/FightEditPart.svelte";
    import ErrorModal from "../../../components/ErrorModal.svelte";
    import {createEventDispatcher} from "svelte";
    import dayjs from "dayjs";
    import {fightRepo} from "@repo/fight.ts";

    let dispatch = createEventDispatcher();

    export let open: boolean = false;
    export let data: ExtendedEvent;

    let blueTeam: string = "";
    let redTeam: string = "";

    let start: string = "";

    let gamemode: string = "";
    let map: string = "";

    let kampfleiter: string | undefined = undefined;
    let group: string | null = null;
    let groupSearch = "";

    let errorOpen = false;
    let error: any = undefined;

    $: canCreate = blueTeam !== "" && redTeam !== "" && start !== "" && gamemode !== "" && map !== "";

    async function create() {
        try {
            open = false;
            let res = await $fightRepo.createFight(data.event.id, {
                spielmodus: gamemode,
                blueTeam: parseInt(blueTeam),
                redTeam: parseInt(redTeam),
                start: dayjs(start),
                map,
                kampfleiter: parseInt(kampfleiter!),
                group,
            });
            reset();

            dispatch("create");
        } catch (e) {
            error = e;
            errorOpen = true;
            reset();
        }
    }

    function reset() {
        blueTeam = "";
        redTeam = "";
        start = "";
        gamemode = "";
        map = "";
        kampfleiter = "";
        group = null;
        groupSearch = "";
    }
</script>

<Modal bind:open outsideclose title="Create Fight" on:hide={reset}>
    <div class="text-center">
        <FightEditPart
                bind:blueTeam
                bind:redTeam
                bind:start
                bind:kampfleiter
                bind:gamemode
                bind:map
                bind:group
                bind:groupSearch
                teams={data.teams}
        />
    </div>
    <svelte:fragment slot="footer">
        <Button on:click={create} class="mr-auto" disabled={!canCreate}>Create</Button>
        <Button color="light" on:click={() => open = false}>Cancel</Button>
    </svelte:fragment>
</Modal>

<ErrorModal bind:open={errorOpen} bind:error={error} on:close={() => errorOpen = false}/>
