package zmo.technology.lite.httpcli;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ZeroHttpClient extends ZeroHttpRequest {

	protected String requestURL;
	protected String encode;
	protected Map<String, String> headers;
	protected Map<String, Object> quaryParams;
	protected Map<String, Object> params;
	protected byte[] response;

	public static ZeroHttpClient request(String url) {
		return new ZeroHttpClient().url(url);
	}

	public ZeroHttpClient() {
		super(5000, 5000);
		encode = "UTF-8";
		headers = new HashMap<>();
		quaryParams = new HashMap<String, Object>();
		params = new HashMap<String, Object>();
	}

	@Override
	public ZeroHttpClient setConnectTimeout(int connectTimeout) {
		super.setConnectTimeout(connectTimeout);
		return this;
	}

	@Override
	public ZeroHttpClient setReadTimeout(int readTimeout) {
		super.setReadTimeout(readTimeout);
		return this;
	}

	public ZeroHttpClient url(String requestURL) {
		this.requestURL = requestURL;
		return this;
	}

	public ZeroHttpClient encode(String encode) {
		this.encode = encode;
		return this;
	}

	public void setHeaders(Map<String, String> headers) {
		this.headers = headers;
	}

	public Map<String, String> getHeaders() {
		return headers;
	}

	public ZeroHttpClient addHeader(String headerName, String headerValue) {
		headers.put(headerName, headerValue);
		return this;
	}

	public ZeroHttpClient quarys(Map<String, Object> quaryParams) {
		this.quaryParams = quaryParams;
		return this;
	}

	public ZeroHttpClient params(Map<String, Object> params) {
		this.params = params;
		return this;
	}

	public ZeroHttpClient quary(String key, Object data) {
		this.quaryParams.put(key, data);
		return this;
	}

	public ZeroHttpClient param(String key, Object data) {
		this.params.put(key, data);
		return this;
	}

	public ZeroHttpClient post() throws Exception {
		String quary = requestParams(quaryParams, true);
		quary = quary.length() > 0 ? "?" + quary : quary;
		response = super.post(requestURL + quary, headers, params, encode, true);
		return this;
	}

	public ZeroHttpClient get(boolean urlencode) throws Exception {
		response = super.get(requestURL, headers, params, encode, urlencode);
		return this;
	}

	public ZeroHttpClient get() throws Exception {
		return get(true);
	}

	public ZeroHttpClient formDataGet() throws Exception {
		String requestParam = requestParams(params, false);
		response = send("GET", new URL(requestURL), headers, requestParam.getBytes(), "UTF-8");
		return this;
	}

	public String result() {
		return new String(response);
	}

	public JSONObject json() {
		return JSONObject.parseObject(result());
	}

	public JSONArray jsonArray() {
		return JSONArray.parseArray(result());
	}

	public <T> T jsonObject(Class<T> c) {
		return JSONObject.parseObject(response, c);
	}

	public ZeroHttpClient jsonPost(String json) throws Exception {
		headers.put("Content-Type", "application/json");
		response = send("POST", new URL(requestURL), headers, json.getBytes(), encode);
		return this;
	}

	public ZeroHttpClient jsonPostObject(Object json) throws Exception {
		jsonPost(JSONObject.toJSONString(json));
		return this;
	}

	public ZeroHttpClient jsonPost() throws Exception {
		jsonPostObject(params);
		return this;
	}

	public ZeroHttpClient jsonPut(String json) throws Exception {
		headers.put("Content-Type", "application/json");
		response = send("PUT", new URL(requestURL), headers, json.getBytes(), encode);
		return this;
	}

	public ZeroHttpClient jsonPutObject(Object json) throws Exception {
		jsonPut(JSONObject.toJSONString(json));
		return this;
	}

	public ZeroHttpClient jsonPut() throws Exception {
		jsonPutObject(params);
		return this;
	}

	public byte[] response() {
		return response;
	}

	public ZeroHttpClient jsonDelete(String json) throws Exception {
		headers.put("Content-Type", "application/json");
		response = send("DELETE", new URL(requestURL), headers, json.getBytes(), encode);
		return this;
	}

	public ZeroHttpClient jsonDeleteObject(Object json) throws Exception {
		jsonDelete(JSONObject.toJSONString(json));
		return this;
	}
}
