/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 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/>.
 */

package de.steamwar.velocitycore.commands;

import com.velocitypowered.api.proxy.Player;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.EventMode;
import de.steamwar.linkage.Linked;
import de.steamwar.messages.Chatter;
import de.steamwar.messages.Message;
import de.steamwar.messages.PlayerChatter;
import de.steamwar.persistent.Subserver;
import de.steamwar.sql.GameModeConfig;
import de.steamwar.sql.IgnoreSystem;
import de.steamwar.velocitycore.ServerStarter;
import net.kyori.adventure.text.event.ClickEvent;

import java.util.LinkedList;

import static de.steamwar.persistent.Storage.challenges;

@Linked
@EventMode(false)
public class ChallengeCommand extends SWCommand {

    public ChallengeCommand() {
        super("challenge");
    }

    @Register(description = "CHALLENGE_USAGE")
    public void challenge(@Validator("arenaPlayer") PlayerChatter sender, @Validator("target") Player target, @Mapper("nonHistoricArenaMode") @OptionalValue("") @AllowNull GameModeConfig<String, String> arenaMode, @Mapper("arenaMap") @OptionalValue("") @AllowNull String map) {
        FightCommand.createArena(sender, "/challenge " + target.getUsername() + " ", false, arenaMode, map, false, (chatter, mode, m) -> {
            Player p = sender.getPlayer();
            if (challenges.containsKey(target) && challenges.get(target).contains(p)) {
                challenges.remove(target);
                challenges.remove(p);

                new ServerStarter().arena(mode, map).blueLeader(sender.getPlayer()).redLeader(target).callback(
                        arena -> Chatter.broadcast().system("CHALLENGE_BROADCAST", new Message("CHALLENGE_BROADCAST_HOVER"), ClickEvent.runCommand("/arena " + arena.getServer().getName()), mode.GameName, p, target)
                ).start();
            } else {
                if (!challenges.containsKey(p)) {
                    challenges.put(p, new LinkedList<>());
                }

                challenges.get(p).add(target);

                sender.system("CHALLENGE_CHALLENGED", target, mode.GameName);
                Chatter.of(target).system("CHALLENGE_CHALLENGED_TARGET", p, mode.GameName, mode.Server.Maps.size() != 1 ? new Message("CHALLENGE_CHALLENGED_MAP", m) : "");

                Chatter.of(target).system("CHALLENGE_ACCEPT", new Message("CHALLENGE_ACCEPT_HOVER"), ClickEvent.runCommand("/challenge " + p.getUsername() + " " + mode.getChatName() + " " + m));
            }
        });
    }

    @Validator(value = "target", local = true)
    public TypeValidator<Player> targetValidator() {
        return (sender, value, messageSender) -> {
            if (value == null) {
                messageSender.send("CHALLENGE_OFFLINE");
                return false;
            }
            if (sender == value) {
                messageSender.send("CHALLENGE_SELF");
                return false;
            }
            if (IgnoreSystem.isIgnored(value.getUniqueId(), sender.user().getUUID())) {
                messageSender.send("CHALLENGE_IGNORED");
                return false;
            }

            if (Subserver.isArena(Subserver.getSubserver(value))) {
                messageSender.send("CHALLENGE_INARENA");
                return false;
            }
            return true;
        };
    }

    public static void remove(Player player) {
        challenges.remove(player);
    }
}
