package org.allaymc.server.block.component.banner;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.data.BlockFace;
import org.allaymc.api.block.dto.Block;
import org.allaymc.api.block.type.BlockState;
import org.allaymc.api.block.type.BlockType;
import org.allaymc.api.blockentity.interfaces.BlockEntityBanner;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.type.ItemTypes;
import org.allaymc.server.block.component.BlockBaseComponentImpl;

import java.util.Set;

/**
 * @author daoge_cmd
 */
public class BlockStandingBannerBaseComponentImpl extends BlockBaseComponentImpl {

    public BlockStandingBannerBaseComponentImpl(BlockType<? extends BlockBehavior> blockType) {
        super(blockType);
    }

    @Override
    public void onNeighborUpdate(Block block, Block neighbor, BlockFace face, BlockState oldNeighborState) {
        super.onNeighborUpdate(block, neighbor, face, oldNeighborState);
        if (face == BlockFace.DOWN && !neighbor.getBlockStateData().isSolid()) {
            block.breakBlock();
        }
    }

    @Override
    public Set<ItemStack> getDrops(Block block, ItemStack usedItem, Entity entity) {
        return createBannerDrop(block);
    }

    @Override
    public ItemStack getSilkTouchDrop(Block block) {
        return createBannerDropSingle(block);
    }

    protected Set<ItemStack> createBannerDrop(Block block) {
        return Set.of(createBannerDropSingle(block));
    }

    protected ItemStack createBannerDropSingle(Block block) {
        var blockEntity = block.getBlockEntity();
        if (blockEntity instanceof BlockEntityBanner bannerEntity) {
            var item = ItemTypes.BANNER.createItemStack(1, bannerEntity.getBaseColor().ordinalInverted());
            item.setBannerType(bannerEntity.getBannerType());
            item.setPatterns(bannerEntity.getPatterns());
            return item;
        }
        return ItemTypes.BANNER.createItemStack(1);
    }
}
