package com.r3944realms.leashedplayer.content.misc;

import com.r3944realms.leashedplayer.content.entities.LeashRopeArrow;
import com.r3944realms.leashedplayer.modInterface.PlayerLeashable;
import net.minecraft.core.BlockPos;
import net.minecraft.core.dispenser.BlockSource;
import net.minecraft.core.dispenser.OptionalDispenseItemBehavior;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.Leashable;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.BeehiveBlock;
import net.minecraft.world.level.block.DispenserBlock;
import net.minecraft.world.level.block.entity.BeehiveBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.NotNull;

public class LeadBreakItemBehavior extends OptionalDispenseItemBehavior {
    @Override
    protected @NotNull ItemStack execute(@NotNull BlockSource blockSource, @NotNull ItemStack item) {
        ServerLevel serverLevel = blockSource.level();
        if(!serverLevel.isClientSide()) {
            BlockPos blockPos = blockSource.pos().relative(blockSource.state().getValue(DispenserBlock.FACING));
            this.setSuccess(tryShearBeehive(serverLevel, blockPos) || tryShearLivingEntity(serverLevel, blockPos));
            if (this.isSuccess()) {
                item.hurtAndBreak(1, serverLevel, null, p_348118_ -> {
                });
            }
        }
        return item;
    }
    private static boolean tryShearBeehive(ServerLevel level, BlockPos pos) {
        BlockState blockstate = level.getBlockState(pos);
        if (blockstate.is(BlockTags.BEEHIVES, p_202454_ -> p_202454_.hasProperty(BeehiveBlock.HONEY_LEVEL) && p_202454_.getBlock() instanceof BeehiveBlock)) {
            int i = blockstate.getValue(BeehiveBlock.HONEY_LEVEL);
            if (i >= 5) {
                level.playSound(null, pos, SoundEvents.BEEHIVE_SHEAR, SoundSource.BLOCKS, 1.0F, 1.0F);
                BeehiveBlock.dropHoneycomb(level, pos);
                ((BeehiveBlock)blockstate.getBlock())
                        .releaseBeesAndResetHoneyLevel(level, blockstate, pos, null, BeehiveBlockEntity.BeeReleaseStatus.BEE_RELEASED);
                level.gameEvent(null, GameEvent.SHEAR, pos);
                return true;
            }
        }

        return false;
    }

    private static boolean tryShearLivingEntity(ServerLevel level, BlockPos pos) {
        for (LivingEntity livingentity : level.getEntitiesOfClass(LivingEntity.class, new AABB(pos), EntitySelector.NO_SPECTATORS)) {
            if (livingentity instanceof PlayerLeashable playerLeashable) {
                if (playerLeashable.isLeashed()){
                    Leashable.dropLeash((Entity & Leashable)playerLeashable, true, !(playerLeashable.getLeashHolder() instanceof LeashRopeArrow));
                    if (playerLeashable.getLeashHolder() instanceof LeashRopeArrow leashRopeArrow) {
                        leashRopeArrow.setOwner(null);
                    }
                    return true;
                } else return false;
            } else if (livingentity instanceof Leashable leashable) {
                if (leashable.isLeashed()) {
                    Leashable.dropLeash((Entity & Leashable) leashable,true, !(leashable.getLeashHolder() instanceof LeashRopeArrow));
                    if (leashable.getLeashHolder() instanceof LeashRopeArrow leashRopeArrow) {
                        leashRopeArrow.setOwner(null);
                    }
                    return true;
                } else return false;
            }
        }
        return false;
    }
}
