package com.yutian.minerwar.listener;

import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class FoodAndMedicineListener implements Listener {

    /*
     * 吃指定的食物恢复指定的饱和度
     * */
    @EventHandler
    public void onPlayerConsume(PlayerItemConsumeEvent event) {
        // 获取玩家和物品
        Player player = event.getPlayer();
        Material itemType = event.getItem().getType();
        switch (itemType) {
            case COOKIE:
                setFoodAndSaturation(player, 2,Material.COOKIE); // 曲奇饼
                break;
            case BREAD:
                setFoodAndSaturation(player, 6,Material.BREAD); // 面包
                break;
            case COOKED_BEEF:
                setFoodAndSaturation(player, 10,Material.COOKED_BEEF); // 熟牛排
                break;
            case COOKED_CHICKEN:
                setFoodAndSaturation(player, 14,Material.COOKED_CHICKEN); // 熟鸡肉
                break;
            case RABBIT_STEW:
                setFoodAndSaturation(player, 20,Material.RABBIT_STEW); // 兔肉煲
                break;
            case POTATO:
                setMedicamentAndSaturation(player,event.getItem()); // 兔肉煲
                break;
            default:
                // 对于其他食物，不做处理
                break;
        }
        // 自定义饱食度逻辑
        event.setCancelled(true);
    }

    // 设置饱食度和饱和度的方法
    private void setFoodAndSaturation(Player player,int foodLevel,Material material) {
        // 增加饱食度（食物条），并设置饱和度
        int newFoodLevel = Math.min(player.getFoodLevel() + foodLevel, 20);  // 最大饱食度为20
        player.setFoodLevel(newFoodLevel);
        player.getWorld().spawnParticle(Particle.VILLAGER_HAPPY,player.getLocation().add(0,1,0),10,0.1,0.1,0.1,0.3);
        player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_BURP,1,1);
        ItemStack mainHand = player.getInventory().getItemInMainHand();
        if (mainHand != null && mainHand.getType().equals(material)) {
            if (mainHand.getAmount() == 1) {
                player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
            } else {
                mainHand.setAmount(mainHand.getAmount() - 1);
            }
        } else {
            ItemStack offHand = player.getInventory().getItemInOffHand();
            if (offHand != null && offHand.getType().equals(material)) {
                if (offHand.getAmount() == 1) {
                    player.getInventory().setItemInOffHand(new ItemStack(Material.AIR));
                } else {
                    offHand.setAmount(offHand.getAmount() - 1);
                }
            }
        }
    }

    private void setMedicamentAndSaturation(Player player,ItemStack stack) {
        int healLevel = 2;
        ItemMeta meta = stack.getItemMeta();
        assert meta != null;
        if(meta.getCustomModelData() == 802){
            healLevel = 6;
        }else if(meta.getCustomModelData() == 803){
            healLevel = 10;
        }
        int newHealLevel = (int) Math.min(player.getHealth() + healLevel, 20);
        player.setHealth(newHealLevel);
        player.getWorld().spawnParticle(Particle.VILLAGER_HAPPY,player.getLocation().add(0,1,0),10,0.1,0.1,0.1,0.3);
        player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_BURP,1,1);
        ItemStack mainHand = player.getInventory().getItemInMainHand();
        if (mainHand != null && mainHand.getType().equals(stack.getType())) {
            if (mainHand.getAmount() == 1) {
                player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
            } else {
                mainHand.setAmount(mainHand.getAmount() - 1);
            }
        } else {
            ItemStack offHand = player.getInventory().getItemInOffHand();
            if (offHand != null && offHand.getType().equals(stack.getType())) {
                if (offHand.getAmount() == 1) {
                    player.getInventory().setItemInOffHand(new ItemStack(Material.AIR));
                } else {
                    offHand.setAmount(offHand.getAmount() - 1);
                }
            }
        }
    }
}
