package top.r3944realms.lib39.core.compat;

import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.r3944realms.lib39.Lib39;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * The type Compat manager.
 */
@SuppressWarnings("unused")
public abstract class CompatManager {
    public ResourceLocation getId() {
        return id;
    }
    protected final Logger logger;
    protected final ResourceLocation id;
    protected final Map<ResourceLocation, ICompat> compats = new HashMap<>();
    protected boolean initialized = false;
    protected final List<Runnable> pendingTasks = new ArrayList<>();
    protected final IEventBus modEventBus, gameEventBus;

    // 存储事件监听器配置
    protected final List<ListenerConfig> listenerConfigs = new ArrayList<>();

    public void initialize() {
        initializeAllCompat();
    }

    public CompatManager(ResourceLocation id, IEventBus modEventBus, IEventBus gameEventBus) {
        this.id = id;
        this.modEventBus = modEventBus;
        this.gameEventBus = gameEventBus;
        this.logger = LoggerFactory.getLogger(id.toString());
    }

    /**
     * Register compat.
     *
     * @param id     the id
     * @param compat the compat
     */
    public void registerCompat(ResourceLocation id, ICompat compat) {
        if (initialized) {
            // 已初始化，直接注册
            doRegisterCompat(id, compat);
        } else {
            // 未初始化，缓存起来
            pendingTasks.add(() -> doRegisterCompat(id, compat));
            logger.debug("Cached compat registration for: {}", id);
        }
    }

    private void doRegisterCompat(ResourceLocation id, ICompat compat) {
        if (compats.containsKey(id)) {
            logger.warn("Compat with id {} is already registered!", id);
            return;
        }
        compats.put(id, compat);
        addListenerForCompat(id);
        logger.debug("Registered compat: {}", id);
    }

    /**
     * Register compat.
     *
     * @param namespace the namespace
     * @param path      the path
     * @param compat    the compat
     */
    public void registerCompat(String namespace, String path, ICompat compat) {
        registerCompat(Lib39.rl(namespace, path), compat);
    }

    /**
     * 为所有兼容模块配置事件监听器
     *
     * @param dists the dists
     * @param bus   the bus
     */
    public void addListenerForAll(@Nullable Dist dists, Mod.EventBusSubscriber.Bus bus) {
        listenerConfigs.add(new ListenerConfig(null, dists, bus));
    }

    /**
     * 为特定兼容模块配置事件监听器
     *
     * @param compatId the compat id
     * @param dists    the dists
     * @param bus      the bus
     */
    public void addListenerForCompat(ResourceLocation compatId, @Nullable Dist dists, Mod.EventBusSubscriber.Bus bus) {
        listenerConfigs.add(new ListenerConfig(compatId, dists, bus));
    }

    /**
     * 为特定兼容模块配置事件监听器
     *
     * @param compatId the compat id
     */
    public void addListenerForCompat(ResourceLocation compatId) {
        addListenerForCompat(compatId, null, Mod.EventBusSubscriber.Bus.FORGE);
        addListenerForCompat(compatId, null, Mod.EventBusSubscriber.Bus.MOD);

        addListenerForCompat(compatId, Dist.CLIENT, Mod.EventBusSubscriber.Bus.FORGE);
        addListenerForCompat(compatId, Dist.CLIENT, Mod.EventBusSubscriber.Bus.MOD);

        addListenerForCompat(compatId, Dist.DEDICATED_SERVER, Mod.EventBusSubscriber.Bus.FORGE);
        addListenerForCompat(compatId, Dist.DEDICATED_SERVER, Mod.EventBusSubscriber.Bus.MOD);
    }

    /**
     * 为已加载的兼容模块配置事件监听器
     *
     * @param dists    the dists
     * @param bus      the bus
     * @param consumer the consumer
     */
    public void addListenerForLoaded(@Nullable Dist dists, Mod.EventBusSubscriber.Bus bus, Consumer<IEventBus> consumer) {
        listenerConfigs.add(new ListenerConfig(null, dists, bus) {
            @Override
            boolean shouldApply(@NotNull ICompat compat) {
                return super.shouldApply(compat);
            }
        });
    }

    // ===================== 初始化和管理 =====================

    /**
     * 初始化所有兼容模块并应用事件监听器
     */
    protected synchronized void initializeAllCompat() {
        logger.info("Initializing {} compatibility modules", compats.size());

        // 先处理所有缓存的注册
        pendingTasks.forEach(Runnable::run);
        pendingTasks.clear();

        // 初始化所有兼容模块
        for (Map.Entry<ResourceLocation, ICompat> entry : compats.entrySet()) {
            if (!entry.getValue().isInitialized()) {
                try {
                    entry.getValue().initialize();
                    entry.getValue().setInitialize(true);
                    logger.info("Initialized compat: {}", entry.getKey());
                } catch (Exception e) {
                    logger.error("Failed to initialize compat: {}", entry.getKey(), e);
                }
            }
        }
        initialized = true;
        // 2. 然后应用所有事件监听器
        applyAllEventListeners();
    }

    /**
     * 应用所有配置的事件监听器到对应的 ICompat 实例
     */
    private void applyAllEventListeners() {
        logger.info("Applying {} event listener configurations", listenerConfigs.size());

        for (ListenerConfig config : listenerConfigs) {
            if (config.compatId == null) {
                // 应用到所有兼容模块
                applyListenerToAllCompats(config);
            } else {
                // 应用到特定兼容模块
                applyListenerToCompat(config.compatId, config);
            }
        }
    }

    /**
     * 将监听器应用到所有兼容模块
     */
    private void applyListenerToAllCompats(ListenerConfig config) {
        for (ICompat compat : compats.values()) {
            if(!compat.isInitialized()) {
                if (config.shouldApply(compat)) {
                    applyListenerToCompat(compat, config);
                }
            }
        }
    }

    /**
     * 将监听器应用到特定兼容模块
     */
    private void applyListenerToCompat(ResourceLocation compatId, ListenerConfig config) {
        ICompat compat = compats.get(compatId);
        if (compat != null && config.shouldApply(compat)) {
            applyListenerToCompat(compat, config);
        }
    }

    /**
     * 将监听器应用到具体的 ICompat 实例
     */
    private void applyListenerToCompat(ICompat compat, ListenerConfig config) {
        try {
            // 根据配置调用对应的 ICompat 方法
            if (config.dists != null) {
                switch (config.dists) {
                    case CLIENT -> {
                            if (config.bus == Mod.EventBusSubscriber.Bus.FORGE) {
                                compat.addClientGameListener(gameEventBus);
                            } else {
                                compat.addClientModListener(modEventBus);
                            }
                    }
                    case DEDICATED_SERVER -> {
                            if (config.bus == Mod.EventBusSubscriber.Bus.FORGE) {
                                compat.addServerGameListener(gameEventBus);
                            } else {
                                compat.addServerModListener(modEventBus);
                            }
                    }
                }
            } else {
                // 通用监听器
                if (config.bus == Mod.EventBusSubscriber.Bus.FORGE) {
                    compat.addCommonGameListener(gameEventBus);
                } else {
                    compat.addCommonModListener(modEventBus);
                }
            }

            logger.debug("Applied {} listener to compat: {}",
                    getListenerTypeName(config), compat.id());

        } catch (Exception e) {
            logger.error("Failed to apply listener to compat: {}", compat.id(), e);
        }
    }

    /**
     * 获取监听器类型名称（用于日志）
     */
    private @NotNull String getListenerTypeName(@NotNull ListenerConfig config) {
        if (config.dists != null) {
            return config.dists.name().toLowerCase() + " " +
                    (config.bus == Mod.EventBusSubscriber.Bus.FORGE ? "game" : "mod");
        } else {
            return "common " + (config.bus == Mod.EventBusSubscriber.Bus.FORGE ? "game" : "mod");
        }
    }


    /**
     * Add listener for compat.
     *
     * @param compatId the compat id
     * @param bus      the bus
     */
    public void addListenerForCompat(ResourceLocation compatId, Mod.EventBusSubscriber.Bus bus) {
        addListenerForCompat(compatId, null, bus);
    }

    protected static class ListenerConfig {
        /**
         * The Compat id.
         */
        final ResourceLocation compatId;
        /**
         * The Dists.
         */
        final Dist dists;
        /**
         * The Bus.
         */
        final Mod.EventBusSubscriber.Bus bus;

        /**
         * Instantiates a new Listener config.
         *
         * @param compatId the compat id
         * @param dists    the dists
         * @param bus      the bus
         */
        ListenerConfig(ResourceLocation compatId, Dist dists, Mod.EventBusSubscriber.Bus bus) {
            this.compatId = compatId;
            this.dists = dists;
            this.bus = bus;
        }

        /**
         * Should apply boolean.
         *
         * @param compat the compat
         * @return the boolean
         */
        boolean shouldApply(@NotNull ICompat compat) {
            return compat.isModLoaded();
        }
    }

    // ===================== 其他方法 =====================

    /**
     * On load complete.
     */
    public void onLoadComplete() {
        logger.info("Calling onLoadComplete for {} compatibility modules", compats.size());
        for (Map.Entry<ResourceLocation, ICompat> entry : compats.entrySet()) {
            try {
                entry.getValue().onLoadComplete();
            } catch (Exception e) {
                logger.error("Error in onLoadComplete for compat: {}", entry.getKey(), e);
            }
        }
    }

    /**
     * Gets compat.
     *
     * @param id the id
     * @return the compat
     */
    public Optional<ICompat> getCompat(ResourceLocation id) {
        return Optional.ofNullable(compats.get(id));
    }

    /**
     * Has compat boolean.
     *
     * @param id the id
     * @return the boolean
     */
    public boolean hasCompat(ResourceLocation id) {
        return compats.containsKey(id);
    }

    /**
     * Unregister compat.
     *
     * @param id the id
     */
    public void unregisterCompat(ResourceLocation id) {
        ICompat removed = compats.remove(id);
        if (removed != null) {
            logger.debug("Unregistered compat: {}", id);
        }
    }

    /**
     * Gets loaded compats.
     *
     * @return the loaded compats
     */
    public List<ICompat> getLoadedCompats() {
        return compats.values().stream()
                .filter(ICompat::isModLoaded)
                .collect(Collectors.toList());
    }


}