package ast;

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


public class InterfaceDeclaration extends AbstractTypeDeclaration implements IAnnotatable {
    private List<MethodDeclaration> methods = new ArrayList<>();
    private Map<String, Annotation> annotations = new HashMap<>();

    public InterfaceDeclaration(String typeName) {
        this.typeName = typeName;
    }

    public void addMethod(MethodDeclaration method) {
        methods.add(method);
    }

    public void removeMethod(MethodDeclaration method) {
        methods.remove(method);
    }

    public List<MethodDeclaration> getMethods() {
        return methods;
    }

    @Override
    public Annotation getAnnotation(String name) {
        return annotations.get(name);
    }

    @Override
    public Collection<Annotation> getAnnotations() {
        return annotations.values();
    }

    @Override
    public void addAnnotation(Annotation annotation) {
        annotations.put(annotation.getElementName(), annotation);
    }

    public String toString() {
        String code = "";
        for (Annotation annotation : getAnnotations()) {
            code += annotation.toString() + "\n";
        }
        code += "public interface " + typeName + " {\n";
        for (MethodDeclaration m : methods) {
            String signature = "\t";
            if (m.getReturnType() == null) {
                signature += "void ";
            } else {
                signature += m.getReturnType().getInterfaceTypeName() + " ";
            }
            signature += m.getName() + "(";
            if (m.getParameters() != null) {
                for (int i = 0; i < m.getParameters().size(); i++) {
                    signature += m.getParameters().get(i).toString();
                    if (i < m.getParameters().size() - 1) signature += ", ";
                }
            }
            signature += ");\n";
            code += signature;
        }
        code += "}";
        return code;
    }
}