package com.atlassian.stash.internal.repository.ref.restriction.rest;

import com.atlassian.bitbucket.repository.ref.restriction.RefMatcherType;
import com.atlassian.bitbucket.rest.RestMapEntity;

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

public class RestRestrictionMatcherType extends RestMapEntity {

    private final String ID = "id";
    private final String NAME = "name";

    private RestRestrictionMatcherType(String id, String name) {
        put(ID, id);
        put(NAME, name);
    }

    private RestRestrictionMatcherType(Map<String, Object> values) {
        super(values);
    }

    public RestRestrictionMatcherType() {
    }

    public RestRestrictionMatcherType(RefMatcherType type) {
        this(type.getId(), type.getDisplayId());
    }

    public String getId() {
        return getStringProperty(ID);
    }

    public String getName() {
        return getStringProperty(NAME);
    }

    @Nullable
    @SuppressWarnings("unchecked")
    public static RestRestrictionMatcherType valueOf(@Nullable Object object) {
        if (object instanceof RestRestrictionMatcherType) {
            return (RestRestrictionMatcherType) object;
        }
        if (object instanceof Map) {
            return new RestRestrictionMatcherType((Map<String, Object>) object);
        }
        return null;
    }
}
