package de.hessj.environmentex;

import java.io.IOException;
import java.util.Iterator;
import org.bukkit.Bukkit;
import org.bukkit.EntityEffect;
import org.bukkit.Material;
import org.bukkit.block.ShulkerBox;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import de.hessj.helper.Helper;
import net.kyori.adventure.text.Component;

public class BetterTotems implements Listener {
   ItemStack totem = new ItemStack(Material.TOTEM_OF_UNDYING, 1);
   Inventory temp_inv = Bukkit.createInventory(null, 9, Component.translatable("tempBackpack"));

   @EventHandler(priority = EventPriority.LOWEST)
   public void onPlayerDamage(EntityDamageEvent event) throws IOException {
      if (event.getEntity() instanceof Player) {
         Player player = (Player) event.getEntity();
         double pHealth = player.getHealth();
         double pDamage = event.getFinalDamage();
         if (event.getCause().equals(DamageCause.VOID)) {
            return;
         }
         if (pHealth - pDamage <= 0) {
            PlayerInventory pInv = player.getInventory();
            boolean hasTotem = pInv.containsAtLeast(totem, 1);
            Iterator<ItemStack> var17;
            if (hasTotem) {
               pInv.removeItem(totem);
               resurrect(event, player);
            } else {
               var17 = pInv.iterator();

               while (var17.hasNext()) {
                  ItemStack item = (ItemStack) var17.next();

                  if (item != null && item.hasItemMeta()
                        && item.getItemMeta().getPersistentDataContainer().has(App.main.nsk, PersistentDataType.STRING)
                        && (item.getItemMeta().getPersistentDataContainer().get(App.main.nsk, PersistentDataType.STRING)
                              .equals("R3S_kleiner_Rucksack"))) {

                     App.main.log("bin drin + k");
                     temp_inv = Bukkit.createInventory(null, 9, Component.translatable("tempBackpack"));
                     ItemStack[] is;

                     is = Helper.itemStackArrayFromBase64(
                           App.main.getConfig().getString(player.getUniqueId() + ".backpack.contents.leatherbackpack"));
                     temp_inv.setContents(is);
                     if (temp_inv.containsAtLeast(totem, 1)) {
                        temp_inv.removeItem(totem);
                        resurrect(event, player);
                        App.main.getConfig().set(player.getUniqueId() + ".backpack.contents.leatherbackpack",
                              Helper.itemStackArrayToBase64(temp_inv.getContents()));
                        App.main.saveConfig();
                        App.main.reloadConfig();
                        break;
                     }
                  }

                  if (item != null && item.hasItemMeta()
                        && item.getItemMeta().getPersistentDataContainer().has(App.main.nsk2, PersistentDataType.STRING)
                        && (item.getItemMeta().getPersistentDataContainer()
                              .get(App.main.nsk2, PersistentDataType.STRING)
                              .equals("R3S_Rucksack"))) {

                     App.main.log("bin drin + r");
                     temp_inv = Bukkit.createInventory(null, 18, Component.translatable("tempBackpack"));
                     ItemStack[] is;

                     is = Helper.itemStackArrayFromBase64(
                           App.main.getConfig().getString(player.getUniqueId() + ".backpack.contents.ironbackpack"));
                     temp_inv.setContents(is);
                     if (temp_inv.containsAtLeast(totem, 1)) {
                        temp_inv.removeItem(totem);
                        resurrect(event, player);
                        App.main.getConfig().set(player.getUniqueId() + ".backpack.contents.ironbackpack",
                              Helper.itemStackArrayToBase64(temp_inv.getContents()));
                        App.main.saveConfig();
                        App.main.reloadConfig();
                        break;
                     }
                  }

                  if (item != null && item.hasItemMeta()
                        && item.getItemMeta().getPersistentDataContainer().has(App.main.nsk3, PersistentDataType.STRING)
                        && (item.getItemMeta().getPersistentDataContainer()
                              .get(App.main.nsk3, PersistentDataType.STRING)
                              .equals("R3S_großer_Rucksack"))) {

                     App.main.log("bin drin + g");
                     temp_inv = Bukkit.createInventory(null, 27, Component.translatable("tempBackpack"));
                     ItemStack[] is;

                     is = Helper.itemStackArrayFromBase64(
                           App.main.getConfig().getString(player.getUniqueId() + ".backpack.contents.goldbackpack"));
                     temp_inv.setContents(is);
                     if (temp_inv.containsAtLeast(totem, 1)) {
                        temp_inv.removeItem(totem);
                        resurrect(event, player);
                        App.main.getConfig().set(player.getUniqueId() + ".backpack.contents.goldbackpack",
                              Helper.itemStackArrayToBase64(temp_inv.getContents()));
                        App.main.saveConfig();
                        App.main.reloadConfig();
                        break;
                     }
                  }

                  if (item != null && item.hasItemMeta()
                        && item.getItemMeta().getPersistentDataContainer().has(App.main.nsk4, PersistentDataType.STRING)
                        && (item.getItemMeta().getPersistentDataContainer()
                              .get(App.main.nsk4, PersistentDataType.STRING)
                              .equals("R3S_riesiger_Rucksack"))) {

                     App.main.log("bin drin + riesig");
                     temp_inv = Bukkit.createInventory(null, 36, Component.translatable("tempBackpack"));
                     ItemStack[] is;

                     is = Helper.itemStackArrayFromBase64(
                           App.main.getConfig().getString(player.getUniqueId() + ".backpack.contents.diamondbackpack"));
                     temp_inv.setContents(is);
                     if (temp_inv.containsAtLeast(totem, 1)) {
                        temp_inv.removeItem(totem);
                        resurrect(event, player);
                        App.main.getConfig().set(player.getUniqueId() + ".backpack.contents.diamondbackpack",
                              Helper.itemStackArrayToBase64(temp_inv.getContents()));
                        App.main.saveConfig();
                        App.main.reloadConfig();
                        break;
                     }
                  }
                  if (item != null && (item.getType().toString().endsWith("SHULKER_BOX"))) {

                     BlockStateMeta blockMeta = (BlockStateMeta) item.getItemMeta();
                     if (blockMeta.getBlockState() instanceof ShulkerBox) {
                        ShulkerBox box = (ShulkerBox) blockMeta.getBlockState();
                        Inventory inv = box.getSnapshotInventory();

                        if (inv.containsAtLeast(totem, 1)) {
                           inv.removeItem(new ItemStack[] { totem });
                           box.update(true, false);
                           blockMeta.setBlockState(box);
                           item.setItemMeta(blockMeta);
                           break;
                        }
                     }

                  }
               }
            }

         }

      }
   }

   private void resurrect(EntityDamageEvent event, Player player) {
      event.setDamage(0.0D);
      player.setHealth(5);
      player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 5 * 20, 2));
      player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 45 * 20, 2));
      player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 40 * 20, 1));
      if (event.getCause().equals(DamageCause.DROWNING)) {
         player.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 5 * 20, 1));
      }
      player.playEffect(EntityEffect.PROTECTED_FROM_DEATH);
   }
}
