package info.igreque.keepmecontributing.github;

import info.igreque.keepmecontributing.github.Contributions;
import info.igreque.keepmecontributing.github.ContributionsCalendarException;

import java.lang.Exception;

import java.util.HashMap;
import java.io.InputStream;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.HttpClient;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Github API client.
 * Get contribution calendar from github.com/users/:username/contributions
 */
public class Github {
  private String username;
  private HttpClient httpClient;

  public Github(String username, HttpClient httpClient){
    this.username = username;
    this.httpClient = httpClient;
  }

  public HashMap<String, Contributions> fetchContributionsCalendar(){
    return null;
  }

  public String fetchContributionsCalendarJson() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(fetchContributionsCalendar());
  }

  public String fetchContributionsCalendarSvg(){
    return null;
  }

  public HashMap<String, Contributions> contributionsCalendarFromSvg(InputStream input)
    throws ContributionsCalendarException
  {
    try {
      Document doc =
        DocumentBuilderFactory
        .newInstance()
        .newDocumentBuilder()
        .parse(input);

      NodeList days =
        (NodeList) XPathFactory
        .newInstance()
        .newXPath()
        .evaluate("//rect[@class='day']", doc, XPathConstants.NODESET);

      HashMap<String, Contributions> result = new HashMap();
      for(int i = 0; i < days.getLength(); ++i){
        NamedNodeMap attributes = days.item(i).getAttributes();
        String dayString = attributes.getNamedItem("data-date").getNodeValue();
        int count = Integer.parseInt(attributes.getNamedItem("data-count").getNodeValue(), 10);
        Contributions contributions = new Contributions(count);

        result.put(dayString, contributions);
      }
      return result;

    } catch (Exception e) {
      e.printStackTrace();
      String message = "An error occurred while parsing a response from github.com: " + e.toString();
      throw new ContributionsCalendarException(message, e);
    }
  }

}

@@ -46,148 +46,8 @@
 b;%0A%0A
-import info.igreque.keepmecontributing.github.Contributions;%0Aimport info.igreque.keepmecontributing.github.ContributionsCalendarException;%0A%0A
 impo
@@ -62,32 +62,32 @@
 lang.Exception;%0A
+
 %0Aimport java.uti
@@ -271,38 +271,8 @@
 ry;%0A
-import javax.xml.xpath.XPath;%0A
 impo
@@ -306,16 +306,16 @@
 stants;%0A
+
 import j
@@ -346,63 +346,8 @@
 ory;
-%0Aimport javax.xml.parsers.ParserConfigurationException;
 %0A%0Aim
@@ -1677,16 +1677,18 @@
  HashMap
+%3C%3E
 ();%0A    

fix warnings