package io.quarkus.scheduler.runtime;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.concurrent.ExecutorService;

import com.cronutils.model.CronType;

public interface SchedulerContext {

    ExecutorService getExecutor();

    CronType getCronType();

    List<ScheduledMethodMetadata> getScheduledMethods();

    @SuppressWarnings("unchecked")
    default ScheduledInvoker createInvoker(String invokerClassName) {
        try {
            Class<? extends ScheduledInvoker> invokerClazz = (Class<? extends ScheduledInvoker>) Thread.currentThread()
                    .getContextClassLoader()
                    .loadClass(invokerClassName);
            return invokerClazz.getDeclaredConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException
                | InvocationTargetException e) {
            throw new IllegalStateException("Unable to create invoker: " + invokerClassName, e);
        }
    }

    static String getConfigProperty(String val) {
        return val.substring(1, val.length() - 1);
    }

    static boolean isConfigValue(String val) {
        val = val.trim();
        return val.startsWith("{") && val.endsWith("}");
    }
}