package service.updateservice;

import static org.eclipse.egit.github.core.client.IGitHubConstants.CONTENT_TYPE_JSON;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_REPOS;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;

import org.eclipse.egit.github.core.IRepositoryIdProvider;
import org.eclipse.egit.github.core.client.GitHubRequest;
import org.eclipse.egit.github.core.client.PagedRequest;

import service.GitHubClientExtended;

public class UpdateService<T> {
	protected String apiSuffix;
	protected GitHubClientExtended client;
	protected String lastETag;
	protected Date lastCheckTime;
	
	public UpdateService(GitHubClientExtended client){
		this.client = client;
	}
	
	protected void updateLastETag(HttpURLConnection connection){
		lastETag = connection.getHeaderField("ETag");
	}
	
	protected String getFormattedDate(Date date){
		TimeZone tz = TimeZone.getTimeZone("UTC");
	    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
	    df.setTimeZone(tz);
	    String formatted = df.format(date);
	    return formatted;
	}
	
	private void updateLastCheckTime(HttpURLConnection connection) throws ParseException{
		String date = connection.getHeaderField("Date");
		lastCheckTime = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").parse(date);
	}
	
	protected HttpURLConnection createUpdatedConnection(GitHubRequest request) throws IOException{
		HttpURLConnection connection = client.createConnection(request);
		if(lastETag != null){
			connection.setRequestProperty("If-None-Match", lastETag);
		}
		return connection;
	}
	
	protected PagedRequest<T> createUpdatedRequest(IRepositoryIdProvider repoId){
		PagedRequest<T> request = new PagedRequest<T>();
		String path = SEGMENT_REPOS + "/" + repoId.generateId() + apiSuffix;
		request.setUri(path);
		request.setResponseContentType(CONTENT_TYPE_JSON);
		return request;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<T> getUpdatedItems(IRepositoryIdProvider repoId){
		try {
			
			GitHubRequest request = createUpdatedRequest(repoId);
			HttpURLConnection connection = createUpdatedConnection(request);
			int responseCode = connection.getResponseCode();
			System.out.println(responseCode);
			if(client.isError(responseCode)){
				return new ArrayList<T>();
			}
			updateLastETag(connection);
			updateLastCheckTime(connection);
			if(responseCode != GitHubClientExtended.NO_UPDATE_RESPONSE_CODE){
				return (ArrayList<T>)client.getBody(request, client.getStream(connection));
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ArrayList<T>();
	}
	
}

@@ -597,24 +597,140 @@
 ent.Page
-dRequest
+Iterator;%0Aimport org.eclipse.egit.github.core.client.PagedRequest;%0Aimport org.eclipse.egit.github.core.service.GitHubService
 ;%0A%0Aimpor
@@ -792,16 +792,37 @@
 vice%3CT%3E 
+extends GitHubService
 %7B%0A%09prote
@@ -2314,29 +2314,31 @@
 %0A%09%09%09%0A%09%09%09
-GitHub
+Paged
 Request
+%3CT%3E
  request
@@ -2370,16 +2370,91 @@
 epoId);%0A
+%09%09%09PageIterator%3CT%3E requestIterator = new PageIterator%3CT%3E(request, client);%0A
 %09%09%09HttpU
@@ -2811,16 +2811,18 @@
 _CODE)%7B%0A
+//
 %09%09%09%09retu
@@ -2893,16 +2893,66 @@
 tion));%0A
+%09%09%09%09return (ArrayList%3CT%3E)getAll(requestIterator);%0A
 %09%09%09%7D%0A%09%09%09

handle cases where response > 30