package me.km.blocks.cookingpot;

import java.awt.Color;
import java.util.LinkedList;
import me.km.KajetansMod;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;

public class TileEntityCookingPot extends TileEntity {
    public static final TileEntityType<TileEntityCookingPot> COOKING_POT
            = (TileEntityType<TileEntityCookingPot>) TileEntityType.Builder
                    .create(TileEntityCookingPot::new).build(null)
                    .setRegistryName(KajetansMod.MODID, "cooking_pot");

    private final CookingPotColorMixer mixer;

    public TileEntityCookingPot() {
        super(COOKING_POT);
        mixer = new CookingPotColorMixer();
    }

    @Override
    public CompoundNBT write(CompoundNBT com) {
        super.write(com);

        ListNBT list = new ListNBT();
        LinkedList<ItemStack> items = mixer.getItems();
        items.stream().forEach(stack -> {
            CompoundNBT nbttagcompound = new CompoundNBT();
            stack.write(nbttagcompound);
            list.add(nbttagcompound);
        });

        com.put("items", list);
        return com;
    }

    @Override
    public void read(CompoundNBT com) {
        super.read(com);
        readItems(com);
    }

    private void readItems(CompoundNBT com) {
        if(com.contains("items")) {
            mixer.reset();
            ListNBT list = com.getList("items", 10);
            LinkedList<ItemStack> items = mixer.getItems();
            for(INBT base : list) {
                items.add(ItemStack.read((CompoundNBT) base));
            }

            mixer.cache();
        }
    }

    public boolean hasDefaultColor() {
        return mixer.getItems().isEmpty();
    }

    public int getNumberOfIngredients() {
        return mixer.getItems().size();
    }

    public LinkedList<ItemStack> getIngredients() {
        return mixer.getItems();
    }

    public Color getColor() {
        return mixer.getMixedColor();
    }

    public void reset() {
        mixer.reset();
    }

    public boolean addItemStack(ItemStack stack, boolean server) {
        Color c = mixer.getItemColor(stack);
        if(c == null) {
            return false;
        }
        if(server) {
            mixer.addColor(stack, c);
            notifyBlockUpdate();
            markDirty();
        }
        return true;
    }

    public void setColor(int red, int green, int blue) {
        mixer.reset(new Color(red, green, blue));
        notifyBlockUpdate();
        markDirty();
    }

    private void notifyBlockUpdate() {
        BlockState state = world.getBlockState(pos);
        this.world.notifyBlockUpdate(pos, state, state, 3);
    }

    @Override
    public SUpdateTileEntityPacket getUpdatePacket() {
        return new SUpdateTileEntityPacket(this.pos, -1, this.getUpdateTag());
    }

    @Override
    public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
        readItems(pkt.getNbtCompound());
        notifyBlockUpdate();
    }

    @Override
    public CompoundNBT getUpdateTag() {
        return write(new CompoundNBT());
    }
}
