package com.atlassian.bitbucket.rest.repository;

import com.atlassian.bitbucket.rest.RestMapEntity;
import com.atlassian.bitbucket.rest.pull.RestPullRequestMergeConfig;

import javax.annotation.Nullable;

public class RestRepositoryPullRequestSettings extends RestMapEntity {

    private static final String MERGE_CONFIG = "mergeConfig";
    private static final String REQUIRED_ALL_APPROVERS = "requiredAllApprovers";
    private static final String REQUIRED_ALL_TASKS_COMPLETE = "requiredAllTasksComplete";
    private static final String REQUIRED_APPROVERS = "requiredApprovers";
    private static final String REQUIRED_SUCCESSFUL_BUILDS = "requiredSuccessfulBuilds";

    @SuppressWarnings("unused") //Required by Jersey
    public RestRepositoryPullRequestSettings() {
    }

    // Required for REST documentation
    private RestRepositoryPullRequestSettings(boolean requiredAllApprovers, boolean requiredAllTasksComplete,
                                              int requiredApprovers, int requiredSuccessfulBuilds,
                                              RestPullRequestMergeConfig mergeConfig) {
        put(MERGE_CONFIG, mergeConfig);
        put(REQUIRED_ALL_APPROVERS, requiredAllApprovers);
        put(REQUIRED_ALL_TASKS_COMPLETE, requiredAllTasksComplete);
        put(REQUIRED_APPROVERS, requiredApprovers);
        put(REQUIRED_SUCCESSFUL_BUILDS, requiredSuccessfulBuilds);
    }

    @Nullable
    public RestPullRequestMergeConfig getMergeConfig() {
        return RestPullRequestMergeConfig.valueOf(get(MERGE_CONFIG));
    }

    public boolean getRequiredAllApprovers() {
        return getBoolProperty(REQUIRED_ALL_APPROVERS);
    }

    public boolean getRequiredAllTasksComplete() {
        return getBoolProperty(REQUIRED_ALL_TASKS_COMPLETE);
    }

    public int getRequiredApprovers() {
        return getIntProperty(REQUIRED_APPROVERS);
    }

    public int getRequiredSuccessfulBuilds() {
        return getIntProperty(REQUIRED_SUCCESSFUL_BUILDS);
    }

    public static final RestRepositoryPullRequestSettings REQUEST_EXAMPLE =
            new RestRepositoryPullRequestSettings(false, false, 2, 0, RestPullRequestMergeConfig.REQUEST_EXAMPLE);

    public static final RestRepositoryPullRequestSettings RESPONSE_EXAMPLE =
            new RestRepositoryPullRequestSettings(false, false, 2, 0, RestPullRequestMergeConfig.RESPONSE_EXAMPLE);
}
