package com.atlassian.bitbucket.rest.repository;

import com.atlassian.bitbucket.repository.Branch;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.util.RestPage;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.function.Function;

import static com.atlassian.bitbucket.repository.StandardRefType.BRANCH;
import static com.atlassian.bitbucket.rest.util.RestDocUtils.pageOf;

@JsonSerialize
@JsonSurrogate(Branch.class)
public class RestBranch extends RestRef {

    public static final Function<Branch, RestBranch> REST_TRANSFORM = RestBranch::new;

    @SuppressWarnings("UnusedDeclaration")
    public RestBranch() {
    }

    public RestBranch(Branch branch) {
        this(branch.getId(), branch.getDisplayId(), branch.getLatestCommit(), branch.getIsDefault());
    }

    // Required for REST documentation
    private RestBranch(String id) {
        put(ID, id);
    }

    // Required for REST documentation
    private RestBranch(String id, String displayId, String latestCommit, boolean isDefault) {
        super(id, displayId, BRANCH, latestCommit);

        put("isDefault", isDefault);
    }

    // REST Documentation Examples

    public static final RestBranch REQUEST_EXAMPLE = new RestBranch("refs/heads/master");

    public static final RestBranch RESPONSE_EXAMPLE = new RestBranch("refs/heads/master", "master", "8d51122def5632836d1cb1026e879069e10a1e13", true);

    public static final RestPage<RestBranch> PAGE_EXAMPLE = pageOf(RESPONSE_EXAMPLE);
}
