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

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

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

public class RestRefMatcher extends RestMapEntity {

    private final String DISPLAY_ID = "displayId";
    private final String ID = "id";
    private final String TYPE = "type";
    private final String ACTIVE = "active";

    private RestRefMatcher(String id, String displayId, RestRestrictionMatcherType type, boolean active) {
        put(ID, id);
        put(DISPLAY_ID, displayId);
        put(TYPE, type);
        put(ACTIVE, active);
    }

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

    public RestRefMatcher(RefMatcher matcher) {
        this(matcher.getId(), matcher.getDisplayId(), new RestRestrictionMatcherType(matcher.getType()), matcher.isActive());
    }

    public String getDisplayId() {
        return getStringProperty(DISPLAY_ID);
    }

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

    public RestRestrictionMatcherType getType() {
        return RestRestrictionMatcherType.valueOf(get(TYPE));
    }

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