package com.mooo.aimmac23.hub;

import java.net.URL;
import java.util.logging.Logger;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.openqa.grid.internal.ExternalSessionKey;
import org.openqa.selenium.remote.internal.HttpClientFactory;

import com.mooo.aimmac23.hub.videostorage.BasicWebDAVStore;
import com.mooo.aimmac23.hub.videostorage.IVideoStore;
import com.mooo.aimmac23.hub.videostorage.StoredVideoDownloadContext;

public class HubVideoRegistry {
	
	private static final Logger log = Logger.getLogger(HubVideoRegistry.class.getName());

	private static IVideoStore videoStore;
	
	static {
		try {
			videoStore = new BasicWebDAVStore();
		} catch (Exception e) {
			// throw a nasty error to hopefully prevent the Hub from trying to continue without this 
			throw new Error("Could not initialize video store due to exception", e);
		}
	}

	public static void copyVideoToHub(ExternalSessionKey key, String pathKey, URL remoteHost) {
		String serviceUrl = remoteHost + "/extra/VideoRecordingControlServlet";
		
		HttpHost remote = new HttpHost(remoteHost.getHost(),
				remoteHost.getPort());
        HttpClientFactory httpClientFactory = new HttpClientFactory();
        HttpClient client = httpClientFactory.getHttpClient();
        
        HttpGet r = new HttpGet(serviceUrl + "?command=download&filekey=" + pathKey);
		
        try {
			HttpResponse response = client.execute(remote, r);
			if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				log.warning("Could not download video: " + EntityUtils.toString(response.getEntity()));
				return;
			}
			// XXX: Should check mime-type, just in case
			videoStore.storeVideo(response.getEntity().getContent(), "video/webm", key.toString());
        }
		catch(Exception e) {
			log.warning("Could not download video, exception caught: " + e.getMessage());
			e.printStackTrace();
		}
        finally {
        	r.releaseConnection();
        }
	}
	
	public static StoredVideoDownloadContext getVideoForSession(ExternalSessionKey key) throws Exception {
		
		return videoStore.retrieveVideo(key.toString());
	}
	

}

@@ -472,27 +472,22 @@
 storage.
-BasicWebDAV
+IVideo
 Store;%0Ai
@@ -519,38 +519,45 @@
 ub.videostorage.
-IVideo
+LocalTempFile
 Store;%0Aimport co
@@ -618,16 +618,47 @@
 ntext;%0A%0A
+@SuppressWarnings(%22unchecked%22)%0A
 public c
@@ -838,44 +838,355 @@
 %0A%09%09%09
-videoStore = new BasicWebDAVStore();
+Class%3C? extends IVideoStore%3E storageClass = LocalTempFileStore.class;%0A%09%09%09String classname = System.getProperty(%22video.storage%22);%0A%09%09%09if(classname != null) %7B%0A%09%09%09%09storageClass = (Class%3C? extends IVideoStore%3E) Class.forName(classname);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09videoStore = storageClass.newInstance();%0A%09%09%09log.info(%22Using %22 + storageClass + %22 to store videos%22);%0A%09%09%09
 %0A%09%09%7D

Make the video storage we use configurable