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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.MinVersion;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Linked
public class AttributeRemoveCommand extends SWCommand {

    public AttributeRemoveCommand() {
        super("removeattribute", "attributesremove");
    }

    @Register({"all"})
    @Register({"*"})
    public void genericCommand(@Validator Player player) {
        ItemStack itemStack = player.getInventory().getItemInMainHand();
        ItemMeta itemMeta = itemStack.getItemMeta();
        itemMeta.setLore(new ArrayList<>());
        itemStack.setItemMeta(itemMeta);
        BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_ALL", player);
    }

    @Register(description = "ATTRIBUTE_REMOVE_COMMAND_HELP")
    public void genericCommand(@Validator Player player, @Mapper("attribute") String attribute) {
        ItemStack itemStack = player.getInventory().getItemInMainHand();
        ItemMeta itemMeta = itemStack.getItemMeta();
        if (itemMeta == null) {
            BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_NOT_FOUND", player);
            return;
        }
        List<String> lore = itemMeta.getLore();
        if (lore == null) {
            BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_NOT_FOUND", player);
            return;
        }
        if (lore.isEmpty()) {
            BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_NOT_FOUND", player);
            return;
        }
        if (!lore.get(0).equals("§eAttributes§8:")) {
            BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_NOT_FOUND", player);
            return;
        }
        lore.removeIf(s -> s.startsWith("§8-§7 " + attribute + "§8:"));
        if (lore.size() == 1) {
            itemStack.setItemMeta(null);
        } else {
            itemMeta.setLore(lore);
            itemStack.setItemMeta(itemMeta);
        }
        BauSystem.MESSAGE.send("ATTRIBUTE_REMOVE_SINGLE", player, attribute);
    }

    @Mapper(value = "attribute", local = true)
    public TypeMapper<String> attribute() {
        return new TypeMapper<String>() {
            @Override
            public Collection<String> tabCompletes(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                Player player = (Player) commandSender;
                ItemStack itemStack = player.getInventory().getItemInMainHand();
                ItemMeta itemMeta = itemStack.getItemMeta();
                if (itemMeta == null) return null;
                List<String> lore = itemMeta.getLore();
                if (lore == null) return null;
                if (lore.isEmpty()) return null;
                if (!lore.get(0).equals("§eAttributes§8:")) return null;
                return lore.stream()
                        .skip(1)
                        .map(s1 -> s1.substring(6))
                        .map(s1 -> s1.substring(0, s1.indexOf("§8:")))
                        .collect(Collectors.toList());
            }

            @Override
            public String map(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                return s;
            }
        };
    }
}
