package com.yutian.minerwar.listener;

import com.yutian.minerwar.MinerWar;
import com.yutian.minerwar.game.Game;
import com.yutian.minerwar.game.Point;
import com.yutian.minerwar.game.Region;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.entity.*;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.weather.WeatherChangeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import tools.CDTimeAPI;

import java.util.Iterator;

public class GameListener implements Listener {

    /*
     * 当玩家拾取粗金时增加游戏进度
     * */
    @EventHandler
    public void onPick(PlayerPickupItemEvent e){
        Player p = e.getPlayer();
        if(e.getItem().getItemStack().getType().equals(Material.RAW_GOLD)){
            Game game = MinerWar.getGame();
            p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP,1,1);
            p.getWorld().spawnParticle(Particle.FIREWORKS_SPARK,p.getLocation().add(0,1,0),15,0.5,0.5,0.5,0.1);

            new BukkitRunnable() {
                @Override
                public void run() {
                    game.effect_RefreshExcavateAmount(p); // 刷新玩家背包中的粗金
                }
            }.runTaskLater(MinerWar.inst(),10L);
        }else {
            e.setCancelled(true);
            p.playSound(p.getLocation(), Sound.ENTITY_ITEM_PICKUP,1,1);
            e.getItem().remove();
        }
    }
    /*
    * 当玩家破坏金矿后将在300秒后重新生成一个新的金矿石
    * */
    @EventHandler
    public void onBreakGoldOre(BlockBreakEvent e){
        // 判断破坏的方块是否是金矿
        Block block = e.getBlock();
        World world = block.getWorld();
        if(block.getType().equals(Material.GOLD_ORE)) {
            Player player = e.getPlayer();
            if (CDTimeAPI.isCD(player.getUniqueId(),"supermining")) {
                world.dropItem(block.getLocation().add(0,1,0),new ItemStack(Material.RAW_GOLD));
            }
            // 获取这个方块的坐标
            Location location = block.getLocation();
            // 创建计时器300秒  每5秒出现一次红石粒子
            Game game = MinerWar.getGame();
            BukkitTask task = new BukkitRunnable() {
                private int i = 0;
                // 粒子出现的坐标点
                private final Location particlLoc = location.clone().add(0.5,0.5,0.5);
                @Override
                public void run() {
                    // i = 1 = 5秒
                    if (i >= 4) {
                        // 刷新矿石同时终止计时器
                        world.getBlockAt(location).setType(Material.GOLD_ORE);
                        world.spawnParticle(Particle.VILLAGER_HAPPY,particlLoc,5,0.5,0.5,0.5,0.1);
                        cancel();
                        return;
                    }
                    // 村民生气粒子效果
                    world.spawnParticle(Particle.VILLAGER_ANGRY,particlLoc,5,0.5,0.5,0.5,0.1);
                    i++;
                }
            }.runTaskTimer(MinerWar.inst(), 0L, 20L * 5);
            game.addTasks(task);
        }else if(block.getType().equals(Material.COBBLESTONE)){
            // 获取这个方块的坐标
            Location location = block.getLocation();
            Game game = MinerWar.getGame();
            BukkitTask task = new BukkitRunnable() {
                @Override
                public void run() {
                    world.getBlockAt(location).setType(Material.COBBLESTONE);
                }
            }.runTaskLater(MinerWar.inst(), 20L * 10);
            game.addTasks(task);
        }
    }

    @EventHandler /*主播受到伤害关闭飞行*/
    public void onDamagerBee(EntityDamageByEntityEvent e) {
        Game game = MinerWar.getGame();
        if (game.isStarted()) {
            if (e.getEntity() instanceof Player) {
                if (e.getDamager() instanceof Bee) {
                    Bee bee = (Bee) e.getDamager();
                    if (bee.getTarget() != null && bee.getTarget() instanceof Player) {
                        ((Bee) e.getDamager()).setHealth(0);
                        shieldBlockEvent(e.getEntity(),e,0D);
                    }
                }
                if (e.getDamager() instanceof Arrow) {
                    shieldBlockEvent(e.getEntity(),e,0D);
                }
                if (e.getDamager() instanceof EvokerFangs) {
                    shieldBlockEvent(e.getEntity(),e,1D);
                }
            }
        }
    }

    @EventHandler
    public void onPotionEffect(EntityPotionEffectEvent event) {
        // 检查效果是否是中毒效果
        if (event.getNewEffect() != null && event.getNewEffect().getType() == PotionEffectType.POISON) {
            // 检查目标是否是玩家
            if (event.getEntity() instanceof Player) {
                // 取消中毒效果
                event.setCancelled(true);
            }
        }
    }

    public void shieldBlockEvent(Entity entity,EntityDamageByEntityEvent e,double damage){
        Player player = (Player) entity;
        if(CDTimeAPI.isCD(entity.getUniqueId(),"invincible")){
            player.playSound(player.getLocation(),Sound.ENTITY_ZOMBIE_ATTACK_WOODEN_DOOR,1,1);
            e.setCancelled(true);
            return;
        }
        ItemStack offHandItem = player.getInventory().getItemInOffHand();
        // 检查副手是否有盾牌
        if (offHandItem != null && offHandItem.getType() == Material.SHIELD && player.isBlocking()) {
            // 取消伤害事件
            e.setCancelled(true);
            // 减少盾牌的耐久
            short durability = offHandItem.getDurability();
            offHandItem.setDurability((short) (durability + 18)); // 增加1点耐久损耗
            player.playSound(player.getLocation(),Sound.ENTITY_ZOMBIE_ATTACK_WOODEN_DOOR,1,1);
            // 如果盾牌的耐久已经耗尽，移除盾牌
            if (offHandItem.getDurability() >= offHandItem.getType().getMaxDurability()) {
                player.getInventory().setItemInOffHand(null); // 清空副手
                player.playSound(player.getLocation(),Sound.ENTITY_ZOMBIE_ATTACK_WOODEN_DOOR,1,1);
            }
        }else {
            e.setDamage(damage);
        }
    }
}
