package com.yaohun.wheatwar.listener;

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.type.Farmland;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import tools.CDTimeAPI;
import tools.RandomUtil;

import java.util.UUID;

public class GameEffectListener implements Listener {

    /*
    * 当玩家手持物品右键指定方块时触发效果
    * */
    @EventHandler
    public void onPotion_drop_Main(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        ItemStack item = p.getInventory().getItemInMainHand();
        if (item.getType() != Material.AIR && item.getType() == Material.SPLASH_POTION) {
            Block block = e.getClickedBlock();
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
                boolean sound_butt = false;
                Location location = block.getLocation();
                // 检测 3x2x3范围中的方块
                for (int x = -1; x <= 1; x++) {
                    for (int y = -1; y <= 1; y++) {
                        for (int z = -1; z <= 1; z++) {
                            Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
                            // 判断方块是否是泥土
                            if (newBlock.getType() == Material.FARMLAND) {
                                Farmland fdata = (Farmland) newBlock.getBlockData();
                                if (fdata.getMoisture() < 7) {
                                    sound_butt = true;
                                    fdata.setMoisture(fdata.getMaximumMoisture());
                                    newBlock.setBlockData(fdata);
                                    location.getWorld().spawnParticle(Particle.CRIMSON_SPORE, newBlock.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
                                }
                            }
                        }
                    }
                }
                if (sound_butt) {
                    p.playSound(p.getLocation(), Sound.ITEM_BUCKET_EMPTY, 1, 1);
                }
            }
        }
    }


    @EventHandler
    public void onPotion_drop_Off(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        ItemStack item = p.getInventory().getItemInOffHand();
        if (item.getType() != Material.AIR && item.getType() == Material.SPLASH_POTION) {
            Block block = e.getClickedBlock();
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
                boolean sound_butt = false;
                Location location = block.getLocation();
                // 检测 3x2x3范围中的方块
                for (int x = -1; x <= 1; x++) {
                    for (int y = -1; y <= 1; y++) {
                        for (int z = -1; z <= 1; z++) {
                            Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
                            // 判断方块是否是泥土
                            if (newBlock.getType() == Material.FARMLAND) {
                                Farmland fdata = (Farmland) newBlock.getBlockData();
                                if (fdata.getMoisture() < 7) {
                                    sound_butt = true;
                                    fdata.setMoisture(fdata.getMaximumMoisture());
                                    newBlock.setBlockData(fdata);
                                    location.getWorld().spawnParticle(Particle.CRIMSON_SPORE, newBlock.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
                                }
                            }
                        }
                    }
                }
                if (sound_butt) {
                    p.playSound(p.getLocation(), Sound.ITEM_BUCKET_EMPTY, 1, 1);
                }
            }
        }
    }

    @EventHandler
    public void onBone_meal_Main(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        Block block = e.getClickedBlock();
        if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
            ItemStack item = p.getInventory().getItemInMainHand();
            // 检测手持物品是否是骨粉
            if (item.getType() == Material.BONE_MEAL) {
                e.setCancelled(true);
                boolean sound_butt = false;
                Location location = block.getLocation();
                // 检测 3x2x3范围中的方块
                for (int x = -1; x <= 1; x++) {
                    for (int y = -1; y <= 1; y++) {
                        for (int z = -1; z <= 1; z++) {
                            Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
                            if(newBlock.getType() == Material.WHEAT){
                                if(newBlock.getBlockData() instanceof Ageable) {
                                    Ageable ageable = (Ageable) newBlock.getBlockData();
                                    if (ageable.getAge() < 7) {
                                        sound_butt = true;
                                        ageable.setAge(ageable.getMaximumAge());
                                        newBlock.setBlockData(ageable);
                                        block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, newBlock.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
                                    }
                                }
                            }
                        }
                    }
                }
                if(sound_butt){
                    p.playSound(p.getLocation(),Sound.BLOCK_CROP_BREAK,1,1);
                }
            }
        }
    }
    @EventHandler
    public void onBone_meal_Off(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        Block block = e.getClickedBlock();
        if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
            ItemStack item = p.getInventory().getItemInOffHand();
            // 检测手持物品是否是骨粉
            if (item.getType() == Material.BONE_MEAL) {
                e.setCancelled(true);
                boolean sound_butt = false;
                Location location = block.getLocation();
                // 检测 3x2x3范围中的方块
                for (int x = -1; x <= 1; x++) {
                    for (int y = -1; y <= 1; y++) {
                        for (int z = -1; z <= 1; z++) {
                            Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
                            if(newBlock.getType() == Material.WHEAT){
                                if(newBlock.getBlockData() instanceof Ageable) {
                                    Ageable ageable = (Ageable) newBlock.getBlockData();
                                    if (ageable.getAge() < 7) {
                                        sound_butt = true;
                                        ageable.setAge(ageable.getMaximumAge());
                                        newBlock.setBlockData(ageable);
                                        block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, newBlock.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
                                    }
                                }
                            }
                        }
                    }
                }
                if(sound_butt){
                    p.playSound(p.getLocation(),Sound.BLOCK_CROP_BREAK,1,1);
                }
            }
        }
    }

    @EventHandler
    public void onWheat_Seeds(PlayerInteractEvent e){
        Player p = e.getPlayer();
        Block block = e.getClickedBlock();
        if(e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
            ItemStack item = p.getInventory().getItemInMainHand();
            // 检测手持物品是否是小麦种子
            if (item.getType() == Material.WHEAT_SEEDS) {
                boolean sound_butt = false;
                // 获取玩家点击方块的坐标
                Location location = block.getLocation();
                if (p.isSneaking()) {
                    // 检索到泥土方块后判断方块是否是湿润状态
                    if (block.getBlockData() instanceof Farmland) {
                        Farmland farmland = (Farmland) block.getBlockData();
                        // 如果没有打湿则跳过
                        if (farmland.getMoisture() != 7) {
                            return;
                        }
                        // 获取泥土方块上方是否有小麦
                        Block blockWheat = block.getRelative(0, 1, 0);
                        if (blockWheat.getType() == Material.AIR) {
                            blockWheat.setType(Material.WHEAT);
                            if (blockWheat.getBlockData() instanceof Ageable) {
                                // 将小麦设置为刚种下时的阶段
                                Ageable wheat = (Ageable) blockWheat.getBlockData();
                                wheat.setAge(3);
                                blockWheat.setBlockData(wheat);
                                p.playSound(p.getLocation(), Sound.BLOCK_AMETHYST_BLOCK_PLACE, 1, 1);
                                block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
                            }
                        }
                    }
                } else {
                    if(block.getType() == Material.FARMLAND){
                        if (block.getBlockData() instanceof Farmland) {
                            Farmland farmland = (Farmland) block.getBlockData();
                            if (farmland.getMoisture() != 7) {
                                e.setCancelled(true);
                            }
                        }
                    }
                    // 检测 3x2x3范围中的方块
                    for (int x = -1; x <= 1; x++) {
                        for (int y = -1; y <= 0; y++) {
                            for (int z = -1; z <= 1; z++) {
                                Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
                                // 检索到泥土方块后判断方块是否是湿润状态
                                if (newBlock.getBlockData() instanceof Farmland) {
                                    Farmland farmland = (Farmland) newBlock.getBlockData();
                                    // 如果没有打湿则跳过遍历
                                    if (farmland.getMoisture() != 7) {
                                        continue;
                                    }
                                    // 获取泥土方块上方是否有小麦
                                    Block blockWheat = newBlock.getRelative(0, 1, 0);
                                    if (blockWheat.getType() == Material.AIR) {
                                        blockWheat.setType(Material.WHEAT);
                                        if (blockWheat.getBlockData() instanceof Ageable) {
                                            // 将小麦设置为刚种下时的阶段
                                            Ageable wheat = (Ageable) blockWheat.getBlockData();
                                            wheat.setAge(3);
                                            blockWheat.setBlockData(wheat);
                                            block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
                                            sound_butt = true;
                                        }
                                    }else if (block.getType() == Material.WHEAT) {
                                        if (block.getBlockData() instanceof Ageable) {
                                            // 将小麦设置为刚种下时的阶段
                                            Ageable ageable = (Ageable) block.getBlockData();
                                            if (ageable.getAge() < 3) {
                                                ageable.setAge(3);
                                                block.setBlockData(ageable);
                                                block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
                                                sound_butt = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if(sound_butt){
                        p.playSound(p.getLocation(),Sound.BLOCK_CROP_BREAK,1,1);
                    }
                }
            }
        }
    }


    @EventHandler
    public void onWheat_Seeds_off(PlayerInteractEvent e){
        Player p = e.getPlayer();
        Block block = e.getClickedBlock();
        if(e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
            ItemStack item = p.getInventory().getItemInOffHand();
            // 检测手持物品是否是小麦种子
            if (item.getType() == Material.WHEAT_SEEDS) {
                boolean sound_butt = false;
                // 获取玩家点击方块的坐标
                Location location = block.getLocation();
                if (p.isSneaking()) {
                    // 检索到泥土方块后判断方块是否是湿润状态
                    if (block.getBlockData() instanceof Farmland) {
                        Farmland farmland = (Farmland) block.getBlockData();
                        // 如果没有打湿则跳过
                        if (farmland.getMoisture() != 7) {
                            return;
                        }
                        // 获取泥土方块上方是否有小麦
                        Block blockWheat = block.getRelative(0, 1, 0);
                        if (blockWheat.getType() == Material.AIR) {
                            blockWheat.setType(Material.WHEAT);
                            if (blockWheat.getBlockData() instanceof Ageable) {
                                // 将小麦设置为刚种下时的阶段
                                Ageable wheat = (Ageable) blockWheat.getBlockData();
                                wheat.setAge(3);
                                blockWheat.setBlockData(wheat);
                                p.playSound(p.getLocation(), Sound.BLOCK_AMETHYST_BLOCK_PLACE, 1, 1);
                                block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
                            }
                        }
                    }
                } else {
                    if(block.getType() == Material.FARMLAND){
                        if (block.getBlockData() instanceof Farmland) {
                            Farmland farmland = (Farmland) block.getBlockData();
                            if (farmland.getMoisture() != 7) {
                                e.setCancelled(true);
                            }
                        }
                    }
                    // 检测 3x2x3范围中的方块
                    for (int x = -1; x <= 1; x++) {
                        for (int y = -1; y <= 0; y++) {
                            for (int z = -1; z <= 1; z++) {
                                Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
                                // 检索到泥土方块后判断方块是否是湿润状态
                                if (newBlock.getBlockData() instanceof Farmland) {
                                    Farmland farmland = (Farmland) newBlock.getBlockData();
                                    // 如果没有打湿则跳过遍历
                                    if (farmland.getMoisture() != 7) {
                                        continue;
                                    }
                                    // 获取泥土方块上方是否有小麦
                                    Block blockWheat = newBlock.getRelative(0, 1, 0);
                                    if (blockWheat.getType() == Material.AIR) {
                                        blockWheat.setType(Material.WHEAT);
                                        if (blockWheat.getBlockData() instanceof Ageable) {
                                            // 将小麦设置为刚种下时的阶段
                                            Ageable wheat = (Ageable) blockWheat.getBlockData();
                                            wheat.setAge(3);
                                            blockWheat.setBlockData(wheat);
                                            block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
                                            sound_butt = true;
                                        }
                                    }else if (block.getType() == Material.WHEAT) {
                                        if (block.getBlockData() instanceof Ageable) {
                                            // 将小麦设置为刚种下时的阶段
                                            Ageable ageable = (Ageable) block.getBlockData();
                                            if (ageable.getAge() < 3) {
                                                ageable.setAge(3);
                                                block.setBlockData(ageable);
                                                block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
                                                sound_butt = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if(sound_butt){
                        p.playSound(p.getLocation(),Sound.BLOCK_CROP_BREAK,1,1);
                    }
                }
            }
        }
    }
}
