package top.r3944realms.lib39.example.core.event;

import net.minecraft.world.entity.Entity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.RegisterCapabilitiesEvent;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import top.r3944realms.lib39.api.event.SyncManagerRegisterEvent;
import top.r3944realms.lib39.core.compat.CompatManager;
import top.r3944realms.lib39.core.event.CommonEventHandler;
import top.r3944realms.lib39.core.sync.CachedSyncManager;
import top.r3944realms.lib39.example.compat.Lib39Compat;
import top.r3944realms.lib39.example.compat.Lib39CompatManager;
import top.r3944realms.lib39.example.content.capability.AbstractedTestSyncData;
import top.r3944realms.lib39.example.content.capability.ExCapabilityHandler;
import top.r3944realms.lib39.example.content.capability.TestSyncData;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * The type Common handler.
 */
public class ExCommonEventHandler {
    /**
     * The type Game.
     */
    @SuppressWarnings("unused")

    public static class Game extends ExCommonEventHandler {
        /**
         * Attach capability.
         *
         * @param event the event
         */
        @SubscribeEvent
        public static void attachCapability(AttachCapabilitiesEvent<?> event) {
            ExCapabilityHandler.attachCapability(event);
        }

        /**
         * On register sync.
         *
         * @param event the event
         */
        @SubscribeEvent
        public static void onRegisterSync (SyncManagerRegisterEvent event) {
            event.registerSyncManager(
                    TestSyncData.ID,
                    new CachedSyncManager<Entity, AbstractedTestSyncData>() {
                        private final Map<Entity, AbstractedTestSyncData> syncDataMap = new ConcurrentHashMap<>();
                        @Override
                        public Map<Entity, AbstractedTestSyncData> getSyncMap() {
                            return syncDataMap;
                        }
                    },
                    ExCapabilityHandler.TEST_CAP

            );
        }

    }


    /**
     * The type Mod.
     */
    public static class Mod extends ExCommonEventHandler {
        public static final IEventBus EVENT_BUS = FMLJavaModLoadingContext.get().getModEventBus();
        /**
         * Gets compat manager.
         *
         * @return the compat manager
         */
        public static CompatManager getOrCreateCompatManager() {
            if (compatManager == null) {
                synchronized (CommonEventHandler.Mod.class) {
                    if (compatManager == null) {
                        compatManager = new Lib39CompatManager("compat", EVENT_BUS, MinecraftForge.EVENT_BUS);
                    }
                }
            }
            return compatManager;
        }

        /**
         * The Compat manager.
         */
        static volatile CompatManager compatManager;


        /**
         * On construct mod.
         *
         * @param event the event
         */
        @SubscribeEvent
        public static void onConstructMod(FMLConstructModEvent event) {
            event.enqueueWork(() -> {
                CompatManager orCreateCompatManager = Mod.getOrCreateCompatManager();
                orCreateCompatManager
                        .registerCompat(Lib39Compat.ID, Lib39Compat.INSTANCE);
                orCreateCompatManager.initialize();
            });
        }

        /**
         * Register capability.
         *
         * @param event the event
         */
        @SubscribeEvent
        public static void registerCapability(RegisterCapabilitiesEvent event) {
            ExCapabilityHandler.registerCapability(event);
        }

    }
}
