package com.atlassian.bitbucket.rest.permission;

import com.atlassian.bitbucket.permission.EffectivePermissionVisitor;
import com.atlassian.bitbucket.permission.EffectiveRepositoryPermission;

import javax.annotation.Nullable;
import java.util.Map;

public class RestEffectiveRepositoryPermission extends RestEffectivePermission implements EffectiveRepositoryPermission {

    @SuppressWarnings("unused") //Required for Jersey
    public RestEffectiveRepositoryPermission() {
    }

    public RestEffectiveRepositoryPermission(EffectiveRepositoryPermission permission) {
        super(permission);
        put("repositoryId", permission.getRepositoryId());
    }

    public RestEffectiveRepositoryPermission(Map<String, Object> map) {
        super(map);
    }

    @Override
    public <T> T accept(EffectivePermissionVisitor<T> visitor) {
        return visitor.visit(this);
    }

    @Override
    public int getRepositoryId() {
        return getIntProperty("repositoryId");
    }

    @Nullable
    @SuppressWarnings("unchecked")
    public static RestEffectiveRepositoryPermission valueOf(@Nullable Object value) {
        if (value instanceof RestEffectiveRepositoryPermission) {
            return (RestEffectiveRepositoryPermission) value;
        } else if (value instanceof Map && ((Map) value).containsKey("repositoryId")) {
            return new RestEffectiveRepositoryPermission((Map) value);
        }

        return null;
    }
}
