package com.iamberry.rent.handlers.machine;

import java.io.ByteArrayOutputStream;
import java.util.List;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iamberry.rent.face.machine.MachineService;
import com.iamberry.rent.face.order.RentOrderService;
import com.iamberry.rent.service.machine.mapper.MachineMapper;
import com.iamberry.wechat.core.entity.rent.MachineInfo;
import com.iamberry.wechat.face.wechat.ConfigService;
import com.iamberry.wechat.iot.TransMsg;
import com.iamberry.wechat.iot.WechatIotUtils;
import com.iamberry.wechat.tools.HttpClient431Util;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResponseJson;

@RequestMapping("/getCode")
@Controller
public class AdminRentalEquipmentQrCodeHandler {
	//测试
	/*public static String APPID = "wxc71b789fad06a2d7";
	public static String APPSECRET = "8a90e3ab0e2db7772f5f64e7ed2f4b98";
	public static String PRODUCT_ID = "29439";// 微信硬件平台生成的 
	*/
	public static String APPID = "wx27c9f825761a861b";
	public static String APPSECRET = "97b2f2991e4ff0f2567fbc625f858377";
	public static String PRODUCT_ID = "30361";// 微信硬件平台生成的 正式
	public static String USERNAME = "water@iamberry.com";// 账号
	public static String PASSWORD = "Iamberry123";// 密码
	@Autowired
	private ConfigService configService;
	@Autowired
	private MachineMapper machineMapper;
	@Autowired
	private RentOrderService rentOrderService;
	@Autowired
	private MachineService machineService;
	

	@RequestMapping("/selectEquipmentCode")
	@ResponseBody
	public ResponseJson selectEquipmentCode(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String mac = request.getParameter("mac");
		ResponseJson json = new ResponseJson();
		if(mac == null || mac.equals("")){
			//return "{\"status\":\"-1\",\"msg\":\"账号或密码错误!\"}" ;
			json.setReturnCode(503);
			json.setResultCode(200);
			json.addResponseKeyValue("mac错误!");
			return json;
		}
		if(username == null || password == null){
			//return "{\"status\":\"-1\",\"msg\":\"账号或密码错误!\"}" ;
			json.setReturnCode(503);
			json.setResultCode(200);
			json.addResponseKeyValue("账号或密码错误!");
			return json;
		}
		if(!username.equals(USERNAME)|| !password.equals(PASSWORD)){
			json.setReturnCode(503);
			json.setResultCode(200);
			json.addResponseKeyValue("账号或密码错误!");
			return json;
		}
		String deviceid = "";// 设备ID
		String qrticket = "";// 二维码
		/* 获取deviceid和二维码 */
		String accessToken = configService.getAccessToken();
		JSONObject data = WechatIotUtils.getQrcode(accessToken, NameUtils.getConfig("productId"));
		System.out.println("获取设备的deviceid和二维码:" + data);
		Object msg = JSONObject.fromObject(data.get("base_resp")).get("errmsg");
		if (msg.equals("ok")) {
			// 设备授权
			//mac = "789ce700a001";//需要工厂传值
			deviceid = data.get("deviceid").toString();
			qrticket = data.get("qrticket").toString();
			JSONObject authorizationData = WechatIotUtils.authorize(
					accessToken, mac, deviceid);
			System.out.println("返回：" + authorizationData);
			JSONArray errmsg = authorizationData.getJSONArray("resp");
			String seccess = JSONObject.fromObject(errmsg.getString(0))
					.get("errmsg").toString();
			if (seccess.equals("ok")) {// 是否授权成功
				// 保存设备信息
				MachineInfo info = new MachineInfo();
				info.setMachineDeviceId(deviceid);
				info.setMachineQrcode(qrticket);
				info.setMachineMac(mac);
				info.setMachineConnectState(2);
				info.setMachineInitState(2);
				info.setMachineState(1);
				info.setMachineHaveWater(0);
				info.setMachineTotalWater(0);
				machineService.add(info);
				// 返回二维码
				json.setReturnCode(200);
				json.setResultCode(200);
				json.addResponseKeyValue(qrticket);
				json.addResponseKeyValue("equipmentId", 10000+info.getMachineId());
				json.addResponseKeyValue("deviceid", deviceid);
				return json;
			} else {
				json.setReturnCode(503);
				json.setResultCode(200);
				json.addResponseKeyValue("获取出错！");
				return json;
			}

		} else {
			json.setReturnCode(503);
			json.setResultCode(200);
			json.addResponseKeyValue("获取出错！");
			return json;
		}
	}
	
}
