package de.hessj.pokeballs;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.components.CustomModelDataComponent;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import net.kyori.adventure.text.Component;
import net.md_5.bungee.api.ChatColor;

public class App extends JavaPlugin {
    public static App main;
    public NamespacedKey nskPokeball = new NamespacedKey(this, "R3SPokeball");
    public NamespacedKey nskUsedPokeball = new NamespacedKey(this, "R3SUsedPokeball");
    public NamespacedKey nskEntityPokeball = new NamespacedKey(this, "R3SEntity");
    public NamespacedKey nskEntVariantPokeball = new NamespacedKey(this, "R3SEntityVariant");
    public NamespacedKey nskEntStylePokeball = new NamespacedKey(this, "R3SEntityStyle");
    public NamespacedKey nskAgePokeball = new NamespacedKey(this, "R3SEntityAge");

    public ItemStack newPokeball;
    public ItemStack usedPokeball;
    public HashMap<Player, Boolean> isThrownHM = new HashMap<Player, Boolean>();

    @Override
    public void onEnable() {
        getLogger().info("Plugin enabled!");
        main = this;
        listenerRegistration();
        for(Player p : Bukkit.getServer().getOnlinePlayers()){
            isThrownHM.put(p, false);
        }
    }

    @Override
    public void onDisable() {
        getLogger().info("Plugin disabled!");
    }

    private void listenerRegistration() {
        PluginManager pM = Bukkit.getPluginManager();
        newPokeball = new ItemStack(Material.SNOWBALL);
        ItemMeta newPokeballMeta = newPokeball.getItemMeta();
        newPokeballMeta.displayName(Component.translatable(ChatColor.WHITE + "Pokéball"));
        newPokeballMeta.setCustomModelData(1000011);
        
        CustomModelDataComponent cmdc4 = newPokeballMeta.getCustomModelDataComponent();
        List<String> list4 = Arrays.asList("pokeball");
        cmdc4.setStrings(list4);
        newPokeballMeta.setCustomModelDataComponent(cmdc4);

        newPokeballMeta.getPersistentDataContainer().set(nskPokeball, PersistentDataType.STRING, "R3S_Pokeball");
        newPokeball.setItemMeta(newPokeballMeta);
        ShapedRecipe pokeballRecipe = new ShapedRecipe(nskPokeball, newPokeball);
        pokeballRecipe.shape("RRR", "SES", "WWW");
        pokeballRecipe.setIngredient('R', Material.RED_CONCRETE);
        pokeballRecipe.setIngredient('S', Material.BLACK_CONCRETE);
        pokeballRecipe.setIngredient('E', Material.IRON_NUGGET);
        pokeballRecipe.setIngredient('W', Material.WHITE_CONCRETE);
        if (getServer().getRecipe(nskPokeball) == null) {
            getServer().addRecipe(pokeballRecipe);
        }
        pM.registerEvents(new PokeballsListeners(), this);
        getCommand("get").setExecutor(this);
    }


    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            if(!((Player) sender).isOp()){
                return true;
            }

           if(args != null && args.length > 0){

            if (label.equalsIgnoreCase("get") && args[0].toString() != "" && args[0].toString().equals("pokeball") && args.length == 2 && StringUtils.isNumeric(args[1])) {
                ItemStack is = newPokeball.clone();
                is.setAmount(Integer.parseInt(args[1]));
                ((Player) sender).getInventory().addItem(is);
            }
            else if (label.equalsIgnoreCase("get") && args[0].toString() != "" && args[0].toString().equals("pokeball")) {
                ((Player) sender).getInventory().addItem(newPokeball);
            }
        }
        }
        return true;
    }


}