package com.offcn.api.tk.app3;

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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



/**
 * 
 * @Description: 50. 图片提取文字部分
 * @author: mr    
 * @date: 2019年7月31日    
 * @version V1.0
 */

public class getImageInfo extends TK implements API {
	public String parameter;//参数集合

	public String image;//base64位图片码
	public String record_id;//试卷使用记录id
	public String question_id;//试题id
	public String paper_id;//试卷id
	public String user_id;//用户id
	public String channel;//做题平台来源（1：题库；2：NWN）
	

	@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);
		
		image = MapUtil.getParameter(parameter, "image").trim();
		record_id = MapUtil.getParameter(parameter, "record_id").trim();
		question_id = MapUtil.getParameter(parameter, "question_id").trim();
		paper_id = MapUtil.getParameter(parameter, "paper_id").trim();
		user_id = MapUtil.getParameter(parameter, "user_id").trim();
		channel = MapUtil.getParameter(parameter, "channel").trim();
		
		if ((!image.equals("")) && image.equals("code")) {
			image = "iVBORw0KGgoAAAANSUhEUgAAAKoAAABVCAIAAABW0OgZAAALBklEQVR4Ae2dX0wbRxrAJ4D5Z3I2ZKG2KUbUJLIJJk19tJDKTuVGMg/mgT44aiESfgChoEouFYdkIkVIF0vEaoNUgRA8NJJBbf1weSAP8UOsNlwOk8jXnMnFbgKNTILhggM4YENwSG7W/1gvNpFTQyrP7AM7M/vtN/t9v/m+mVnY5MCrV68APlD1QBqqhmO7SQ9g/EiPA4wf40faA0gbj6Mf40faA0gbj6Mf40faA0gbj6Mf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B9qGfyMrLatJFPXt9xfCL7LTs+ElnANiunKPGuPhSEp3Mf55h4Lsgg/e+QCyp37cH+wMfuI/ah+FX/4ubSwlpXus5LUe2AXHa+99rcAB/B+4v9ZHKSwQI/mnsLXYNJoHMH6aQ9CqYvxo8aZZi/HTHIJWFeNHizfNWoyf5hC0qhg/Wrxp1mL8NIegVcX40eJNsxbjpzkErSrGjxZvmrUYP80haFUxfrR406zF+GkOQauK8aPFm2Ytxk9zCFpVjB8t3jRrMX6aQ9CqYvxo8aZZi/HTHIJWFeNHizfN2v8D7ynTYNgofXMAAAAASUVORK5CYII=";
			parameter = parameter.replace("\"image\":code", "\"image\":\""+ image + "\"");
		}
		if ((!record_id.equals("")) && record_id.equals("code")) {
			record_id= L_record_id;
			parameter = parameter.replace("\"record_id\":code", "\"v\":\""+ record_id + "\"");
		}
		if ((!question_id.equals("")) && question_id.equals("code")) {
			question_id = "106047";
			parameter = parameter.replace("\"question_id\":code", "\"question_id\":\""+ question_id + "\"");
		}
		if ((!paper_id.equals("")) && paper_id.equals("code")) {
			paper_id = "4321";
			parameter = parameter.replace("\"paper_id\":code", "\"paper_id\":\""+ paper_id + "\"");
		}
		if ((!user_id.equals("")) && user_id.equals("code")) {
			user_id= ListUtil.getListValue(user_idList,0);
			parameter = parameter.replace("\"user_id\":code", "\"user_id\":\""+ user_id + "\"");
		}	

		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, "\"");
		
		jsonAsMap.put("image", image);
		jsonAsMap.put("record_id", record_id);
		jsonAsMap.put("question_id", question_id);
		jsonAsMap.put("paper_id", paper_id);
		jsonAsMap.put("user_id", user_id);
		jsonAsMap.put("channel", channel);
		jsonAsMap.put("appid", tikuString);
		
		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("success".equals(msg)){
//					L_record_id=jp.getString("data.recordId");	

				conn=ConnectionsMap.get("tk");
			}
		
		if (result)
			return "Pass";
		else
			return "Fail:" + failReason;
		}
		return json;
	}

}
