/*
 *   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.fightsystem.commands;

import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightPlayer;
import de.steamwar.fightsystem.fight.FightTeam;
import de.steamwar.fightsystem.fight.Kit;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.sql.PersonalKit;
import de.steamwar.sql.SteamwarUser;
import lombok.experimental.UtilityClass;
import net.md_5.bungee.api.ChatMessageType;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.entity.Player;

@UtilityClass
public class Commands {

    public static void injectCommand(Command cmd) {
        ((CraftServer) Bukkit.getServer()).getCommandMap().register("FightSystem", cmd);
    }

    private static void errNoTeam(Player p) {
        FightSystem.getMessage().sendPrefixless("NO_TEAM", p, ChatMessageType.ACTION_BAR);
    }

    static boolean checkSetup(Player p) {
        if (!FightState.setup()) {
            FightSystem.getMessage().sendPrefixless("FIGHT_ALREADY_STARTED", p, ChatMessageType.ACTION_BAR);
            return true;
        }
        return false;
    }

    private static FightPlayer checkGetPlayer(Player p) {
        FightPlayer fightPlayer = Fight.getFightPlayer(p);
        if (fightPlayer == null) {
            errNoTeam(p);
        }
        return fightPlayer;
    }

    public static FightPlayer checkGetLeader(Player p) {
        FightPlayer fightPlayer = checkGetPlayer(p);
        if (fightPlayer != null && !fightPlayer.isLeader()) {
            FightSystem.getMessage().sendPrefixless("NOT_LEADER", p, ChatMessageType.ACTION_BAR);
            fightPlayer = null;
        }
        return fightPlayer;
    }

    public static FightTeam checkGetTeam(Player p) {
        FightTeam fightTeam = Fight.getPlayerTeam(p);
        if (fightTeam == null) {
            errNoTeam(p);
        }
        return fightTeam;
    }

    private static Player checkGetPlayer(Player p, String t) {
        Player target = Bukkit.getPlayer(t);
        if (target == null) {
            FightSystem.getMessage().sendPrefixless("PLAYER_UNAVAILABLE", p, ChatMessageType.ACTION_BAR);
        }
        return target;
    }

    static void toggleReady(Player p) {
        FightTeam fightTeam = checkGetTeam(p);
        if (fightTeam == null || checkGetLeader(p) == null) return;

        fightTeam.setReady(!fightTeam.isReady());
    }

    static void toggleSkip(Player p) {
        FightTeam fightTeam = checkGetTeam(p);
        if (fightTeam == null || checkGetLeader(p) == null) return;

        fightTeam.skip();
    }

    static void leaveTeam(Player p) {
        if (checkSetup(p)) return;

        FightTeam fightTeam = checkGetTeam(p);
        if (fightTeam == null) return;

        fightTeam.removePlayer(p);
    }

    static void kick(Player p, String kicked) {
        if (checkSetup(p)) return;

        FightTeam fightTeam = checkGetTeam(p);
        if (fightTeam == null) return;

        FightPlayer fightPlayer = checkGetLeader(p);
        if (fightPlayer == null) return;

        Player target = checkGetPlayer(p, kicked);
        if (target == null) return;

        if (!fightTeam.isPlayerInTeam(target)) {
            FightSystem.getMessage().sendPrefixless("NOT_IN_TEAM", p, ChatMessageType.ACTION_BAR, target.getName());
            return;
        }

        fightTeam.removePlayer(target);
    }

    public static void kit(Player p, String kitName) {
        if (checkSetup(p)) return;

        FightPlayer fightPlayer = checkGetPlayer(p);
        if (fightPlayer == null) return;

        Kit k = null;
        if (Config.GameModeConfig.Kits.PersonalKits) {
            PersonalKit kit = PersonalKit.get(SteamwarUser.get(p.getUniqueId()).getId(), Config.GameModeConfig.Schematic.Type.toDB(), kitName);
            if (kit != null) {
                kit.setInUse();
                k = new Kit(kit);
            }
        } else {
            k = Kit.getKitByName(kitName);
        }

        if (k == null || !k.canUseKit(fightPlayer.isLeader())) {
            FightSystem.getMessage().sendPrefixless("KIT_UNAVAILABLE", p, ChatMessageType.ACTION_BAR);
            return;
        }

        FightSystem.getMessage().sendPrefixless("KIT_CHOSEN", p, ChatMessageType.ACTION_BAR, kitName);
        fightPlayer.setKit(k);
    }
}
