package de.dhbwstuttgart.target.tree.type;

import de.dhbwstuttgart.bytecode.FunNGenerator;

import java.util.List;

public record TargetFunNType(String name, List<TargetType> funNParams, List<TargetType> params, int returnArguments) implements TargetSpecializedType {

    public static TargetFunNType fromParams(List<TargetType> params, int returnArguments) {
        return fromParams(params, params, returnArguments);
    }

    public static TargetFunNType fromParams(List<TargetType> params, List<TargetType> realParams, int returnArguments) {
        var name = FunNGenerator.getSpecializedClassName(FunNGenerator.getArguments(params), FunNGenerator.getReturnType(params));
        return new TargetFunNType(name, params, realParams, returnArguments);
    }

    public String toMethodDescriptor() {
        var res = "(";
        for (var i = 0; i < funNParams.size() - 1; i++) {
            res += "Ljava/lang/Object;";
        }
        res += ")";
        if (returnArguments > 0) {
            res += "Ljava/lang/Object;";
        } else {
            res += "V";
        }
        return res;
    }

    @Override
    public String getInternalName() {
        return name;
    }

    @Override
    public String toSignature() {
        var args = FunNGenerator.getArguments(funNParams);
        return "LFun" + args.size() + "$$" + TargetSpecializedType.signatureParameters(funNParams) + ";";
    }
}
