package com.acethewildfire.acesbs.block.custom;

import com.acethewildfire.acesbs.block.entity.custom.EntropicStabilizerEntity;
import com.mojang.serialization.MapCodec;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemActionResult;
import net.minecraft.util.ItemScatterer;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class EntropicStabilizer extends BlockWithEntity implements BlockEntityProvider {
    public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;
    public static final MapCodec<EntropicStabilizer> CODEC = EntropicStabilizer.createCodec(EntropicStabilizer::new);

    public EntropicStabilizer(Settings settings) {
        super(settings);
        setDefaultState(getStateManager().getDefaultState().with(FACING, Direction.NORTH));
    }

    @Override
    protected MapCodec<? extends BlockWithEntity> getCodec() {
        return CODEC;
    }

    @Override
    public @Nullable BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
        return new EntropicStabilizerEntity(pos, state);
    }

    @Override
    protected BlockRenderType getRenderType(BlockState state) {
        return BlockRenderType.MODEL;
    }

    @Override
    protected void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
        if(state.getBlock() != newState.getBlock()) {
            BlockEntity blockEntity = world.getBlockEntity(pos);
            if(blockEntity instanceof EntropicStabilizerEntity) {
                ItemScatterer.spawn(world, pos, ((EntropicStabilizerEntity) blockEntity));
                world.updateComparators(pos, this);
            }
            super.onStateReplaced(state, world, pos, newState, moved);
        }
    }

    @Override
    protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {

        if(world.getBlockEntity(pos) instanceof EntropicStabilizerEntity entropicStabilizerEntity) {
//            if(entropicStabilizerEntity.isEmpty() && !stack.isEmpty()) {
//                entropicStabilizerEntity.setStack(0, stack.copyWithCount(1));
//                world.playSound(player, pos, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, 1f, 2f);
//                stack.decrement(1);
//
//                entropicStabilizerEntity.markDirty();
//                world.updateListeners(pos, state, state, 0);
//            }
//            else if(!entropicStabilizerEntity.isEmpty() && stack.isEmpty() && !player.isSneaking()) {
//                ItemStack stackOnPedestal = entropicStabilizerEntity.getStack(0);
//                player.setStackInHand(Hand.MAIN_HAND, stackOnPedestal);
//                world.playSound(player, pos, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, 1f, 1f);
//                entropicStabilizerEntity.clear();
//
//                entropicStabilizerEntity.markDirty();
//                world.updateListeners(pos, state, state, 0);
//            }
            if(!player.isSneaking() && !world.isClient()) {
                player.openHandledScreen(entropicStabilizerEntity);
            }

        }

        return ItemActionResult.SUCCESS;
    }

    @Override
    protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
        builder.add(FACING);
    }

    @Override
    public BlockState getPlacementState(ItemPlacementContext ctx) {
        return getDefaultState().with(FACING, ctx.getHorizontalPlayerFacing().getOpposite());
    }
}
