package pl.matsuo.gitlab.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import static org.springframework.http.HttpStatus.*;
import static org.springframework.web.bind.annotation.RequestMethod.*;


/**
 * Created by marek on 04.07.15.
 */
@Controller
@RequestMapping("/gl")
public class GitlabProxyController {


  @Value("#{environment.PRIVATE_TOKEN}")
  private String gitlabPrivateToken;
  @Value("#{environment.GITLAB_SERVER}")
  private String gitlabServer;


  @RequestMapping(value = "/projects/{user}/{project}/issues", method = GET)
  @ResponseStatus(OK)
  public @ResponseBody
  String projectIssues(@PathVariable("user") String user,
                       @PathVariable("project") String project,
                       @RequestParam(value = "labels", required = false) String labels,
                       @RequestParam(value = "status", required = false) String status,
                       @RequestParam(value = "milestone", required = false) String milestone) {
    return sendGet(gitlabServer + "/api/v3/projects/" + user + "%2F" + project + "/issues?"
        + (labels != null ? "labels=" + labels + "&" : "")
        + (status != null ? "status=" + status + "&" : "")
        + (milestone != null ? "milestone=" + milestone + "&" : ""));
  }



  @RequestMapping(value = "/projects/{user}/{project}/milestones", method = GET)
  @ResponseStatus(OK)
  public @ResponseBody
  String projectMilestones(@PathVariable("user") String user,
		                       @PathVariable("project") String project) {
    return sendGet(gitlabServer + "/api/v3/projects/" + user + "%2F" + project + "/milestones");
  }


  @RequestMapping(value = "/projects/{idProject}/issues/{idIssue}/notes", method = GET)
  @ResponseStatus(OK)
  public @ResponseBody
  String projectNotes(@PathVariable("idProject") Integer idProject,
                      @PathVariable("idIssue") Integer idIssue) {
    return sendGet(gitlabServer + "/api/v3/projects/" + idProject + "/issues/" + idIssue + "/notes");
  }


  // HTTP GET request
  private String sendGet(String url) {
    try {
      URL obj = new URL(url);
      HttpURLConnection con = (HttpURLConnection) obj.openConnection();

      // optional default is GET
      con.setRequestMethod("GET");

      // set user agent
      con.setRequestProperty("User-Agent", "kosher/1.0");
      con.setRequestProperty("PRIVATE-TOKEN", gitlabPrivateToken);

      // FIXME: response codes from controller based on this response code
      int responseCode = con.getResponseCode();
      System.out.println("Response code: " + responseCode + " for url " + url);

      BufferedReader in = new BufferedReader(
          new InputStreamReader(con.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();

      while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
      }
      in.close();

      return response.toString();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}


@@ -2502,16 +2502,1240 @@
 ;%0A  %7D%0A%0A%0A
+  @RequestMapping(value = %22/projects/%7Buser%7D/%7Bproject%7D/repository/tree%22, method = GET)%0A  @ResponseStatus(OK)%0A  public @ResponseBody%0A  String listFiles(@PathVariable(%22user%22) String user,%0A                       @PathVariable(%22project%22) String project,%0A                       @RequestParam(value = %22path%22) String path,%0A                       @RequestParam(value = %22ref_name%22) String ref_name) %7B%0A    return sendGet(gitlabServer + %22/api/v3/projects/%22 + user + %22%252F%22 + project + %22/repository/tree?%22%0A        + (path != null ? %22path=%22 + path + %22&%22 : %22%22)%0A        + (ref_name != null ? %22ref_name=%22 + ref_name + %22&%22 : %22%22));%0A  %7D%0A%0A%0A  @RequestMapping(value = %22/projects/%7Buser%7D/%7Bproject%7D/repository/files%22, method = GET)%0A  @ResponseStatus(OK)%0A  public @ResponseBody%0A  String getFile(@PathVariable(%22user%22) String user,%0A                   @PathVariable(%22project%22) String project,%0A                   @RequestParam(value = %22file_path%22) String file_path,%0A                   @RequestParam(value = %22ref%22) String ref) %7B%0A    return sendGet(gitlabServer + %22/api/v3/projects/%22 + user + %22%252F%22 + project + %22/repository/files?%22%0A        + (file_path != null ? %22file_path=%22 + file_path + %22&%22 : %22%22)%0A        + (ref != null ? %22ref=%22 + ref + %22&%22 : %22%22));%0A  %7D%0A%0A%0A
   // HTT

Add api for files