package top.r3944realms.lib39.core.event;

import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.server.level.ServerLevel;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.EntityRenderersEvent;
import net.minecraftforge.client.event.RegisterShadersEvent;
import net.minecraftforge.event.level.LevelEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import top.r3944realms.lib39.Lib39;
import top.r3944realms.lib39.client.model.DollModel;
import top.r3944realms.lib39.client.renderer.block.DollBlockEntityRenderer;
import top.r3944realms.lib39.client.shader.Lib39Shaders;
import top.r3944realms.lib39.content.register.Lib39BlockEntities;
import top.r3944realms.lib39.util.ILevelHelper;

/**
 * The type Client handler.
 */
public class ClientEventHandler {
    /**
     * The type Mod.
     */
    @net.minecraftforge.fml.common.Mod.EventBusSubscriber(value = Dist.CLIENT, bus = net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus.MOD, modid = Lib39.MOD_ID)
    public static class Mod extends ClientEventHandler {
        /**
         * On register shaders.
         *
         * @param event the event
         */
        @SubscribeEvent
        public static void onRegisterShaders(RegisterShadersEvent event) {
            Lib39Shaders.register(event);
        }

        /**
         * On register renderer.
         *
         * @param event the event
         */
        @SubscribeEvent
        public static void onRegisterRenderer (EntityRenderersEvent.RegisterRenderers event) {
            event.registerBlockEntityRenderer(Lib39BlockEntities.DOLL_BLOCK_ENTITY.get(),DollBlockEntityRenderer::new);
        }

        /**
         * Register layer definitions.
         *
         * @param event the event
         */
        @SubscribeEvent
        public static void registerLayerDefinitions(EntityRenderersEvent.RegisterLayerDefinitions event) {
            event.registerLayerDefinition(DollModel.LAYER_LOCATION, DollModel::createBodyLayer);
        }

    }

    /**
     * The type Game.
     */
    @net.minecraftforge.fml.common.Mod.EventBusSubscriber(value = Dist.CLIENT, bus = net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus.FORGE, modid = Lib39.MOD_ID)
    public static class Game extends ClientEventHandler {
        /**
         * Register layer definitions.
         *
         * @param event the event
         */
        @SubscribeEvent
        public static void registerLayerDefinitions(LevelEvent.Load event) {
           if (event.getLevel() != null && event.getLevel() instanceof ClientLevel level) {
               ILevelHelper.LevelHelper.CLIENT.setLevel(level);
           }
        }

        /**
         * Register layer definitions.
         *
         * @param event the event
         */
        @SubscribeEvent
        public static void registerLayerDefinitions(LevelEvent.Unload event) {
            if (event.getLevel() != null && event.getLevel() instanceof ClientLevel level) {
                ILevelHelper.LevelHelper.CLIENT.setLevel(null);
            }
        }
    }

}
