package com.acethewildfire.acesbs.block.custom;

import com.acethewildfire.acesbs.AcesBS;
import com.acethewildfire.acesbs.util.ModTags;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.registry.Registries;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import java.util.List;
import java.util.Random;

public class EntropyBlock extends Block {
    public EntropyBlock(Settings settings) {
        super(settings);
    }

    Random r = new Random();

//    @Override
//    protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
//        world.playSound(player, pos, SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, SoundCategory.BLOCKS, 1f, 1f);
//        return ActionResult.SUCCESS;
//    }

    @Override
    public void onSteppedOn(World world, BlockPos pos, BlockState state, Entity entity) {
        if (entity instanceof ItemEntity itemEntity){
            if(isValidItem(itemEntity.getStack())){
                int chance = r.nextInt(1,21); // 1-20
                int itemIndex = r.nextInt(1, Registries.ITEM.size()+1);

                itemEntity.setStack(new ItemStack(itemEntity.getStack().getItem(), itemEntity.getStack().getCount() - 1));
                if (chance == 20) {
                    world.playSound(itemEntity, pos, SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, SoundCategory.BLOCKS, 1f, 1f);
                    ItemEntity convItem = new ItemEntity(world, itemEntity.getX(), itemEntity.getY(), itemEntity.getZ(), new ItemStack(Registries.ITEM.get(Identifier.of(AcesBS.MOD_ID, "lemon")), 1));
                    world.spawnEntity(convItem);
                } else {
                    world.playSound(itemEntity, pos, SoundEvents.ITEM_FIRECHARGE_USE, SoundCategory.BLOCKS, 1f, 1f);
                }
            }
        }

        double randomVelocityX = r.nextDouble(-1.5, 1.5);
        double randomVelocityY = r.nextDouble(0.1, 1.5);
        double randomVelocityZ = r.nextDouble(-1.5, 1.5);
        // 1d is roughly 4.5 blocks worth of upward velocity
        entity.addVelocity(randomVelocityX, randomVelocityY, randomVelocityZ);

        if (entity instanceof PlayerEntity){
            world.playSound(entity, pos, SoundEvents.ENTITY_ENDERMAN_HURT, SoundCategory.BLOCKS, 1f, 0.4f);
        }


        super.onSteppedOn(world, pos, state, entity);
    }

    private boolean isValidItem(ItemStack stack) {
        return stack.isIn(ModTags.Items.ENTROPY_CONVERTABLE);
    }

    @Override
    public void onLandedUpon(World world, BlockState state, BlockPos pos, Entity entity, float fallDistance) {
        // Never take fall damage
        if (entity.bypassesLandingEffects()) {
            super.onLandedUpon(world, state, pos, entity, 0);
        } else {
            entity.handleFallDamage(fallDistance, 0.0F, world.getDamageSources().fall());
        }
    }

    @Override
    public void appendTooltip(ItemStack stack, Item.TooltipContext context, List<Text> tooltip, TooltipType options) {
        tooltip.add(Text.translatable("tooltip.acesbs.entropy_block.tooltip"));
        tooltip.add(Text.translatable("tooltip.acesbs.entropy_block.tooltip2"));
        super.appendTooltip(stack, context, tooltip, options);
    }
}
