package com.yaohun.wheatwar.listener;

import com.yaohun.wheatwar.WheatWar;
import com.yaohun.wheatwar.effect.SummonTheZombie;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Farmland;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.server.ServerListPingEvent;

import java.util.*;

public class GameListener implements Listener {
    @EventHandler
    public void onPingMotd(ServerListPingEvent e){
        if(WheatWar.getGame().isStarted()){
            e.setMotd("§a整蛊模式: §6小麦保卫战  §a当前版本: §6常规版 §b运行中");
        } else {
            e.setMotd("§a整蛊模式: §6小麦保卫战  §a当前版本: §6常规版 §b运行中");
        }
    }

    @EventHandler
    public void onZombieTarget(EntityTargetEvent event) {
        // 检查是否是僵尸
        if (event.getEntityType() == EntityType.ZOMBIE_VILLAGER) {
            // 检查目标是否是玩家
            if (event.getTarget() instanceof Player) {
                // 取消该事件以阻止僵尸将玩家设为仇恨目标
                event.setCancelled(true);
            }
        }
        // 检查是否是僵尸
        if (event.getEntityType() == EntityType.ZOMBIE) {
            // 检查目标是否是玩家
            if (event.getTarget() instanceof Player) {
                // 取消该事件以阻止僵尸将玩家设为仇恨目标
                event.setCancelled(true);
            }
        }
    }

    @EventHandler
    public void ondeath(EntityDeathEvent e){
        // 检查被杀的实体是否是村民
        if (e.getEntity() instanceof Villager villager) {
            // 检查村民是否是被僵尸杀死的
            if (villager.getLastDamageCause() instanceof EntityDamageByEntityEvent damageEvent) {
                if (damageEvent.getDamager() instanceof Zombie zombie) {
                    HashMap<UUID, Integer> zombieKillMap = SummonTheZombie.zombieKillMap;
                    if(zombieKillMap.get(zombie.getUniqueId()) != null){
                        int newAmount = zombieKillMap.get(zombie.getUniqueId()) - 1;
                        if(newAmount >= 1){
                            SummonTheZombie.zombieKillMap.put(zombie.getUniqueId(),newAmount);
                        } else {
                            zombie.damage(999.0D);
                        }
                    }
                }
            }
        }
    }


    public static List<UUID> whiteListTNT = new ArrayList<>();
    @EventHandler
    public void onDamage(EntityDamageByEntityEvent e){
        if(e.getDamager() instanceof TNTPrimed tntPrimed){
            Entity entity = e.getEntity();
            if(entity instanceof ZombieVillager){
                e.setDamage(1000.0D);
            } else if(entity instanceof Zombie){
                e.setDamage(1000.0D);
            } else {
                if(whiteListTNT.contains(tntPrimed.getUniqueId())){
                    e.setCancelled(true);
                }
            }
        }
    }
    @EventHandler
    public void onEntityExplode(EntityExplodeEvent event) {
        Iterator<Block> iterator = event.blockList().iterator();
        while (iterator.hasNext()) {
            Block block = iterator.next();
            iterator.remove();
        }
    }
}
