/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2026 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */

package net.ccbluex.liquidbounce.utils.client.exploitpreventer;

import com.nikoverflow.exploitpreventer.ExploitPreventer;
import com.nikoverflow.exploitpreventer.module.Module;
import com.nikoverflow.exploitpreventer.module.Modules;
import net.ccbluex.liquidbounce.LiquidBounce;
import org.jspecify.annotations.Nullable;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Compatibility layer for ExploitPreventer
 * <p>
 * DO NOT CALL ANY OF THESE METHODS WITHOUT CHECKING IF EXPLOIT PREVENTER IS LOADED
 */
public enum ExpCompatibility {

    INSTANCE;

    /**
     * Retrieves a map of ExploitPreventer module enum names to their display names.
     *
     * @return Map of module enum names to display names, or null if an error occurs.
     */
    public Map<String, String> getModules() {
        try {
            return Arrays.stream(Modules.values()).collect(Collectors.toMap(
                Modules::name,
                Modules::getName
            ));
        } catch (Throwable throwable) {
            LiquidBounce.INSTANCE.getLogger().error("Failed to list ExploitPreventer modules", throwable);
            return Map.of();
        }
    }

    private @Nullable Module getModuleByName(String name) {
        try {
            var enumModule = Modules.valueOf(name);
            return ExploitPreventer.getAPI().getModuleManager().getModule(enumModule);
        } catch (IllegalArgumentException e) {
            LiquidBounce.INSTANCE.getLogger().error("ExploitPreventer module {} not found", name);
            return null;
        }
    }

    public boolean isModuleEnabled(String moduleName) {
        try {
            var module = getModuleByName(moduleName);
            return module != null && module.isEnabled();
        } catch (Throwable throwable) {
            LiquidBounce.INSTANCE.getLogger().error("Failed to check ExploitPreventer module state for {}", moduleName, throwable);
            return false;
        }
    }

    public void setModuleEnabled(String moduleName, boolean enabled) {
        try {
            var module = getModuleByName(moduleName);
            if (module != null) {
                module.setEnabled(enabled);
            }
        } catch (Throwable throwable) {
            LiquidBounce.INSTANCE.getLogger().error("Failed to set ExploitPreventer module state for {}", moduleName, throwable);
        }
    }

}
