package de.dhbwstuttgart.parser.scope;

import de.dhbwstuttgart.exceptions.NotImplementedException;

import java.util.*;

/**
 * Speichert die Klassen für einen bestimmten Projektscope
 */
public class JavaClassRegistry{
    final Map<JavaClassName, Integer> existingClasses = new HashMap<>();

    public JavaClassRegistry(Map<String, Integer> initialNames) {
        addNames(initialNames);
    }

    public JavaClassRegistry() {}

    public void addNames(Map<String, Integer> names) {
        for (String name : names.keySet()) {
            existingClasses.put(new JavaClassName(name), names.get(name));
        }
    }

    public Set<JavaClassName> getAllClassNames(){
        return existingClasses.keySet();
    }

    public void addName(String className, int numberOfGenerics) {
        existingClasses.put(new JavaClassName(className), numberOfGenerics);
    }

    public JavaClassName getName(String className) {
        for (JavaClassName name : existingClasses.keySet()) {
            if (name.equals(new JavaClassName(className)))
                return name;
        }
        throw new RuntimeException("Class " + className + " not found!");
    }

    @Override
    public String toString() {
        return existingClasses.toString();
    }

    public List<JavaClassName> getAllFromPackage(String packageName) {
        List<JavaClassName> ret = new ArrayList<>();
        for (JavaClassName className : this.existingClasses.keySet()) {
            JavaClassName toCompare = new JavaClassName(
                    packageName + "." + JavaClassName.stripClassName(className.toString()));
            if (toCompare.toString().equals(className.toString())) {
                ret.add(className);
            }
        }
        return ret;
    }

    public boolean contains(String whole) {
        return existingClasses.containsKey(new JavaClassName(whole));
    }

    public boolean contains(JavaClassName name) {
        return existingClasses.containsKey(name);
    }

    public int getNumberOfGenerics(String name) {
        return existingClasses.getOrDefault(new JavaClassName(name), 0);
    }
}
