package me.nvus.nvus_prison_setup.Listeners;

import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.LeatherArmorMeta;

public class PlayerSpawn implements Listener {

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player joinedPlayer = event.getPlayer();

        // Check if the player has the permission
        if (joinedPlayer.hasPermission("nvus.prisoner")) {
            // Create and equip prisoner armor
            equipPrisonerArmor(joinedPlayer);
        }
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        Player deadPlayer = event.getEntity();

        // Check if the player has the permission
        if (deadPlayer.hasPermission("nvus.prisoner")) {
            // Destroy default prisoner armor
            destroyDefaultPrisonerArmor(deadPlayer);
        }
    }

    @EventHandler
    public void onPlayerRespawn(PlayerRespawnEvent event) {
        Player respawnedPlayer = event.getPlayer();

        // Check if the player has the permission
        if (respawnedPlayer.hasPermission("nvus.prisoner")) {
            // Create and equip prisoner armor
            equipPrisonerArmor(respawnedPlayer);
        }
    }

    private void equipPrisonerArmor(Player player) {
        // Create Prisoner Armor
        ItemStack leatherHelmetPrison = new ItemStack(Material.LEATHER_HELMET);
        LeatherArmorMeta helmetMeta = (LeatherArmorMeta) leatherHelmetPrison.getItemMeta();
        helmetMeta.setColor(Color.ORANGE);
        leatherHelmetPrison.setItemMeta(helmetMeta);

        ItemStack leatherChestplatePrison = new ItemStack(Material.LEATHER_CHESTPLATE);
        LeatherArmorMeta chestplateMeta = (LeatherArmorMeta) leatherChestplatePrison.getItemMeta();
        chestplateMeta.setColor(Color.ORANGE);
        leatherChestplatePrison.setItemMeta(chestplateMeta);

        ItemStack leatherLeggingsPrison = new ItemStack(Material.LEATHER_LEGGINGS);
        LeatherArmorMeta leggingsMeta = (LeatherArmorMeta) leatherLeggingsPrison.getItemMeta();
        leggingsMeta.setColor(Color.ORANGE);
        leatherLeggingsPrison.setItemMeta(leggingsMeta);

        ItemStack leatherBootsPrison = new ItemStack(Material.LEATHER_BOOTS);
        LeatherArmorMeta bootsMeta = (LeatherArmorMeta) leatherBootsPrison.getItemMeta();
        bootsMeta.setColor(Color.ORANGE);
        leatherBootsPrison.setItemMeta(bootsMeta);

        // Equip Prisoner Armor
        player.getInventory().setHelmet(leatherHelmetPrison);
        player.getInventory().setChestplate(leatherChestplatePrison);
        player.getInventory().setLeggings(leatherLeggingsPrison);
        player.getInventory().setBoots(leatherBootsPrison);

        player.sendMessage(ChatColor.translateAlternateColorCodes('&',"&6&lYou're a prisoner! &6You've been given the default prisoner armor!"));
    }

    // Destroy armor upon death etc.
    private void destroyDefaultPrisonerArmor(Player player) {
        // Loop through the armor slots and remove any armor pieces
        for (ItemStack armor : player.getInventory().getArmorContents()) {
            if (armor != null && armor.getType() != Material.AIR) {
                player.getInventory().remove(armor);
            }
        }

        // Using this as a debug/check, will comment out later!
        player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6&lYour default prisoner armor has been destroyed!"));
    }
}
