package com.alterdekim.hearthhack.reflect;

import com.alterdekim.hearthhack.component.processor.Processor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Getter
@Slf4j
public class ReflectionLoader<V extends AbstractParser> {
    private Map<Integer, V> parsers;

    public void initReflect(Class<V> vClass) {
        this.parsers = new HashMap<>();
        Set<Class<? extends V>> processorClasses = new Reflections(vClass.getPackageName()).getSubTypesOf(vClass);
        processorClasses.forEach(c -> {
            try {
                var ci = c.getDeclaredConstructor().newInstance();
                this.parsers.put(ci.getId(), ci);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        });
    }
}
