package com.atlassian.stash.internal.web.soy.functions.nav;

import com.atlassian.soy.renderer.JsExpression;
import com.atlassian.stash.internal.web.soy.functions.SoyArgumentUtils;
import com.atlassian.bitbucket.nav.NavBuilder;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.user.UserType;

public class UserNavFunction extends BaseNavFunction {

    public UserNavFunction(NavBuilder navBuilder) {
        super(navBuilder, "nav_user", SoyArgumentUtils.argCountFromTo(0, 2));
    }

    @Override
    protected NavBuilder.Builder initBuilder(Object... args) {
        SoyArgumentUtils.ensureArgumentTypeIsOneOf(args, 0, ApplicationUser.class, String.class);
        if (args[0] instanceof ApplicationUser) {
            return navBuilder.user((ApplicationUser) args[0]);
        }
        // a user slug has been passed in - check whether a UserType has also been provided. If not, default to NORMAL
        UserType userType = UserType.NORMAL;
        if (args.length > 1) {
            SoyArgumentUtils.ensureArgumentTypeIsOneOf(args, 1, UserType.class);
            userType = (UserType) args[1];
        }
        return navBuilder.userBySlug((String) args[0], userType);
    }

    @Override
    protected JsNavBuilder initJsBuilder(JsExpression... jsExpressions) {
        return new JsNavBuilder().user(jsExpressions[0]);
    }
}
