package me.km.blocks.cookingpot;

import me.km.items.ItemColoredSoup;
import me.km.items.ModItems;
import net.minecraft.block.Block;
import net.minecraft.block.CauldronBlock;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.material.MaterialColor;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraft.util.ActionResultType;

public class BlockCookingPot extends CauldronBlock implements ITileEntityProvider {
    public BlockCookingPot(String name) {
        super(Block.Properties.create(Material.IRON, MaterialColor.BROWN).hardnessAndResistance(2.0f));
        this.setRegistryName(name);
    }

    @Override
    public TileEntity createNewTileEntity(IBlockReader reader) {
        return new TileEntityCookingPot();
    }

    @Override
    public boolean hasTileEntity() {
        return true;
    }

    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return new TileEntityCookingPot();
    }

    @Override
    public ActionResultType onBlockActivated(BlockState state, World w, BlockPos pos, PlayerEntity p, Hand hand, BlockRayTraceResult hit) {
        TileEntity te = w.getTileEntity(pos);
        if(te instanceof TileEntityCookingPot) {
            TileEntityCookingPot cauldron = (TileEntityCookingPot) te;
            ItemStack stack = p.getHeldItem(hand);
            if(stack.isEmpty()) {
                return ActionResultType.PASS;
            } else if(stack.getItem() == Items.BOWL) {
                int waterLevel = state.get(LEVEL);
                if(waterLevel > 0 && !w.isRemote) {
                    ItemStack stew = new ItemStack(ModItems.coloredSoup);
                    ItemColoredSoup.addIngredient(stew, cauldron.getIngredients());
                    ItemColoredSoup.setColor(stew, cauldron.getColor().getRGB());
                    ItemColoredSoup.setFoodStats(stew, cauldron.getIngredients());
                    stack.shrink(1);
                    if(stack.isEmpty()) {
                        p.setHeldItem(hand, stew);
                    } else if(!p.inventory.addItemStackToInventory(stew)) {
                        p.dropItem(stew, false);
                    } else if(p instanceof ServerPlayerEntity) {
                        ((ServerPlayerEntity) p).sendContainerToPlayer(p.container);
                    }
                    this.setWaterLevel(w, pos, state, waterLevel - 1);
                }
                return ActionResultType.SUCCESS;
            }
            int ingredients = cauldron.getNumberOfIngredients();
            if(ingredients < 3 && cauldron.addItemStack(stack, !w.isRemote)) {
                if(!w.isRemote && !p.isCreative()) {
                    stack.shrink(1);
                }
                return ActionResultType.SUCCESS;
            }
            if(ingredients > 0) {
                return ActionResultType.PASS;
            }
        }
        return super.onBlockActivated(state, w, pos, p, hand, hit);
    }

    @Override
    public void setWaterLevel(World w, BlockPos pos, BlockState state, int level) {
        if(level <= 0 && !w.isRemote) {
            TileEntity te = w.getTileEntity(pos);
            if(te instanceof TileEntityCookingPot) {
                ((TileEntityCookingPot) te).reset();
            }
        }
        super.setWaterLevel(w, pos, state, level);
    }

    @Override
    public void fillWithRain(World w, BlockPos pos) {
        if(!w.isRemote) {
            TileEntity te = w.getTileEntity(pos);
            if(te instanceof TileEntityCookingPot) {
                ((TileEntityCookingPot) te).reset();
            }
        }
        super.fillWithRain(w, pos);
    }
}
