package xyz.illuc.neuralgiacore;

import com.mojang.logging.LogUtils;
import com.simibubi.create.content.contraptions.bearing.MechanicalBearingBlockEntity;
import dan200.computercraft.api.ForgeComputerCraftAPI;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.EntityRenderersEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.slf4j.Logger;
import xyz.illuc.neuralgiacore.entities.OfflinePlayerEntity;
import xyz.illuc.neuralgiacore.entities.OfflinePlayerRenderer;
import xyz.illuc.neuralgiacore.peripherals.BearingPeripheral;
import xyz.illuc.neuralgiacore.registers.RegisterItems;


@Mod(NeuralgiaCoreMod.MOD_ID)
public class NeuralgiaCoreMod {
    public static final String MOD_ID = "neuralgiacore";

    public static final Logger LOGGER = LogUtils.getLogger();

    public static final DeferredRegister<EntityType<?>> ENTITY_TYPES =
            DeferredRegister.create(ForgeRegistries.ENTITY_TYPES, MOD_ID);

    public static final RegistryObject<EntityType<OfflinePlayerEntity>> GHOST = ENTITY_TYPES.register("ghost",
            () -> EntityType.Builder.<OfflinePlayerEntity>of(OfflinePlayerEntity::new, MobCategory.MISC)
                    .sized(2.0F, 4.0F)
                    .setCustomClientFactory((spawnEntity, world) -> new OfflinePlayerEntity(world))
                    .build("ghost"));

    public static void initialize() {
        LOGGER.info("Hello from neuralgia core!");
    }




    public NeuralgiaCoreMod() {
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();

        RegisterItems.ITEMS.register(modEventBus);

        ENTITY_TYPES.register(modEventBus);




        modEventBus.addListener(this::commonSetup);

        MinecraftForge.EVENT_BUS.register(this);
    }

    private void commonSetup(FMLCommonSetupEvent event) {
        event.enqueueWork(this::registerPeripherals);
    }

    private void registerPeripherals() {
        registerBearingPeripheral("mechanical_bearing");

        registerBearingPeripheral("clockwork_bearing");

        registerBearingPeripheral("windmill_bearing");
    }

    private void registerBearingPeripheral(String blockEntityName) {
        BlockEntityType<?> type = ForgeRegistries.BLOCK_ENTITY_TYPES.getValue(
                new ResourceLocation("create", blockEntityName)
        );

        if (type != null) {
            ForgeComputerCraftAPI.registerPeripheralProvider((level, pos, direction) -> {
                var blockEntity = level.getBlockEntity(pos);
                if (blockEntity != null && blockEntity.getType() == type) {
                    try {
                        return net.minecraftforge.common.util.LazyOptional.of(
                                () -> new BearingPeripheral((MechanicalBearingBlockEntity) blockEntity)
                        );

                    } catch (ClassCastException e) {
                        LOGGER.error("Failed to cast block entity to MechanicalBearingBlockEntity at {}", pos);
                        return null;
                    }
                }
                return null;
            });
            LOGGER.debug("Registered peripheral for {}", blockEntityName);
        } else {
            LOGGER.warn("Could not find block entity type: create:{}", blockEntityName);
        }
    }




}

