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.Material;
import org.bukkit.Sound;
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.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.entity.EntityCombustEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.weather.WeatherChangeEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.Iterator;

public class GamePotect implements Listener {

    @EventHandler // 取消生物燃烧
    public void onEntityCombust(EntityCombustEvent event) {
        if (event.getEntity() instanceof LivingEntity) {
            event.setCancelled(true);
        }
    }
    @EventHandler // 禁止火焰燃烧方块
    public void onBlockFire(BlockBurnEvent e) {
        e.setCancelled(true);
    }
    @EventHandler// 禁止火焰燃烧物品
    public void onBlockIgnite(BlockIgniteEvent e) {
        e.setCancelled(true);
    }
    @EventHandler(priority= EventPriority.HIGH)
    public void onWeatherChange(WeatherChangeEvent e) {
        e.setCancelled(true);
    }

    @EventHandler /*保护农作物免踩踏*/
    public void onJumpFarm(PlayerInteractEvent e){
        if (e.isCancelled()) {return;}
        if(e.getAction() == Action.PHYSICAL){
            Block block = e.getClickedBlock();
            if(block.getType() == Material.FARMLAND){
                e.setCancelled(true);
            }
        }
    }

    @EventHandler /*保护农作物免踩踏*/
    public void onMobsFarm(EntityInteractEvent e){
        if (e.isCancelled()) {return;}
        if(e.getEntityType() != EntityType.PLAYER){
            Block block = e.getBlock();
            if(block.getType() == Material.FARMLAND){
                e.setCancelled(true);
            }
        }
    }

    @EventHandler
    public void onClick(InventoryClickEvent e){
        int rawSlot = e.getRawSlot();
        Player p = (Player) e.getWhoClicked();
        Inventory inv = e.getInventory();
        if(inv.getType() == InventoryType.CRAFTING){
            if(rawSlot == 36 || rawSlot == 37 || rawSlot == 38){
                e.setCancelled(true);
            }
        }
        if(inv.getType() == InventoryType.MERCHANT){
            if(rawSlot == 30 || rawSlot == 31 || rawSlot == 32){
                e.setCancelled(true);
            }
        }
    }

    @EventHandler // 禁止丢弃任何物品
    public void onDrop(PlayerDropItemEvent e){
        Game game = MinerWar.getGame();
        game.effect_RefreshExcavateAmount(e.getPlayer()); // 刷新玩家背包中的粗金
        e.setCancelled(true);
        /*if(e.getItemDrop().getItemStack().getType().equals(Material.RAW_GOLD)) {
                e.setCancelled(true);
          }*/
    }

    @EventHandler
    public void onCloseInv(InventoryCloseEvent e){
        Player player = (Player) e.getPlayer();
        Inventory inventory = e.getInventory();
        if(inventory.getType().equals(InventoryType.MERCHANT)){
            MinerWar.getGame().effect_RefreshExcavateAmount(player);
        }
    }


    @EventHandler
    public void onBreakProtectedArea(BlockBreakEvent e){
        Player p = e.getPlayer();
        Game game = MinerWar.getGame();
        Block block = e.getBlock();
        if(block.getType() == Material.GOLD_ORE) {
            e.setCancelled(false);
        }else if(block.getType() == Material.COBBLESTONE) {
            e.setCancelled(false);
        } else {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onPlaceProtectedArea(BlockPlaceEvent e){
        Player p = e.getPlayer();
        Game game = MinerWar.getGame();
        Block block = e.getBlock();
        e.setCancelled(true);
    }

    @EventHandler
    public void onEntityExplode(EntityExplodeEvent event) {
        Game game = MinerWar.getGame();
        if(game.isStarted()){
            Iterator<Block> iterator = event.blockList().iterator();
            while (iterator.hasNext()) {
                Block block = iterator.next();
                Point min = new Point(21,68,-16);
                Point max = new Point(39,90,-5);
                Region region = new Region(game.getVillagerPoint().getWorld(),min,max);
                if (region.isInRegion(block.getLocation())) {
                    iterator.remove();
                }
            }
        }
    }
}
