package com.atlassian.bitbucket.web.conditions;

import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.Condition;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.user.UserType;

import java.util.Map;

public class IsUserOfTypeCondition implements Condition {
    String userKey;
    UserType userType;

    @Override
    public void init(Map<String, String> params) throws PluginParseException {
        if (params.containsKey("userKey")) {
            userKey = params.get("userKey");
        } else {
            userKey = "currentUser";
        }
        if (params.containsKey("userType")) {
            userType = UserType.valueOf(params.get("userType"));
        } else {
            userType = UserType.NORMAL;
        }
    }

    @Override
    public boolean shouldDisplay(Map<String, Object> context) {
        Object user = context.get(userKey);
        return user instanceof ApplicationUser && ((ApplicationUser) user).getType() == userType;
    }
}
