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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.world.ScriptListener;
import de.steamwar.command.SWCommand;
import de.steamwar.command.SWCommandUtils;
import de.steamwar.command.TypeMapper;
import org.bukkit.entity.Player;

import java.util.*;

public class CommandScriptVars extends SWCommand {

    public CommandScriptVars() {
        super("scripvars");
    }

    @Register(help = true)
    public void genericHelp(Player p, String... args) {
        p.sendMessage("§8/§escriptvars §8- §7Zähle alle globalen Variablen auf");
        p.sendMessage("§8/§escriptvars §8[§7Variable§8] §8- §7Gebe den Wert der Variable zurück");
        p.sendMessage("§8/§escriptvars §8[§7Variable§8] §8[§7Value§8] §8- §7Setzte eine Variable auf einen Wert");
        p.sendMessage("§8/§escriptvars §8[§7Variable§8] §8<§7remove§8|§7delete§8|§7clear§8> §8- §7Lösche eine Variable");
    }

    @Register
    public void genericCommand(Player p) {
        Map<String, Integer> globalVariables = ScriptListener.GLOBAL_VARIABLES.get(p);
        if (globalVariables == null) {
            p.sendMessage(BauSystem.PREFIX + "§cKeine globalen Variablen definiert");
            return;
        }
        int i = 0;
        p.sendMessage(BauSystem.PREFIX + globalVariables.size() + " Variable(n)");
        for (Map.Entry<String, Integer> var : globalVariables.entrySet()) {
            if (i++ >= 40) break;
            p.sendMessage("- " + var.getKey() + "=" + var.getValue());
        }
    }

    @Register
    public void removeCommand(Player p, String varName) {
        Map<String, Integer> globalVariables = ScriptListener.GLOBAL_VARIABLES.get(p);
        if (globalVariables == null) {
            p.sendMessage(BauSystem.PREFIX + "§cKeine globalen Variablen definiert");
            return;
        }
        if (!globalVariables.containsKey(varName)) {
            p.sendMessage(BauSystem.PREFIX + "§cUnbekannte Variable");
            return;
        }
        p.sendMessage(BauSystem.PREFIX + varName + "=" + globalVariables.get(varName));
    }

    @Register
    public void booleanValueCommand(Player p, String varName, int value) {
        ScriptListener.GLOBAL_VARIABLES.computeIfAbsent(p, player -> new HashMap<>()).put(varName, value);
        p.sendMessage(BauSystem.PREFIX + varName + " auf " + value + " gesetzt");
    }

    @Register
    public void removeCommand(Player p, String varName, @Mapper(value = "Delete") String remove) {
        if (!ScriptListener.GLOBAL_VARIABLES.containsKey(p)) {
            p.sendMessage(BauSystem.PREFIX + "§cKeine globalen Variablen definiert");
            return;
        }
        ScriptListener.GLOBAL_VARIABLES.get(p).remove(varName);
        p.sendMessage(BauSystem.PREFIX + "Variable " + varName + " gelöscht");
    }

    @ClassMapper(value = String.class, local = true)
    public TypeMapper<String> stringTypeMapper() {
        return SWCommandUtils.createMapper(s -> s, (commandSender, s) -> {
            if (commandSender instanceof Player) {
                Player player = (Player) commandSender;
                return new ArrayList<>(ScriptListener.GLOBAL_VARIABLES.getOrDefault(player, new HashMap<>()).keySet());
            }
            return null;
        });
    }

    @Mapper(value = "Delete", local = true)
    public TypeMapper<String> clearStringTypeMapper() {
        List<String> tabCompletes = Arrays.asList("delete", "clear", "remove");
        return SWCommandUtils.createMapper(s -> {
            if (s.equalsIgnoreCase("delete") || s.equalsIgnoreCase("clear") || s.equalsIgnoreCase("remove")) {
                return s;
            }
            return null;
        }, s -> tabCompletes);
    }

    @ClassMapper(value = int.class, local = true)
    public TypeMapper<Integer> integerTypeMapper() {
        List<String> tabCompletes = Arrays.asList("true", "false", "yes", "no");
        return SWCommandUtils.createMapper(s -> {
            if (s.equalsIgnoreCase("remove") || s.equalsIgnoreCase("clear") || s.equalsIgnoreCase("delete")) return null;
            return ScriptListener.parseValue(s);
        }, s -> tabCompletes);
    }
}
