package com.atlassian.bitbucket.test;

import com.atlassian.bitbucket.rest.user.RestPerson;
import com.atlassian.bitbucket.rest.task.RestTask;
import com.atlassian.bitbucket.task.TaskState;
import com.jayway.restassured.response.Response;

public class TaskTestHelper {

    private final Long id;
    private final Long commentId;
    private final Long pullRequestId;
    private final Integer repositoryId;
    private final String text;
    private final String createdDate;
    private final String author;
    private final String state;
    private final String user;
    private final String password;

    public TaskTestHelper(Builder builder) {
        this.author = builder.author;
        this.commentId = builder.commentId;
        this.createdDate = builder.createdDate;
        this.id = builder.id;
        this.password = builder.password;
        this.pullRequestId = builder.pullRequestId;
        this.repositoryId = builder.repositoryId;
        this.state = builder.state;
        this.text = builder.text;
        this.user = builder.user;
    }

    public void delete() {
        TaskRestTestHelper.delete(user, password, id);
    }

    public void deleteSafe() {
        try {
            TaskRestTestHelper.delete(user, password, id);
        } catch (Throwable t) {
            // ignore
        }
    }

    public void resolve() {
        TaskRestTestHelper.updateTask(id, text, TaskState.RESOLVED.name(), user, password);
    }

    public Long getId() {
        return id;
    }

    public Long getCommentId() {
        return commentId;
    }

    public Long getPullRequestId() {
        return pullRequestId;
    }

    public Integer getRepositoryId() {
        return repositoryId;
    }

    public String getText() {
        return text;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public String getAuthor() {
        return author;
    }

    public String getState() {
        return state;
    }

    public String getResourceUrl() {
        return getResourceUrl(id);
    }

    @Override
    public String toString() {
        return "{id: " + id + ", commentId:" + commentId + ", text: " + text + "}";
    }

    public static String getCreateUrl() {
        return DefaultFuncTestData.getRestURL() + "/tasks";
    }

    public static String getResourceUrl(long id) {
        return DefaultFuncTestData.getRestURL() + "/tasks/" + id;
    }

    public static class Builder {

        private Long id = null;
        private String user = null;
        private String password = null;
        private String text = null;
        private String projectName = null;
        private String repoSlug = null;
        private Long commentId = null;
        private Integer repositoryId = null;
        private Long pullRequestId = null;
        public String state = null;
        public String createdDate = null;
        public String author = null;

        public Builder(TaskTestHelper taskTestHelper) {
            this.id = taskTestHelper.id;
            this.user = taskTestHelper.user;
            this.password = taskTestHelper.password;
            this.text = taskTestHelper.text;
            this.repositoryId = taskTestHelper.repositoryId;
            this.commentId = taskTestHelper.commentId;
            this.pullRequestId = taskTestHelper.pullRequestId;
            this.state = taskTestHelper.state;
            this.createdDate = taskTestHelper.createdDate;
            this.author = taskTestHelper.author;
        }

        public Builder(String user, String password, String text) {

            this.user = user;
            this.password = password;
            this.text = text;
        }

        public TaskTestHelper create() {
            if (repositoryId == null) {
                repositoryId = RepositoryTestHelper.getRepository(projectName, repoSlug).jsonPath().getInt("id");
            }
            Response response = TaskRestTestHelper.createTask(user, password, text, commentId);

            id = response.body().jsonPath().getLong(RestTask.ID);
            text = response.body().jsonPath().getString(RestTask.TEXT);
            state = response.body().jsonPath().getString(RestTask.STATE);
            author = response.body().jsonPath().getString(RestTask.AUTHOR + '.' + RestPerson.NAME);
            createdDate = response.body().jsonPath().getString(RestTask.CREATED_DATE);

            return new TaskTestHelper(this);
        }

        public Builder projectName(String projectName) {
            this.projectName = projectName;

            return this;
        }

        public Builder repoSlug(String repoSlug) {
            this.repoSlug = repoSlug;

            return this;
        }

        public Builder pullRequestId(Long pullRequestId) {
            this.pullRequestId = pullRequestId;

            return this;
        }

        public Builder commentId(Long commentId) {
            this.commentId = commentId;

            return this;
        }

        public Builder state(String state) {
            this.state = state;

            return this;
        }
    }
}
