package nl.tudelft.ewi.git.web;

import cucumber.api.PendingException;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import lombok.val;
import nl.tudelft.ewi.git.models.CommitModel;
import nl.tudelft.ewi.git.models.DetailedCommitModel;
import nl.tudelft.ewi.git.models.RepositoryModel;
import nl.tudelft.ewi.git.models.TagModel;
import nl.tudelft.ewi.git.web.api.CommitApi;
import nl.tudelft.ewi.git.web.api.RepositoriesApiImpl;
import nl.tudelft.ewi.git.web.api.RepositoryApiImpl;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.revwalk.RevCommit;

import javax.inject.Inject;
import javax.xml.soap.Detail;

/**
 * Created by Sayra Ranjha and Maaike Visser on 21-6-2017.
 */
public class AddTagStepDefinitions {

    public static final String DEFAULT_COMMIT_AUTHOR_NAME = "SayraMaaike";
    public static final String DEFAULT_COMMIT_AUTHOR_EMAIL = "MS@Test";

    @Inject
    private CloneStepDefinitions cloneStepDefinitions;
    @Inject
    private RepositoriesApiImpl repositoriesApi;

    @When("^I tag the commit$")
    public void iTagTheCommit() throws Throwable {


//        CommitApi commitApi = repositoriesApi.getRepository(cloneStepDefinitions.getRepositoryName()).getCommit(cloneStepDefinitions.getCreatedCommitId());
//        CommitModel commitModel = commitApi.get();

        String commitId = repositoriesApi.getRepository(cloneStepDefinitions.getRepositoryName())
                .getBranch("master")
                .getCommit()
                .get()
                .getCommit();

        CommitModel commitModel = repositoriesApi.getRepository(cloneStepDefinitions.getRepositoryName()).getCommit(commitId).get();

        TagModel tagModel = new TagModel();
        tagModel.setName("ourtag");
        tagModel.setCommit(commitModel);
        tagModel.setDescription("This is our tag.");

        repositoriesApi.getRepository(cloneStepDefinitions.getRepositoryName()).addTag(tagModel);
    }

    @Then("^A tag is added to the commit$")
    public void aTagIsAddedToTheCommit() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

}

@@ -127,24 +127,98 @@
 ava.en.When;
+%0A%0Aimport cucumber.runtime.java.guice.ScenarioScoped;%0Aimport lombok.Getter;
 %0Aimport lomb
@@ -646,16 +646,46 @@
 vCommit;
+%0Aimport org.hamcrest.Matchers;
 %0A%0Aimport
@@ -737,16 +737,261 @@
 etail;%0A%0A
+%0Aimport java.util.Collection;%0Aimport java.util.List;%0A%0Aimport static org.hamcrest.Matchers.contains;%0Aimport static org.hamcrest.Matchers.containsString;%0Aimport static org.hamcrest.Matchers.hasProperty;%0Aimport static org.junit.Assert.assertThat;%0A%0A
 /**%0A * C
@@ -1049,16 +1049,32 @@
 17.%0A */%0A
+@ScenarioScoped%0A
 public c
@@ -1380,16 +1380,60 @@
 esApi;%0A%0A
+    @Getter%0A    private TagModel tagModel;%0A%0A
     @Whe
@@ -1511,20 +1511,16 @@
 e %7B%0A
-%0A%0A//
         
 Comm
@@ -1519,27 +1519,23 @@
     
-CommitApi
+String
  commit
-Api
+Id
  = r
@@ -1552,214 +1552,25 @@
 sApi
-.getRepository(cloneStepDefinitions.getRepositoryName()).getCommit(cloneStepDefinitions.getCreatedCommitId());%0A//        CommitModel commitModel = commitApi.get();%0A%0A        String commitId = repositoriesApi
+%0A                
 .get
@@ -1783,32 +1783,49 @@
  repositoriesApi
+%0A                
 .getRepository(c
@@ -1864,16 +1864,33 @@
 yName())
+%0A                
 .getComm
@@ -1901,16 +1901,33 @@
 ommitId)
+%0A                
 .get();%0A
@@ -1935,25 +1935,16 @@
         
-TagModel 
 tagModel
@@ -2172,16 +2172,33 @@
 yName())
+%0A                
 .addTag(
@@ -2331,120 +2331,235 @@
     
-// Write code here that turns the phrase above into concrete actions%0A        throw new PendingException();
+Collection%3CTagModel%3E tags = repositoriesApi.getRepository(cloneStepDefinitions.getRepositoryName()).getTags();%0A%0A        assertThat(tags, contains(%0A                hasProperty(%22name%22, containsString(tagModel.getName()))));%0A
 %0A    %7D%0A
-%0A
 %7D%0A

Add cucumber step to check that a tag has been added