
package de.hessj.environmentex;

import java.util.Collection;
import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import net.kyori.adventure.text.Component;

public class HealthBarListener implements Listener {
   public App plugin = App.main;
   public HashMap<UUID, Component> hitMobList = new HashMap<UUID, Component>();
   public HashMap<UUID, Integer> hitMobSched = new HashMap<UUID, Integer>();

   public Boolean isNearby(Collection<Entity> nearbyEntities) {
      for (Entity e : nearbyEntities) {
         
         if (e instanceof Player) {
            return true;
         }
      }
      return false;
   }

   public void setBarString(Double remainingHearts, Double fullHearts, Double finaldmg, LivingEntity lentity) {
      lentity.setCustomNameVisible(true);
      if (remainingHearts > 0 && remainingHearts < 1) {
         lentity.customName(Component.text("Stehend K.O"));

      } else if (remainingHearts > 0) {
         lentity.customName(
               Component.text((int) Math.round(remainingHearts) + "/" + (int) Math.round(fullHearts) + " §c❤"));
      } else {
         lentity.customName(null);
         lentity.setCustomNameVisible(false);
      }

   }

   @EventHandler
   public void onHit(EntityDamageEvent e) {
      if (e.getEntity() instanceof LivingEntity) {
         LivingEntity en = (LivingEntity) e.getEntity();
         if (isNearby(e.getEntity().getLocation().getWorld().getNearbyEntities(e.getEntity().getLocation(), 8, 8, 8))) {
            if (en.customName() != null
                  && (!en.customName().toString().contains("/") && !en.customName().toString().contains(" §c❤"))) {
               hitMobList.put(en.getUniqueId(), en.customName());
            }
            if (en.getType() != EntityType.ENDER_DRAGON && en.getType() != EntityType.WITHER
                  && en.getType() != EntityType.PLAYER && en.getType() != EntityType.ARMOR_STAND) {
               Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {

                 setBarString(en.getHealth(), en.getAttribute(Attribute.MAX_HEALTH).getValue(), e.getDamage(), en);
               }, (long) 1 * 1L);
            }

            startTimer(en);
         }
      }

   }

   @EventHandler
   public void onDeath(EntityDeathEvent e) {
      e.getEntity().setCustomNameVisible(false);
      e.getEntity().customName(null);
      if (hitMobList.containsKey(e.getEntity().getUniqueId())) {
         hitMobList.remove(e.getEntity().getUniqueId());
         if (hitMobSched.containsKey(e.getEntity().getUniqueId())) {
            Bukkit.getScheduler().cancelTask(hitMobSched.get(e.getEntity().getUniqueId()));
         }
      }
   }

   private void startTimer(LivingEntity e) {

      if (hitMobSched.containsKey(e.getUniqueId())) {
         Bukkit.getScheduler().cancelTask(hitMobSched.get(e.getUniqueId()));
      }

      int sched = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {

         if (hitMobList.containsKey(e.getUniqueId())) {
            e.customName(hitMobList.get(e.getUniqueId()));
         } else {
            e.setCustomNameVisible(false);
            e.customName(null);
         }
      }, (long) 5 * 20L);

      hitMobSched.put(e.getUniqueId(), sched);

   }
}