package net.pietru.omni_power.blocks;

import com.badlogic.gdx.math.Quaternion;
import com.badlogic.gdx.math.Vector3;
import finalforeach.cosmicreach.GameSingletons;
import finalforeach.cosmicreach.blockentities.BlockEntityCreator;
import finalforeach.cosmicreach.blockentities.BlockEntityItemContainer;
import finalforeach.cosmicreach.blocks.Block;
import finalforeach.cosmicreach.blocks.BlockState;
import finalforeach.cosmicreach.blocks.MissingBlockStateResult;
import finalforeach.cosmicreach.entities.player.Player;
import finalforeach.cosmicreach.items.ItemStack;
import finalforeach.cosmicreach.items.containers.SlotContainer;
import finalforeach.cosmicreach.world.Zone;
import net.pietru.omni_power.api.HoloBlock;
import net.pietru.omni_power.ui.CustomScreen;

import static net.pietru.omni_power.OmniPower.MOD_ID;

public class CustomUIBlockEntity extends BlockEntityItemContainer {

    public final String ui_id;
    public CustomUIBlockEntity(Zone zone, int globalX, int globalY, int globalZ, SlotContainer slotContainer, String gui_id) {
        super(zone, globalX, globalY, globalZ, slotContainer);
        this.ui_id=gui_id;
        for (int i = 0; i < 360; i++) {
            HoloBlock hb = new HoloBlock(
                    new ItemStack(BlockState.getInstance("base:c4[default]", MissingBlockStateResult.EXCEPTION).getItem()),
                    new Vector3(globalX, globalY+1, globalZ),
                    (float) (.9f-((.1/360.0)*i))
            );
            hb.renderRotation=new Quaternion(Vector3.Y,i);
            zone.addEntity(hb);
        }
    }

    @Override
    public void onInteract(Player player, Zone zone) {
        CustomScreen.addOpenContainer(this,slotContainer);
    }

    @Override
    public String getBlockEntityId() {
        return MOD_ID+":CustomUIBlockEntity";
    }

    public static void registerBlockEntityCreator() {
        BlockEntityCreator.registerBlockEntityCreator(MOD_ID+":CustomUIBlockEntity", (blockState, zone, x, y, z) -> {
            Block block = blockState.getBlock();
            int numSlots = getBlockEntityParamInt(block, "numSlots", 1);
            String ui_id = getBlockEntityParamString(block, "gui");
            SlotContainer slotContainer = new SlotContainer(numSlots);
            return new CustomUIBlockEntity(zone, x, y, z, slotContainer, ui_id);
        });
    }

}
