package org.jerrymouse.cc.site.web.spider;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HeaderElement;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;
import org.jerrymouse.cc.site.bean.Type;

import au.id.jericho.lib.html.Element;

public class WebResource {

	private Type type;
	private String HTTP = "http";
	private String url;
	private byte[] bytes;
	private String name;

	public WebResource(String url) {
		if (url.startsWith(HTTP)) {
			FlvSplider flvSplider = new FlvcdSpider(url);
			if (flvSplider.isSupport()) {
				name = flvSplider.getName();
				type = new Type("flv");
				url = flvSplider.getUrl();
			}
			this.url = url;
		}
	}

	public String getFullName() {
		return type.addSuffix(name);
	}

	public Type getType() {
		if (type == null) {
			connect();
			if (type == null) {
				guessFromUrl();
			}
		}
		return type;
	}

	private void guessFromUrl() {
		try {
			String file = new URL(this.url).getFile();
			file = URLDecoder.decode(file, "utf-8");
			file = file.substring(file.lastIndexOf('/') + 1);
			int i = file.indexOf('?');
			if (i != -1)
				file = file.substring(0, i);
			type = new Type();
			name = type.removeSuffix(file);
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getName() {
		if (name == null) {
			connect();
			if (name == null) {
				guessFromUrl();
			}
		}
		return name;
	}

	private void connect() {
		try {
			GetMethod getMethod = new GetMethod(url);
			HttpClientParams params = new HttpClientParams(null);
			params.setParameter(HttpMethodParams.USER_AGENT, "Mozilla/5.0");
			int s = new HttpClient(params).executeMethod(getMethod);
			if (s == HttpStatus.SC_OK) {
				bytes = IOUtils
						.toByteArray(getMethod.getResponseBodyAsStream());
				if (name == null || type == null) {
					Header[] httpHeaders = getMethod
							.getResponseHeaders("Content-Disposition");
					if (httpHeaders != null && httpHeaders.length > 1) {
						HeaderElement[] le = httpHeaders[0].getElements();
						String filename = le[0].getParameterByName("filename")
								.toString();
						Type t = new Type();
						String n = t.removeSuffix(filename);
						if (name == null)
							name = n;
						if (type == null)
							type = t;
					}
				}
			} else {
				URL u = new URL(url);
				if (bytes == null) {
					bytes = IOUtils.toByteArray(u.openStream());
				}
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public byte[] getBytes() {
		if (bytes == null)
			connect();
		return bytes;
	}

}
