/*
 *   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 com.google.common.collect.ImmutableList;
import de.steamwar.core.CommandRemover;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.linkage.Linked;
import net.md_5.bungee.api.ChatMessageType;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

@Linked
public class GamemodeCommand extends BukkitCommand {

    private static final List<String> GAMEMODE_NAMES = ImmutableList.of("adventure", "creative", "survival",
            "spectator");

    public GamemodeCommand() {
        super("gamemode");
        List<String> aliases = new ArrayList<>();
        aliases.add("gm");
        this.setAliases(aliases);

        try {
            CommandRemover.removeAll("gamemode");
        } catch (Exception e) {
            FightSystem.getPlugin().getLogger().log(Level.SEVERE, "Failed to replace commands", e);
        }
        Commands.injectCommand(this);
    }

    @Override
    public boolean execute(CommandSender sender, String currentAlias, String[] args) {
        if (!(sender instanceof Player)) {
            return false;
        }else if (args.length == 0) {
            FightSystem.getMessage().sendPrefixless("GAMEMODE_HELP", sender);
            return false;
        }

        Player p = (Player) sender;

        if (!(Config.test() || Config.isReferee(p))) {
            FightSystem.getMessage().sendPrefixless("GAMEMODE_NOT_ALLOWED", p, ChatMessageType.ACTION_BAR);
            return false;
        }

        GameMode mode = createMode(args[0]);

        if(mode == null){
            FightSystem.getMessage().sendPrefixless("GAMEMODE_UNKNOWN", p, ChatMessageType.ACTION_BAR, args[0]);
            return false;
        }

        p.setGameMode(mode);
        return true;
    }

    @SuppressWarnings("deprecation")
    private GameMode createMode(String modeArg){
        try {
            return GameMode.getByValue(Integer.parseInt(modeArg));
        } catch (NumberFormatException ignored) {
            if ((modeArg.equalsIgnoreCase("creative")) || (modeArg.equalsIgnoreCase("c")))
                return GameMode.CREATIVE;
            else if ((modeArg.equalsIgnoreCase("adventure")) || (modeArg.equalsIgnoreCase("a")))
                return GameMode.ADVENTURE;
            else if ((modeArg.equalsIgnoreCase("spectator")) || (modeArg.equalsIgnoreCase("sp")))
                return GameMode.SPECTATOR;
            else if ((modeArg.equalsIgnoreCase("survival")) || (modeArg.equalsIgnoreCase("s")))
                return GameMode.SURVIVAL;
        }
        return null;
    }

    @Override
    public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
        if (args.length == 1)
            return StringUtil.copyPartialMatches(args[0], GAMEMODE_NAMES, new ArrayList<>(GAMEMODE_NAMES.size()));
        return ImmutableList.of();
    }

}
