package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.kohsuke.github.BranchProtection.RequiredStatusChecks;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

import static org.kohsuke.github.Previews.LOKI;

/**
 * A branch in a repository.
 * 
 * @author Yusuke Kokubo
 */
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", 
    "NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD"}, justification = "JSON API")
public class GHBranch {
    private GitHub root;
    private GHRepository owner;

    private String name;
    private Commit commit;

    public static class Commit {
        String sha;
        
        @SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
        String url;
    }

    public GitHub getRoot() {
        return root;
    }

    /**
     * Repository that this branch is in.
     */
    public GHRepository getOwner() {
        return owner;
    }

    public String getName() {
        return name;
    }

    /**
     * The commit that this branch currently points to.
     */
    public String getSHA1() {
        return commit.sha;
    }

    /**
     * Disables branch protection and allows anyone with push access to push changes.
     */
    @Preview @Deprecated
    public void disableProtection() throws IOException {
        BranchProtection bp = new BranchProtection();
        bp.enabled = false;
        setProtection(bp);
    }

    /**
     * Enables branch protection to control what commit statuses are required to push.
     *
     * @see GHCommitStatus#getContext()
     */
    @Preview @Deprecated
    public void enableProtection(EnforcementLevel level, Collection<String> contexts) throws IOException {
        BranchProtection bp = new BranchProtection();
        bp.enabled = true;
        bp.requiredStatusChecks = new RequiredStatusChecks();
        bp.requiredStatusChecks.enforcement_level = level;
        bp.requiredStatusChecks.contexts.addAll(contexts);
        setProtection(bp);
    }

    @Preview @Deprecated
    public void enableProtection(EnforcementLevel level, String... contexts) throws IOException {
        enableProtection(level, Arrays.asList(contexts));
    }

    private void setProtection(BranchProtection bp) throws IOException {
        new Requester(root).method("PATCH").withPreview(LOKI)._with("protection",bp).to(getApiRoute());
    }

    String getApiRoute() {
        return owner.getApiTailUrl("/branches/"+name);
    }
    
    @Override
    public String toString() {
        final String url = owner != null ? owner.getUrl().toString() : "unknown";
        return "Branch:" + name + " in " + url;
    }

    /*package*/ GHBranch wrap(GHRepository repo) {
        this.owner = repo;
        this.root = repo.root;
        return this;
    }
}

@@ -33,123 +33,47 @@
 ort 
-edu.umd.cs.findbugs.annotations.SuppressFBWarnings;%0Aimport org.kohsuke.github.BranchProtection.RequiredStatusChecks
+static org.kohsuke.github.Previews.LOKI
 ;%0A%0Ai
@@ -153,39 +153,32 @@
 ection;%0A%0Aimport 
-static 
 org.kohsuke.gith
@@ -180,29 +180,168 @@
 .github.
-Previews.LOKI
+BranchProtection.RequiredStatusChecks;%0A%0Aimport com.fasterxml.jackson.annotation.JsonProperty;%0A%0Aimport edu.umd.cs.findbugs.annotations.SuppressFBWarnings
 ;%0A%0A/**%0A 
@@ -706,16 +706,115 @@
 commit;%0A
+    @JsonProperty(%22protected%22)%0A    private boolean protection;%0A    private String protection_url;%0A%0A
 %0A    pub
@@ -1232,32 +1232,180 @@
 urn name;%0A    %7D%0A
+    %0A    public boolean isProtected() %7B%0A      return protection;%0A    %7D%0A%0A    public String getProtection_url() %7B%0A      return protection_url;%0A    %7D%0A%0A
 %0A    /**%0A     * 

add branch protection attributes