<!--
  -   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 {EventFight, ExtendedEvent} from "@type/event.ts";
    import FightEditPart from "../../../components/FightEditPart.svelte";
    import {fightRepo, type UpdateFight} from "@repo/fight.ts";
    import ErrorModal from "../../../components/ErrorModal.svelte";
    import {createEventDispatcher} from "svelte";
    import dayjs from "dayjs";
    import utc from "dayjs/plugin/utc";

    dayjs.extend(utc);

    export let fight: EventFight;
    export let data: ExtendedEvent;
    export let open = false;

    let redTeam = fight.redTeam.id.toString();
    let blueTeam = fight.blueTeam.id.toString();
    let start = dayjs(fight.start).utc(true).toISOString().slice(0, -1);
    let kampfleiter = fight.kampfleiter?.id.toString();
    let gamemode = fight.spielmodus;
    let map = fight.map;
    let group = fight.group;
    let groupSearch = fight.group ?? "";

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

    let dispatch = createEventDispatcher();

    function save() {
        const update: UpdateFight = {
            blueTeam: parseInt(blueTeam),
            group: group === "" ? null : group,
            kampfleiter: parseInt(kampfleiter),
            map: map,
            redTeam: parseInt(redTeam),
            spielmodus: gamemode,
            start: dayjs(start)
        };

        $fightRepo.updateFight(fight.id, update)
            .then(value => {
                open = false;
                fight = value;
                dispatch("update", value);
            })
            .catch((e) => {
                error = e.message;
                errorOpen = true;
            });
    }
</script>

<Modal title="Edit {fight.blueTeam.name} vs. {fight.redTeam.name}" bind:open outsideclose size="xs">
    <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>
    <div class="flex">
        <Button on:click={save}>Save</Button>
        <Button color="light" class="ml-auto" on:click={() => open = false}>Cancel</Button>
    </div>
</Modal>

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