package com.atlassian.bitbucket.rest.pull;

import com.atlassian.bitbucket.rest.RestMapEntity;
import org.codehaus.jackson.map.annotate.JsonSerialize;

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

@JsonSerialize
public class RestPullRequestMergeRequest extends RestMapEntity {

    private static final String CONTEXT = "context";
    private static final String MESSAGE = "message";
    private static final String STRATEGY_ID = "strategyId";
    private static final String VERSION = "version";

    public RestPullRequestMergeRequest() {
    }

    @Nonnull
    public Map<String, Object> getContext() {
        @SuppressWarnings("unchecked")
        Map<String, Object> context = (Map) get(CONTEXT);
        if (context == null) {
            context = Collections.emptyMap();
        }

        return context;
    }

    @Nullable
    public String getMessage() {
        return getStringProperty(MESSAGE);
    }

    /**
     * @return the requested merge strategy ID, which may be {@code null} if the default strategy should be used
     * @since 4.9
     */
    @Nullable
    public String getStrategyId() {
        return getStringProperty(STRATEGY_ID);
    }

    public int getVersion() {
        return getIntProperty(VERSION);
    }

    public boolean hasVersion() {
        return containsKey(VERSION);
    }

    // REST Documentation Examples

    public static final RestPullRequestMergeRequest MERGE_REQUEST_EXAMPLE = new RestPullRequestMergeRequest() { {
        put(CONTEXT, Collections.emptyMap());
        put(MESSAGE, "(Optional) A descriptive message for the merge commit");
        put(STRATEGY_ID, "(Optional) squash");
        put(VERSION, 1);
    } };
}
