package com.r3944realms.modernlifepatch.mixin.block.kitchen;

import com.dairymoose.modernlife.blocks.RefrigeratorBlock;
import com.dairymoose.modernlife.tileentities.SeedSpreaderBlockEntity;
import com.r3944realms.modernlifepatch.datagen.lang.ModLangKeyValue;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ChestMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(RefrigeratorBlock.class)
public class MixinRefrigerator {
    @Inject(method = {"getMenuProvider"}, at = @At("HEAD"), cancellable = true)
    public void getMenuProvider(BlockState blockState, Level level, BlockPos blockPos, CallbackInfoReturnable<MenuProvider> cir) {
        cir.setReturnValue(new MenuProvider() {
            @Override
            public AbstractContainerMenu createMenu(int paramInt, @NotNull Inventory paramInventory, @NotNull Player paramPlayer) {
                BlockEntity tileEntity = level.getBlockEntity(blockPos);
                if (tileEntity instanceof SeedSpreaderBlockEntity seedSpreaderBlockEntity) {
                    return new ChestMenu(MenuType.GENERIC_9x1, paramInt, paramInventory, seedSpreaderBlockEntity, 1);
                } else {
                    return null;
                }
            }

            public @NotNull Component getDisplayName() {
                return Component.translatable(ModLangKeyValue.REFRIGERATOR_MENU_LABEL.getKey());
            }
        });
    }
}
