package de.hessj.environmentex;

import java.util.Map;
import org.bukkit.Material;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.block.data.Levelled;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Axolotl;
import org.bukkit.entity.Fish;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.player.PlayerBucketEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.view.AnvilView;
import org.bukkit.scheduler.BukkitRunnable;
import de.hessj.helper.Helper;
import de.hessj.helper.Helper.Type;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;

public class InfinityWaterBucket implements Listener {

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onAnvilUse(PrepareAnvilEvent e) {
        if (e.getView().getBottomInventory().getHolder() instanceof Player) {
            AnvilView av = e.getView();
            ItemStack slot1 = av.getItem(0);
            ItemStack slot2 = av.getItem(1);
            if (slot1 == null || slot2 == null)
                return;
            if (slot1.getType() == Material.WATER_BUCKET && slot2.getType() == Material.ENCHANTED_BOOK) {
                EnchantmentStorageMeta enchMeta = (EnchantmentStorageMeta) slot2.getItemMeta();

                Map<Enchantment, Integer> enchs = enchMeta.getStoredEnchants();
                if (enchs.containsKey(Enchantment.INFINITY) && enchs.get(Enchantment.INFINITY) > 0) {
                    ItemStack result = slot1.clone();
                    ItemMeta meta = result.getItemMeta();
                    meta.displayName(Component.text(av.getRenameText()));
                    Style st = Style.style().decoration(TextDecoration.ITALIC, false).build();
                    if (av.getRenameText().length() == 0) {
                        meta.displayName(Component.translatable(slot1.getType().getItemTranslationKey()).style(st));
                    } else {
                        meta.displayName(Component.text(av.getRenameText()).style(st));
                    }

                    meta.addEnchant(Enchantment.INFINITY, 1, true);

                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            result.setItemMeta(meta);
                            av.setRepairCost(2);
                            av.setItem(2, result);
                        }
                    }.runTaskLater(App.main, 1);

                }
            }
        }
    }

    @EventHandler
    public void onFishPickUp(PlayerBucketEntityEvent e) {
        var helper = new Helper();
        Player player = e.getPlayer();
        ItemStack item = e.getOriginalBucket();
        if (item == null)
            return;
        if (item.getEnchantments().size() <= 0)
            return;
        if (e.getEntity() instanceof Fish || e.getEntity() instanceof Axolotl) {
            e.setCancelled(true);
            player.sendMessage(helper.R3SMessage(Type.ERROR, "Du kannst mit diesem Eimer nichts fangen!"));
        }
    }

    @EventHandler
    public void onBucketUse(PlayerInteractEvent e) {
        if (e.getItem() == null)
            return;
        ItemStack itm = e.getItem().clone();
        if (itm.getType() == Material.WATER_BUCKET && itm.getEnchantments().size() > 0
                && e.getAction() == Action.RIGHT_CLICK_BLOCK && e.useItemInHand() != Result.DENY) {
            e.setCancelled(true);
            Player player = e.getPlayer();
            if (player.getWorld().getEnvironment() == Environment.NETHER
                    || (App.helper.isInteractable(e.getClickedBlock()) && !e.getPlayer().isSneaking()))
                return;

            Block block = e.getClickedBlock().getRelative(e.getBlockFace());

            if (!e.getClickedBlock().isEmpty() && e.getClickedBlock().getBlockData() instanceof Waterlogged) {

                Waterlogged waterlogged = (Waterlogged) e.getClickedBlock().getBlockData();

                if (!waterlogged.isWaterlogged()) {
                    waterlogged.setWaterlogged(true);
                    e.getClickedBlock().setBlockData(waterlogged);
                } else {
                    waterlogged.setWaterlogged(false);
                    e.getClickedBlock().setBlockData(waterlogged);
                }
            } else {
                if (block.getType().equals(Material.WATER)) { // aber nicht bei flowing water!!

                    Levelled lv = (Levelled) block.getBlockData();

                    if (lv.getLevel() == 0) {
                        block.setType(Material.AIR, true);
                    } else {
                        block.setType(Material.WATER, true);
                    }

                } else {
                    block.setType(Material.WATER, true);
                }

            }

        }
    }
}