package com.offcn.api.nwn.ht;

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.Map;
import java.util.Random;

import org.json.simple.JSONArray;
import org.testng.reporters.Files;

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

import net.sf.json.JSONObject;



/**
 * 148 .直播间作业--文档上传
 * 
 * @author wff
 *
 */
public class uploadFile_zhibo extends NWN implements API {
	public String parameter;//参数集合
	
	public String admin_card_no;//教务当前登录用户的 mis账号（如：zwq50680）
	public String admin_sso_id;//教务当前登录用户的sso_id
	public String store_id;//教务当前的登录店铺id
	public String token;// 教务token
	public String file;//文档名称
	public String file_type;//上传文件地址    
	
	public String ssoId;//

	@Override
	public void initialize(HashMap<String, Object> data) {
		try {
			String sql ="SELECT sso_id FROM iip.n_user WHERE phone = '15201350979'";
			ResultSet rs_bankcard = this.sqlFromDB(sql);
			rs_bankcard.last();
			ssoId = rs_bankcard.getString("sso_id");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public HashMap<String, Object> handleInput(HashMap<String, Object> data) {
		// 获取parameter对应的内容
		parameter = MapUtil.getValue("parameter", data);
		admin_sso_id = MapUtil.getParameter(parameter, "admin_sso_id").trim();
		admin_card_no = MapUtil.getParameter(parameter, "admin_card_no").trim();
		store_id = MapUtil.getParameter(parameter, "store_id").trim();
		token = MapUtil.getParameter(parameter, "token").trim();
		file = MapUtil.getParameter(parameter, "file").trim();
		file_type = MapUtil.getParameter(parameter, "file_type").trim();
		
		if ((!admin_sso_id.equals("")) && admin_sso_id.equals("code")) {
//			ssoIds= "1" + new Random().nextInt(9000000);
//			ssoIds= "5412389";
			admin_sso_id= ssoId;
//			ssoIds= "7323436,7323436";
			parameter = parameter.replace("\"admin_sso_id\":code", "\"admin_sso_id\":\""+ admin_sso_id + "\"");
		}
		if((!admin_card_no.equals("")) && admin_card_no.equals("code")) {
			admin_card_no = "wff66504";
			parameter = parameter.replace("\"admin_card_no\":code", "\"admin_card_no\":\""+ admin_card_no + "\"");
		}
		if((!store_id.equals("")) && store_id.equals("code")) {
			store_id = "1";
			parameter = parameter.replace("\"store_id\":code", "\"store_id\":\""+ store_id + "\"");
		}
		if((!token.equals("")) && token.equals("code")) {
			token = jw_token;
			parameter = parameter.replace("\"token\":code", "\"token\":\""+ token + "\"");
		}
		if((!file.equals("")) && file.equals("code")) {
			file = "Git123.pdf";
			parameter = parameter.replace("\"file\":code", "\"file\":\""+ file + "\"");
		}
		if((!file_type.equals("")) && file_type.equals("code")) {
			file_type = "";
			parameter = parameter.replace("\"file_type\":code", "\"file_type\":\""+ file_type + "\"");
		}
		
		data.put("parameter", parameter);
		return data;
	}

	@Override
	public Response SendRequest(HashMap<String, Object> data, String Url,
			String Request) {
		
		Map<String, String> formFileParameter=new HashMap<String, String>();
		formFileParameter.put("admin_sso_id", admin_sso_id);
		formFileParameter.put("admin_card_no", admin_card_no);
		formFileParameter.put("store_id", store_id);
		formFileParameter.put("token", token);
		formFileParameter.put("file_type", file_type);
		formFileParameter.put("file", "/out"+ file);
		MyRequest myRequest=new MyRequest();
		myRequest.setRequest(Request);
		myRequest.setUrl("/admin/package/edu/task/uploadFile");
		Map<String, String> cookie=new HashMap<String, String>();
		cookie.put("PHPSESSID", PHPSESSID);
		myRequest.setFormFile(file);
	    myRequest.setFormFileParameter(formFileParameter);
		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 = 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=StringUtils.decodeUnicode(getMsg(re));
			String code=getCode(re);
			
			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 && jp.getString("data")!=null){
				String custom=data.get("custom").toString();
				String[] ArrayString=StringUtils.getArrayString(custom,",");
				if(!StringUtils.VerificationString(jp.getString("data"),ArrayString)){
					result = result && false;
					failReason = failReason + "custom is expected "
							+ data.get("custom").toString() + " but actually "
							+ jp.getString("data") + ".";
				}
			}
			
			if("添加成功!".equals(msg)){
				
				//是否验证数据库
				if (!isProduct) {
					
				}
			}
			
			
		}
		if (result)
			return "Pass";
		else
			return "Fail:" + failReason;
	}

}
