package com.offcn.api.tk.app2;

import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.xmlbeans.impl.common.IdentityConstraint.IdState;
import org.json.simple.JSONArray;

import com.offcn.TestUnti.Log;
import com.offcn.TestUnti.MapUtil;
import com.offcn.TestUnti.MySqlUtil;
import com.offcn.TestUnti.RequestDataUtils;
import com.offcn.TestUnti.StringUtils;
import com.offcn.TestUnti.VerificationMethod;
import com.offcn.interfaces.API;
import com.offcn.process.TK;
import com.offcn.system.MyRequest;
import com.offcn.TestUnti.ListUtil;

import net.sf.json.JSONObject;




public class register extends TK implements API {
	public String parameter;//参数集合
	
	public String phone;//手机号
	public String passwd;//密码（md5加密，32位小写）
//	public String system;//手机系统信息
//	public String platform;//手机平台
//	public String version;//版本号
//	public String device_id;//设备号
	public String passcode;//密码明文

	@Override
	public void initialize(HashMap<String, Object> data) {
		if (!isProduct && data.get("CleanDB").toString().contains("Y")) {
			cleanUser_FromDB();//清除注册用户根据user_id
		}
	}

	@Override
	public HashMap<String, Object> handleInput(HashMap<String, Object> data) {
		// 获取parameter对应的内容
		parameter = MapUtil.getValue("parameter", data);
		phone = MapUtil.getParameter(parameter, "phone").trim();
		passwd = MapUtil.getParameter(parameter, "passwd").trim();
//		system = MapUtil.getParameter(parameter, "system").trim();
//		platform = MapUtil.getParameter(parameter, "platform").trim();
//		version = MapUtil.getParameter(parameter, "version").trim();
//		device_id = MapUtil.getParameter(parameter, "device_id").trim();
		passcode = MapUtil.getParameter(parameter, "passcode").trim();

		phone = MapUtil.killQuotes(phone, "\"");
		passwd = MapUtil.killQuotes(passwd, "\"");
//		system = MapUtil.killQuotes(system, "\"");
//		platform = MapUtil.killQuotes(platform, "\"");
//		version = MapUtil.killQuotes(version, "\"");
//		device_id = MapUtil.killQuotes(device_id, "\"");
		passcode = MapUtil.killQuotes(passcode, "\"");
		
		data.put("parameter", parameter);
		return data;
	}

	@Override
	public Response SendRequest(HashMap<String, Object> data, String Url,
			String Request) {

		Map<String, String>  headers=new HashMap<String, String>();
//		headers.put("Authorization", tk_token);
		
		Map<String, String>  jsonAsMap=new HashMap<String, String>();
		parameter = MapUtil.getValue("parameter", data);
		String tikuString= MapUtil.getParameter(parameter, "appid").trim();
		tikuString = MapUtil.killQuotes(tikuString, "\"");
		String signString= MapUtil.getParameter(parameter, "sign").trim();
		signString = MapUtil.killQuotes(signString, "\"");
		jsonAsMap.put("phone", phone);
		jsonAsMap.put("passwd", passwd);
//		jsonAsMap.put("system", system);
//		jsonAsMap.put("platform", platform);
//		jsonAsMap.put("version", version);
//		jsonAsMap.put("device_id", device_id);
		jsonAsMap.put("passcode", passcode);
		jsonAsMap.put("appid", tikuString);
		jsonAsMap.put("sign", signString);
		
		MyRequest myRequest=new MyRequest();
		myRequest.setUrl(Url);
		myRequest.setHeaders(headers);
		myRequest.setFormParameter(jsonAsMap);		
		myRequest.setRequest(Request);
		Response re = RequestDataUtils.RestAssuredApi(data, myRequest);
		return re;
	}

	@Override
	public String handleOutput(Response re, HashMap<String, Object> data) {
		JsonPath jp = re.body().jsonPath();
		boolean result = true;
		String failReason = "";

		String json = StringUtils.decodeUnicode(re.asString());

		if ((data.get("statusCode") != null)
				&& (!data.get("statusCode").toString()
						.equals(String.valueOf(re.getStatusCode())))) {
			result = result && false;
			failReason = failReason + "statusCode is expected "
					+ data.get("statusCode").toString() + " but actually "
					+ String.valueOf(re.getStatusCode()) + ". ";
		}

		if (json.length() != 0) {
			
			String msg=jp.getString("message");
			String code=jp.getString("retcode");
			
			
			if ((data.get("code") != null ) && (code != null) && (!code.equals(data.get("code").toString()))) {
				result = result && false;
				failReason = failReason + "code is expected "
						+ data.get("code").toString() + " but actually "
						+ jp.getString("code") + ".";
			}

			if ((data.get("msg") != null) && (msg != null) && (!msg.equals(data.get("msg").toString()))) {
				result = result && false;
				failReason = failReason + "msg is expected "
						+ data.get("msg").toString() + " but actually "
						+ jp.getString("msg") + ".";
			}
			
			if(data.get("custom") != null && json!=null){
				//如果自定义结果包含多个希望结果，也就是有逗号，那么就切割，包含比较。
				if(data.get("custom").toString().contains(",")){
					String [] strCustomStrings=data.get("custom").toString().split(",");
					for(int i=0;i<strCustomStrings.length;i++){
						if(!json.contains(strCustomStrings[i])){
							result = result && false;
							failReason = failReason + "custom is expected "
									+ data.get("custom").toString() + " but actually "
									+ strCustomStrings[i] + ".";
							break;
						}
					}
				}else{//如果只有一个希望结果，就直接包含比较。
					if(!json.contains(data.get("custom").toString())){
						result = result && false;
						failReason = failReason + "custom is expected "
								+ data.get("custom").toString() + " but actually "
								+ data.get("custom").toString() + ".";
					}
				}
			}
			
			if("注册成功".equals(msg)){
				String user_id=jp.getString("data.userinfo.user_id");
				user_id = MapUtil.killQuotes(user_id, "\"");
				user_idList.add(user_id);
				
				String dataPhone=jp.getString("data.userinfo.phone");
				dataPhone = MapUtil.killQuotes(dataPhone, "\"");
				
				String dataSystem=jp.getString("data.userinfo.system");
				dataSystem = MapUtil.killQuotes(dataSystem, "\"");
				
				String dataPlatform=jp.getString("data.userinfo.platform");
				dataPlatform = MapUtil.killQuotes(dataPlatform, "\"");
				
				String dataVersion=jp.getString("data.userinfo.version");
				dataVersion = MapUtil.killQuotes(dataVersion, "\"");
				
				String token=jp.getString("data.token");
				tk_token = MapUtil.killQuotes(token, "\"");//保存token
				
				//是否验证数据库
				if (!isProduct) {
					
					try {
						ResultSet rs_bankcard = this.selectFromDB(
								" t_user u ",
								" u.user_id = '" + user_id + "' ");
						rs_bankcard.last();
						
						if(rs_bankcard.getRow()>=1){
							phone = MapUtil.killQuotes(phone, "\"");
							phoneNeb=phone;
							
							
							parameter = MapUtil.getValue("parameter", data);
							
//							system = MapUtil.getParameter(parameter, "system");
//							system=VerificationMethod.VerificationField(system);
//							
//							platform = MapUtil.getParameter(parameter, "platform");
//							platform=VerificationMethod.VerificationField(platform);
//							
//							version = MapUtil.getParameter(parameter, "version");
//							version=VerificationMethod.VerificationField(version);
//							
//							device_id = MapUtil.getParameter(parameter, "device_id");
//							device_id=VerificationMethod.VerificationField(device_id);
							
							passwd = MapUtil.getParameter(parameter, "passwd");
							passwd=VerificationMethod.VerificationField(passwd);
							
//							String act_phone=rs_bankcard.getString("phone").equals("")?"null":rs_bankcard.getString("phone");
							String act_phone=rs_bankcard.getString("phone");
//							String act_system=rs_bankcard.getString("system").equals("")?"null":rs_bankcard.getString("system");
							String act_system=rs_bankcard.getString("system");
//							String act_platform=rs_bankcard.getString("platform").equals("")?"null":rs_bankcard.getString("platform");
							String act_platform=rs_bankcard.getString("platform");
//							String act_version=rs_bankcard.getString("version").equals("")?"null":rs_bankcard.getString("version");
							String act_version=rs_bankcard.getString("version");
//							String act_device_id=rs_bankcard.getString("device_id").equals("")?"null":rs_bankcard.getString("device_id");
							String act_device_id=rs_bankcard.getString("device_id");
//							String act_passwd=rs_bankcard.getString("passwd").equals("")?"null":rs_bankcard.getString("passwd");
							String act_passwd=rs_bankcard.getString("passwd");
							
							
//							System.out.println((dataPhone.equals(act_phone)));
//							System.out.println((dataPhone.equals(phone)));
							
							if(phone.length()<60){
								if(!(dataPhone.equals(act_phone) && (dataPhone.equals(phone)))){
									result = result && false;
									failReason = failReason + "t_user info phone is not in DB;";
								}
							}
							
//							if(system.length()<60){
//								if(!(dataSystem.equals(act_system) && (dataSystem.equals(system)))){
//									result = result && false;
//									failReason = failReason + "t_user info system is not in DB;";
//								}
//							}
//							
//							if(platform.length()<60){
//							if(!(dataPlatform.equals(act_platform) && (dataPlatform.equals(platform)))){
//								result = result && false;
//								failReason = failReason + "t_user info platform is not in DB;";
//							}
//							}
//							
//							if(version.length()<60){
//								if(!(dataVersion.equals(act_version) && (dataVersion.equals(version)))){
//									result = result && false;
//									failReason = failReason + "t_user info  version is not in DB;";
//								}
//							}
//							
//							if(device_id.length()<60){
//								if(!(act_device_id.equals(device_id))){
//									result = result && false;
//									failReason = failReason + "t_user info device_id is not in DB;";
//								}
//							}
							
							if(passwd.length()<60){
								if(!(act_passwd.equals(passwd))){
									result = result && false;
									failReason = failReason + "t_user info  passwd is not in DB;";
								}
							}
							
						}
						
					} catch (Exception e) {
						result = result && false;
						failReason = failReason + "cannot verify t_user info in DB;";
						e.printStackTrace();
					}
				}
				conn=ConnectionsMap.get("tk");
			}
		}
		if (result)
			return "Pass";
		else
			return "Fail:" + failReason;
	}

}
