package top.r3944realms.lib39.core.event;

import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.ShaderInstance;
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 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.core.register.Lib39BlockEntities;
import top.r3944realms.lib39.util.ILevelHelper;

import java.io.IOException;

/**
 * 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
         * @throws IOException the io exception
         */
        @SubscribeEvent
        public static void onRegisterShaders(RegisterShadersEvent event) throws IOException {
            event.registerShader(new ShaderInstance(event.getResourceProvider(), Lib39.rl("ring"), DefaultVertexFormat.POSITION_COLOR), Lib39Shaders::setRingShader);
            event.registerShader(new ShaderInstance(event.getResourceProvider(), Lib39.rl("selection"), DefaultVertexFormat.POSITION_COLOR), Lib39Shaders::setSelectionShader);
        }

        /**
         * 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 obClientUnload(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 obClientUnload(LevelEvent.Unload event) {
            if (event.getLevel() != null && event.getLevel() instanceof ClientLevel level) {
                ILevelHelper.LevelHelper.CLIENT.setLevel(null);
            }
        }
    }

}
