package com.itranswarp.wxapi.sample.robot;

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.itranswarp.wxapi.util.HttpUtil;
import com.itranswarp.wxapi.util.JsonUtil;
import com.itranswarp.wxapi.util.MapUtil;

@Component
public class Robot {

	static final String TULING_URL = "http://www.tuling123.com/openapi/api";

	@Value("${robot.app.id:}")
	String apiKey;

	public RobotResponse talk(String userId, String text) throws Exception {
		return talk(userId, text, null);
	}

	public RobotResponse talk(String userId, String text, String location) throws Exception {
		Map<String, String> params = MapUtil.createMap("key", apiKey, "userid", userId, "info", text);
		if (location != null && !location.isEmpty()) {
			params.put("loc", location);
		}
		String json = HttpUtil.httpPost(TULING_URL, HttpUtil.urlEncode(params), null);
		RobotResponse resp = JsonUtil.fromJson(RobotResponse.class, json);
		if (resp.code >= 40000 && resp.code <= 49999) {
			throw new RobotException(resp.code, resp.text);
		}
		return resp;
	}

	public static class RobotResponse {

		public static int CODE_TEXT = 100000;
		public static int CODE_LINK = 200000;
		public static int CODE_NEWS = 302000;
		public static int CODE_COOK = 308000;
		public static int CODE_SONG = 313000;
		public static int CODE_POEM = 314000;

		public int code;
		public String text;
		public String url;
		public Item[] list;
		public Func function;
	}

	public static class Item {
		public String article;
		public String source;
		public String icon;
		public String info;
		public String detailurl;
	}

	public static class Func {
		public String song;
		public String singer;
		public String author;
		public String name;
	}

}

@@ -49,24 +49,275 @@
 rt java.
-util.Map
+nio.charset.StandardCharsets;%0Aimport java.security.GeneralSecurityException;%0Aimport java.security.Key;%0Aimport java.util.Base64;%0Aimport java.util.Map;%0A%0Aimport javax.crypto.Cipher;%0Aimport javax.crypto.spec.IvParameterSpec;%0Aimport javax.crypto.spec.SecretKeySpec
 ;%0A%0Aimpor
@@ -416,24 +416,67 @@
 Component;%0A%0A
+import com.itranswarp.wxapi.util.HashUtil;%0A
 import com.i
@@ -700,16 +700,116 @@
 /api%22;%0A%0A
+%09static final Map%3CString, String%3E HEADERS = MapUtil.createMap(%22Content-Type%22, %22application/json%22);%0A%0A
 %09@Value(
@@ -825,11 +825,11 @@
 app.
-id:
+key
 %7D%22)%0A
@@ -845,16 +845,67 @@
 piKey;%0A%0A
+%09@Value(%22$%7Brobot.app.secret%7D%22)%0A%09String apiSecret;%0A%0A
 %09public 
@@ -1153,23 +1153,8 @@
 Map(
-%22key%22, apiKey, 
 %22use
@@ -1267,16 +1267,341 @@
 n);%0A%09%09%7D%0A
+%09%09long timestamp = System.currentTimeMillis();%0A%09%09String jsonData = JsonUtil.toJson(params);%0A%09%09String aesKey = HashUtil.md5(apiSecret + timestamp + apiKey);%0A%09%09String encryptedData = aesEncrypt(aesKey, jsonData);%0A%09%09Map%3CString, Object%3E postData = MapUtil.createMap(%22key%22, apiKey, %22timestamp%22, timestamp, %22data%22, encryptedData);%0A
 %09%09String
@@ -1642,40 +1642,42 @@
 RL, 
-Http
+Json
 Util.
-urlEncode(params), null
+toJson(postData), HEADERS
 );%0A%09
@@ -1864,24 +1864,516 @@
 n resp;%0A%09%7D%0A%0A
+%09String aesEncrypt(String aesKey, String data) throws GeneralSecurityException %7B%0A%09%09Key key = new SecretKeySpec(aesKey.getBytes(), %22AES%22);%0A%09%09IvParameterSpec iv = new IvParameterSpec(new byte%5B%5D %7B 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 %7D);%0A%09%09Cipher cipher = Cipher.getInstance(%22AES/CBC/PKCS5Padding%22);%0A%09%09cipher.init(Cipher.ENCRYPT_MODE, key, iv);%0A%09%09byte%5B%5D encryptData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));%0A%09%09return Base64.getEncoder().encodeToString(encryptData);%0A%09%7D%0A%0A
 %09public stat

add aes encrypt