package org.jerrymouse.cc.site.view.redirecter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jerrymouse.cc.site.bean.Resource;
import org.jerrymouse.cc.site.bean.Type;
import org.jerrymouse.cc.site.web.upload.Upload;
import org.jerrymouse.cc.site.web.upload.UploadToGoogle;

/**
 * has important problem
 * 
 * @author yankai
 * 
 */
public class ZohoRedirecter implements Redirecter {
	private static String apiKey = "ce45d4e81ce5c42f70284811025a9c0d";

	Log log = LogFactory.getLog(ZohoRedirecter.class);

	@SuppressWarnings("unused")
	private void addFHeaders(HttpMethodBase httpMethod, List<Header> headers) {
		for (Header header : headers) {
			httpMethod.addRequestHeader(header);
		}
	}

	@SuppressWarnings("unused")
	private List<Header> firefoxHeaders() {
		List<Header> headers = new ArrayList<Header>();
		headers
				.add(new Header(
						"User-Agent",
						"Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB5"));
		headers
				.add(new Header("Accept",
						"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
		headers.add(new Header("Accept-Language", "zh-cn,zh;q=0.5"));
		headers.add(new Header("Accept-Encoding", "gzip,deflate"));
		headers.add(new Header("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"));
		headers.add(new Header("Keep-Alive", "300"));
		headers.add(new Header("Connection", "keep-alive"));
		headers.add(new Header("Cache-Control", "max-age=0"));
		return headers;
	}

	public String getName() {
		return "zoho";
	}

	public List<Type> support() {
		String[] s = { "doc", "docx", "xls", "xlsx", "ppt", "pptx", "pps",
				"odt", "ods", "odp", "sxw", "sxc", "sxi", "pdf", "rtf", "csv" };
		return Type.createTypes(s);
	}

	public URI view(Resource resource) {
		Upload upload = new UploadToGoogle();
		String url = upload.putResource(resource);
		// URI url = view(resource, false);
		return zohoviewUrl(url);
	}

	/**
	 * un complet
	 * 
	 * @param apiKey
	 * @param resource
	 * @param redirect
	 * @return
	 */
	@SuppressWarnings("unused")
	private URI view(Resource resource, Boolean redirect) {
		try {
			PostMethod postMethod = new PostMethod(
					"http://viewer.zoho.com/api/view.do");

			StringPart stringPart = new StringPart("apiKey", apiKey);
			stringPart.setTransferEncoding(null);
			ByteArrayPartSource file = new ByteArrayPartSource(resource
					.getOrgType().addSuffix(resource.getFilename()), resource
					.getOrgBytes());
			FilePart filePart = new FilePart("file", file);
			filePart.setContentType(resource.getOrgType().getMime());
			filePart.setCharSet(null);
			filePart.setTransferEncoding(null);
			Part[] parts = { stringPart, filePart };
			postMethod.setRequestEntity(new MultipartRequestEntity(parts,
					postMethod.getParams()));
			int status = new HttpClient().executeMethod(postMethod);
			log.debug(status);
			log.debug(Arrays.toString(postMethod.getRequestHeaders()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	private URI zohoviewUrl(String url) {
		URI uri = null;
		try {
			uri = new URI("http://viewer.zoho.com/api/view.do?url="
					+ URLEncoder.encode(url, "utf-8") + "&apikey=" + apiKey);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return uri;
	}
}
