package net.halbear.aotg.custom.blocks.AlloyFurnace;

import net.halbear.aotg.AgeoftheGods;
import net.halbear.aotg.registries.ModBlocks;
import net.halbear.aotg.registries.ModItems;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.ItemContainerContents;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.level.BlockEvent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.halbear.aotg.custom.blocks.AlloyFurnace.AlloyFurnaceMultiBlock.FACING;
import static net.halbear.aotg.custom.blocks.AlloyFurnace.AlloyFurnaceMultiBlock.PARENT_BLOCK;

@EventBusSubscriber(modid = AgeoftheGods.MODID)
public class AlloyFurnaceUpdateHandler {

    public static final BlockPos[] parts = new BlockPos[]{new BlockPos(0,0,0),new BlockPos(-1,0,0),new BlockPos(-1,0,1),new BlockPos(0,0,1),
            new BlockPos(0,1,0),new BlockPos(-1,1,0),new BlockPos(-1,1,1),new BlockPos(0,1,1)};

    public static final Map<String, Integer> ModelPartLookup = new HashMap<>();

    static{
        for(int i = 0; i < 8; i++) {
            String PosString = Math.abs(parts[i].getX()) + "_" + Math.abs(parts[i].getY()) + "_" + Math.abs(parts[i].getZ());
            ModelPartLookup.put(PosString, i);
        }
    }

    public static int GetModelPart(BlockPos MultipartLocation){
        String PosString = Math.abs(MultipartLocation.getX()) + "_" + Math.abs(MultipartLocation.getY()) + "_" + Math.abs(MultipartLocation.getZ());
        return ModelPartLookup.get(PosString);
    }

    public static BlockPos GetParentPosFromModelPart(Level world, int ModelPart, BlockState state, BlockPos sourcePos){
        if(ModelPart == 0) return sourcePos;
        BlockPos offset = AlloyFurnaceUpdateHandler.parts[ModelPart];
        Direction direction = state.getValue(FACING);
        Rotation rotation =  getInverseRotationForDirection(direction);
        BlockPos Rotated = offset.rotate(rotation);
        BlockPos TargetPos = new BlockPos(sourcePos.getX() - Rotated.getX(), sourcePos.getY() - Rotated.getY(), sourcePos.getZ() - Rotated.getZ());
        if(world.getBlockState(TargetPos).getBlock().getName().getString().equals(ModBlocks.ALLOY_FURNACE.get().getName().getString()) &&
                world.getBlockState(TargetPos).getValue(PARENT_BLOCK)) return TargetPos;
        return null;
    }

    protected static ItemStack CreateFilledPot(ItemStack ItemToFillWith, ItemStack ItemToFillWith2){
        ItemStack potStack = new ItemStack(Items.DECORATED_POT);
        ItemStack liquidMarker = new ItemStack(ModItems.FLUID_INDICATOR_ITEM.asItem(),1);
        ItemContainerContents contents = ItemContainerContents.fromItems(List.of(liquidMarker, ItemToFillWith != null ? ItemToFillWith : ItemStack.EMPTY,ItemToFillWith2 != null ? ItemToFillWith2 : ItemStack.EMPTY));
        potStack.set(DataComponents.CONTAINER, contents);
        return potStack;
    }


    //@SubscribeEvent
    public static void onNeighborNotify(BlockEvent.NeighborNotifyEvent event) {
        var level = event.getLevel();
        var pos = event.getPos();
        var state = event.getState();
        var notifiedSides = event.getNotifiedSides();

    }

    //@SubscribeEvent
    public static void onBlockPlace(BlockEvent.EntityPlaceEvent event) {
        var level = event.getLevel();
        var pos = event.getPos();
        var state = event.getPlacedBlock();
    }

    public static BlockPos GetMultipartLocation(int ModelPart){
        return parts[Math.clamp(ModelPart,0,7)];
    }

    public static Rotation getInverseRotationForDirection(Direction facing) {
        return switch (facing) {
            case SOUTH -> Rotation.CLOCKWISE_180;
            case WEST -> Rotation.COUNTERCLOCKWISE_90;
            case EAST -> Rotation.CLOCKWISE_90;
            default -> Rotation.NONE;
        };
    }
}
