package de.hessj.environmentex;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerResourcePackStatusEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import net.kyori.adventure.text.Component;

public class CustomItems implements Listener {

        public static ItemStack flintBlock = new ItemStack(Material.DIRT);
        


        public de.hessj.helper.Helper helper= new de.hessj.helper.Helper();

        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
                if (e.getBlock().getType().equals(Material.BARRIER)) {
                        e.setCancelled(true);
                }
        }

        private String url = App.main.tpConfig.getString("url");
        private String sha1 = App.main.tpConfig.getString("sha1");

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
                Player player = e.getPlayer();
                Bukkit.getScheduler().scheduleSyncDelayedTask(App.main, () -> {

                        player.setResourcePack(url, sha1);
                }, (long) 1L);
        }

        @EventHandler
        public void onResourcepackStatusEvent(PlayerResourcePackStatusEvent event) {
                if (event.getStatus() == PlayerResourcePackStatusEvent.Status.DECLINED) {
                        event.getPlayer().kick(Component.translatable(
                                        "Um auf diesem Server spielen zu können, musst du die Server-Ressourcenpakete aktivieren!"));
                } else if (event.getStatus() == PlayerResourcePackStatusEvent.Status.FAILED_DOWNLOAD) {
                        event.getPlayer().setResourcePack(url, sha1);
                }
        }

        @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
                if (e.getPlayer() == null || e.getPlayer().getInventory().getItemInMainHand() == null
                                || !e.getPlayer().getInventory().getItemInMainHand().hasItemMeta()
                                || !e.getPlayer().getInventory().getItemInMainHand().getItemMeta()
                                                .hasCustomModelData()) {
                        return;
                }
                if (e.getPlayer().getInventory().getItemInMainHand().getItemMeta().getCustomModelData() == 1000001 ||
                                e.getPlayer().getInventory().getItemInMainHand().getItemMeta()
                                                .getCustomModelData() == 1000002
                                ||
                                e.getPlayer().getInventory().getItemInMainHand().getItemMeta()
                                                .getCustomModelData() == 1000003
                                ||
                                e.getPlayer().getInventory().getItemInMainHand().getItemMeta()
                                                .getCustomModelData() == 1000004
                                ||
                                e.getPlayer().getInventory().getItemInMainHand().getItemMeta()
                                                .getCustomModelData() == 1000005

                ) {
                        ItemStack is = e.getPlayer().getInventory().getItemInMainHand();
                        ItemMeta meta = is.getItemMeta();
                        Damageable d = (Damageable) meta;
                        d.setDamage(d.getDamage() + 2);
                        is.setItemMeta(meta);
                }
        }

        @EventHandler
        public void onFight(EntityDamageByEntityEvent e) {

                if (e.getDamager() instanceof Player == false
                                || ((Player) e.getDamager()).getInventory().getItemInMainHand() == null
                                || !((Player) e.getDamager()).getInventory().getItemInMainHand().hasItemMeta()
                                || !((Player) e.getDamager()).getInventory().getItemInMainHand().getItemMeta()
                                                .hasCustomModelData()) {
                        return;
                }

                if (((Player) e.getDamager()).getInventory().getItemInMainHand().getItemMeta()
                                .getCustomModelData() == 1000001
                                ||
                                ((Player) e.getDamager()).getInventory().getItemInMainHand().getItemMeta()
                                                .getCustomModelData() == 1000002
                                ||
                                ((Player) e.getDamager()).getInventory().getItemInMainHand().getItemMeta()
                                                .getCustomModelData() == 1000003
                                ||
                                ((Player) e.getDamager()).getInventory().getItemInMainHand().getItemMeta()
                                                .getCustomModelData() == 1000004
                                ||
                                ((Player) e.getDamager()).getInventory().getItemInMainHand().getItemMeta()
                                                .getCustomModelData() == 1000005

                ) {
                        ItemStack is = ((Player) e.getDamager()).getInventory().getItemInMainHand();
                        ItemMeta meta = is.getItemMeta();
                        Damageable d = (Damageable) meta;
                        d.setDamage(d.getDamage() + 1);
                        is.setItemMeta(meta);
                }
        }

      
        @EventHandler
        public void lightBlock(PlayerInteractEvent e) {
                if (e.getClickedBlock() == null || e.getAction() != Action.RIGHT_CLICK_BLOCK
                                || e.getPlayer().getInventory().getItemInMainHand() != null && !e.getPlayer()
                                                .getInventory().getItemInMainHand().equals(App.main.bulb)) {
                        return;
                }
                if (e.getPlayer().getInventory().getItemInMainHand().equals(App.main.bulb)
                                && e.getClickedBlock().getType().equals(Material.LIGHT)) {
                        e.setCancelled(true);
                }
        }
}
