package top.r3944realms.lib39.core.compat;

import net.minecraft.resources.Identifier;

import java.util.concurrent.Callable;

/**
 * The interface Compat.
 */
public interface ICompat {
    /**
     * Sets initialize.
     *
     * @param initialize the initialize
     */
    void setInitialize(boolean initialize);

    /**
     * Is initialized boolean.
     *
     * @return the boolean
     */
    boolean isInitialized();

    /**
     * Id resource location.
     *
     * @return the resource location
     */
    Identifier id();

    /**
     * Initialize.
     */
    void initialize();

    /**
     * On load complete.
     */
    default void onLoadComplete() {}

    /**
     * Is mod loaded boolean.
     *
     * @return the boolean
     */
    default boolean isModLoaded() {
        return false;
    }

    /**
     * Call if present t.
     *
     * @param <T>      the type parameter
     * @param callable the callable
     * @return the t
     * @throws Exception the exception
     */
    default <T> T callIfPresent(Callable<T> callable) throws Exception {
        if (isModLoaded()) return callable.call();
        else return null;
    }

    /**
     * Call if pesent t.
     *
     * @param <T>      the type parameter
     * @param callable the callable
     * @param elseCall the else call
     * @return the t
     * @throws Exception the exception
     */
    default <T> T callIfPresent(Callable<T> callable, Callable<T> elseCall) throws Exception {
        if (isModLoaded()) return callable.call();
        else return elseCall.call();
    }

    /**
     * Run if present boolean.
     *
     * @param runnable the runnable
     * @return the boolean
     * @throws Exception the exception
     */
    default boolean runIfPresent(Runnable runnable) throws Exception {
        if (isModLoaded()) runnable.run(); else return false;
        return true;
    }
}
