package dev.micle.firefly_bush_backport.proxy;

import dev.micle.firefly_bush_backport.FireflyBushBackport;
import dev.micle.firefly_bush_backport.block.ModBlocks;
import dev.micle.firefly_bush_backport.config.Config;
import dev.micle.firefly_bush_backport.creative_mode_tab.ModCreativeModeTabs;
import dev.micle.firefly_bush_backport.item.ModItems;
import dev.micle.firefly_bush_backport.particle.FireflyParticle;
import dev.micle.firefly_bush_backport.particle.ModParticles;
import dev.micle.firefly_bush_backport.sound.ModSounds;
import net.minecraft.client.Minecraft;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.RegisterParticleProvidersEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.event.server.ServerStartedEvent;
import net.minecraftforge.event.server.ServerStoppingEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.event.lifecycle.*;

public class Proxy implements IProxy {
    private static MinecraftServer server = null;

    // Common setup
    public Proxy() {
        IEventBus modEventBus = FireflyBushBackport.getFMLJavaModLoadingContext().getModEventBus();

        Config.register();
        ModBlocks.register(modEventBus);
        ModItems.register(modEventBus);
        ModCreativeModeTabs.register(modEventBus);
        ModSounds.register(modEventBus);

        // Register mod event bus listeners
        modEventBus.addListener(Proxy::setup);
        modEventBus.addListener(Proxy::imcEnqueue);
        modEventBus.addListener(Proxy::imcProcess);

        // Register event bus listeners
        MinecraftForge.EVENT_BUS.addListener(Proxy::onAddReloadListeners);
        MinecraftForge.EVENT_BUS.addListener(Proxy::serverStarted);
        MinecraftForge.EVENT_BUS.addListener(Proxy::serverStopping);
    }

    private static void setup(FMLCommonSetupEvent event) {}

    private static void imcEnqueue(InterModEnqueueEvent event) {}

    private static void imcProcess(InterModProcessEvent event) {}

    private static void onAddReloadListeners(AddReloadListenerEvent event) {}

    private static void serverStarted(ServerStartedEvent event) {
        server = event.getServer();
    }

    private static void serverStopping(ServerStoppingEvent event) {
        server = null;
    }

    @Override
    public MinecraftServer getServer() {
        return server;
    }

    @Override
    public Player getClientPlayer() {
        return null;
    }

    @Override
    public Level getClientLevel() {
        return null;
    }

    // Client setup
    public static class Client extends Proxy {
        public Client() {
            IEventBus modEventBus = FireflyBushBackport.getFMLJavaModLoadingContext().getModEventBus();

            ModParticles.register(modEventBus);

            // Register mod event bus listeners
            modEventBus.addListener(Client::setup);
            modEventBus.addListener(Client::postSetup);
            modEventBus.addListener(Client::registerParticleFactories);
        }

        private static void setup(FMLClientSetupEvent event) {}

        private static void postSetup(FMLLoadCompleteEvent event) {}

        private static void registerParticleFactories(RegisterParticleProvidersEvent event) {
            event.registerSpriteSet(ModParticles.FIREFLY.get(), FireflyParticle.FireflyProvider::new);
        }

        @Override
        @OnlyIn(Dist.CLIENT)
        public Player getClientPlayer() {
            return Minecraft.getInstance().player;
        }

        @Override
        @OnlyIn(Dist.CLIENT)
        public Level getClientLevel() {
            return Minecraft.getInstance().level;
        }
    }

    // Server setup
    public static class Server extends Proxy {
        public Server() {
            // Register mod event bus listeners
            IEventBus modEventBus = FireflyBushBackport.getFMLJavaModLoadingContext().getModEventBus();
            modEventBus.addListener(Server::setup);
        }

        private static void setup(FMLDedicatedServerSetupEvent event) {}
    }
}
