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

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import top.r3944realms.lib39.api.callback.ActionResult;
import top.r3944realms.lib39.api.callback.SyncManagerRegisterCallback;
import top.r3944realms.lib39.core.compat.ICompatManager;
import top.r3944realms.lib39.core.event.FabricCommonEventHandler;
import top.r3944realms.lib39.core.sync.CachedSyncManager;
import top.r3944realms.lib39.example.content.data.AbstractedTestSyncData;
import top.r3944realms.lib39.example.content.data.FabricTestSyncData;
import top.r3944realms.lib39.example.core.compat.FabricLib39CompatManager;

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

/**
 * The type Fabric ex common event handler.
 */
public class FabricExCommonEventHandler {
    /**
     * Gets compat manager.
     *
     * @return the compat manager
     */
    public static ICompatManager getOrCreateCompatManager() {
        if (compatManager == null) {
            synchronized (FabricCommonEventHandler.class) {
                if (compatManager == null) {
                    compatManager = new FabricLib39CompatManager("compat");
                }
            }
        }
        return compatManager;
    }

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

    /**
     * Init.
     */
    public static void init() {
        SyncManagerRegisterCallback.EVENT.register(registrar -> {
            registrar.register(
                    FabricTestSyncData.ID,
                    new CachedSyncManager<>() {
                        private final Map<Entity, AbstractedTestSyncData> syncDataMap = new ConcurrentHashMap<>();

                        @Override
                        public Map<Entity, AbstractedTestSyncData> getSyncMap() {
                            return syncDataMap;
                        }
                    },
                    AbstractedTestSyncData.class
            );
            registrar.allowEntityClass(FabricTestSyncData.ID, LivingEntity.class);
            return ActionResult.PASS;
        });
    }
}
