package com.atlassian.bitbucket.test;

import com.atlassian.bitbucket.rest.task.RestTask;
import com.atlassian.bitbucket.task.TaskAnchorType;
import com.google.common.collect.ImmutableMap;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.response.Response;

import static javax.ws.rs.core.Response.Status.*;

public class TaskRestTestHelper {

    public static Response createTask(String user, String password, String text, long commentId) {
        return RestAssured
            .given()
                .auth().preemptive().basic(user, password)
                .body(ImmutableMap.of(
                        RestTask.ANCHOR,
                            ImmutableMap.of(RestTask.ID, commentId,
                                            RestTask.ANCHOR_TYPE, TaskAnchorType.COMMENT),
                        RestTask.TEXT, text))
                .contentType(ContentType.JSON)
            .when()
                .post(TaskTestHelper.getCreateUrl())
            .then()
                .statusCode(CREATED.getStatusCode())
            .extract().response();
    }

    public static void delete(String user, String password, long id) {
        RestAssured
            .given()
                .auth().preemptive().basic(user, password)
            .when()
                .delete(TaskTestHelper.getResourceUrl(id))
            .then()
                .statusCode(NO_CONTENT.getStatusCode());
    }

    public static void updateTask(long id, String text, String state, String user, String password) {
        RestAssured
            .given()
                .auth().preemptive().basic(user, password)
                .body(ImmutableMap.of(
                        RestTask.ID, id,
                        RestTask.STATE, state,
                        RestTask.TEXT, text))
                .contentType(ContentType.JSON)
            .when()
                .put(TaskTestHelper.getResourceUrl(id))
            .then()
                .statusCode(OK.getStatusCode());
    }
}
