package de.dhbwstuttgart.strucTypes5.assumptions;

import de.dhbwstuttgart.strucTypes5.typeVars.TypeVar;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVarAbstract;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sebastian on 20.04.17.
 */
public class AssumptionMap {


    private Map<String, AssumptionMethod> mAss = new HashMap<>();
    private Map<String, AssumptionField> fass = new HashMap<>();
    private Map<String, AssumptionArgument> argAss = new HashMap<>();
    private Map<String, AssumptionClass> classAss = new HashMap<>();


    public void addAssumption(AssumptionAbstract ass) {

        if (ass instanceof AssumptionMethod) {
           AssumptionMethod assMethod = (AssumptionMethod) ass;
           mAss.put(assMethod.getName() , assMethod);
        }
        else if (ass instanceof  AssumptionField) {
            AssumptionField assumptionField = (AssumptionField) ass;
            fass.put(assumptionField.getName(), assumptionField);
        }
        else if (ass instanceof AssumptionArgument) {
            AssumptionArgument assumptionArgument = (AssumptionArgument) ass;
            argAss.put(assumptionArgument.getNameOfArgument(), assumptionArgument);
        }
        else if (ass instanceof AssumptionClass) {
            AssumptionClass assumptionClass = (AssumptionClass) ass;
            classAss.put(assumptionClass.getCl().getClassName().toString() , assumptionClass);
        }
        else {
            System.err.println("Fehler Ass nicht bekannt");
        }
    }


    public void addAll(List<AssumptionAbstract> assumptionAbstractList) {
        for (AssumptionAbstract ass : assumptionAbstractList) {
            addAssumption(ass);
        }
    }


    public TypeVar getReturnTypeMethod(String name) {
        return mAss.get(name).getReturnType();
    }


    public TypeVarAbstract getVarAssumption(String name) {
        // gibt lokale Var zurück. wenn nicht vorhanden gloable. wenn nicht vorhanden null

        if (argAss.containsKey(name)) {
            return argAss.get(name).getTypeOfArgument();
        }
        else if (fass.containsKey(name)) {
            return fass.get(name).getTypeVar();
        }
        else {
            System.err.println("Kein Wert gespeichert");
            return null;
        }
    }



    public boolean putClass(AssumptionClass ass){
        classAss.put( ass.getCl().getClassName().toString(), ass);
        return true;
    }

    public TypeVarAbstract getThisVar(String name) {
        return fass.get(name).getTypeVar();
    }


    public AssumptionClass getClassAssumption(String className) {
        return classAss.get(className);
    }


    @Override
    public String toString() {
        return String.format("AssMethod \n  AssField: %s \n AssArgument %s  \n AssClass %s " , mAss.toString() , fass.toString() , argAss.toString() , classAss.toString());
    }
}

