package com.tiedup.remake.v2.blocks;

import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.v2.V2BlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;

/**
 * Pet Bowl Block - Used by Master to feed pets.
 * Features:
 * - Right-click with food to fill
 * - Crouch + right-click to eat (only for pets)
 * - Custom OBJ model rendering
 */
public class PetBowlBlock extends Block implements EntityBlock {

    public static final DirectionProperty FACING =
        HorizontalDirectionalBlock.FACING;

    // Collision shape (rough approximation of bowl)
    private static final VoxelShape SHAPE = Block.box(
        3.0,
        0.0,
        3.0,
        13.0,
        4.0,
        13.0
    );

    public PetBowlBlock(Properties properties) {
        super(properties);
        this.registerDefaultState(
            this.stateDefinition.any().setValue(FACING, Direction.NORTH)
        );
    }

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

    @Override
    public BlockState getStateForPlacement(BlockPlaceContext context) {
        return this.defaultBlockState().setValue(
            FACING,
            context.getHorizontalDirection().getOpposite()
        );
    }

    @Override
    public VoxelShape getShape(
        BlockState state,
        BlockGetter level,
        BlockPos pos,
        CollisionContext context
    ) {
        return SHAPE;
    }

    @Override
    public RenderShape getRenderShape(BlockState state) {
        return RenderShape.ENTITYBLOCK_ANIMATED; // Use block entity renderer
    }

    @Nullable
    @Override
    public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
        return new PetBowlBlockEntity(
            V2BlockEntities.PET_BOWL.get(),
            pos,
            state
        );
    }

    @Override
    public InteractionResult use(
        BlockState state,
        Level level,
        BlockPos pos,
        Player player,
        InteractionHand hand,
        BlockHitResult hit
    ) {
        if (level.isClientSide) {
            return InteractionResult.SUCCESS;
        }

        BlockEntity be = level.getBlockEntity(pos);
        if (!(be instanceof PetBowlBlockEntity bowl)) {
            return InteractionResult.PASS;
        }

        ItemStack heldItem = player.getItemInHand(hand);

        // Fill bowl with food items
        if (isFood(heldItem)) {
            int foodValue = getFoodValue(heldItem);
            bowl.fillBowl(foodValue);

            if (!player.getAbilities().instabuild) {
                heldItem.shrink(1);
            }

            TiedUpMod.LOGGER.debug(
                "[PetBowlBlock] {} filled bowl at {} with {}",
                player.getName().getString(),
                pos,
                heldItem.getItem()
            );

            return InteractionResult.CONSUME;
        }

        // Eat from bowl (empty hand)
        if (heldItem.isEmpty() && bowl.hasFood()) {
            // TODO: Check if player is a pet and allowed to eat
            int consumed = bowl.eatFromBowl();
            if (consumed > 0) {
                player.getFoodData().eat(consumed, 0.6f);

                TiedUpMod.LOGGER.debug(
                    "[PetBowlBlock] {} ate {} from bowl at {}",
                    player.getName().getString(),
                    consumed,
                    pos
                );
            }
            return InteractionResult.CONSUME;
        }

        return InteractionResult.PASS;
    }

    private boolean isFood(ItemStack stack) {
        if (stack.isEmpty()) return false;
        return (
            stack.getItem().isEdible() ||
            stack.is(Items.WHEAT) ||
            stack.is(Items.CARROT) ||
            stack.is(Items.APPLE)
        );
    }

    private int getFoodValue(ItemStack stack) {
        if (stack.getItem().isEdible()) {
            var food = stack.getItem().getFoodProperties();
            return food != null ? food.getNutrition() : 2;
        }
        return 2; // Default for non-standard foods
    }
}
