package com.palmnest.webapp.controller.lingling;

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

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.core.util.CollectionUtils;
import com.palmnest.application.core.util.LingLingUtil;
import com.palmnest.application.dto.input.LingLingVisitorMachineInputDto;
import com.palmnest.application.dto.input.lingling.QrcodeInsInputDTO;
import com.palmnest.application.dto.output.lingling.LingLingResultDto;
import com.palmnest.application.dto.output.lingling.LingLingResultUtil;
import com.palmnest.application.dto.output.lingling.QrcodeInsVo;
import com.palmnest.model.estateserve.VisitorInfo;
import com.palmnest.model.estateserve.VisitorOrder;
import com.palmnest.service.estateserve.VisitorInFoManager;
import com.palmnest.service.lingling.manager.LingLingManager;

/**
 * @Author lmz
 * @Description 新版令令访客机控制器======>NEW
 * @Date 2021/01/21
 **/
@Slf4j
@Controller
@RequestMapping(value = "/lingling/VisitorPcPost")
public class LingLingVisitorPcPostController {
	
	@Autowired
	private LingLingManager lingLingManager;
	@Autowired
	private VisitorInFoManager visitorInFoManager;
	
	/**
	 * 访客机接口参数解析
	 *
	 * @param request
	 * @return
	 */
	private JSONObject handleParam(HttpServletRequest request) {

		try {
			String message = request.getParameter("MESSAGE");

			JSONObject jsonObject   = JSONObject.parseObject(message);
			JSONObject requestParam = jsonObject.getJSONObject("requestParam");

            log.info("LingLingVisitorMachineController handleParam 令令回调参数处理========>params:{}",message);

			return requestParam;
		} catch (Exception e) {
			log.error("LingLingVisitorMachineController handleParam 令令回调参数处理异常=====>" + e.getMessage());
		}
		return null;
	}

	/**
	 * 上传照片(身份证头像/抓拍头像)
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/uploadPhoto")
	@ResponseBody
	public LingLingResultDto uploadPhoto(HttpServletRequest request) {

		JSONObject requestParam = handleParam(request);
		if (Objects.isNull(requestParam)) {
			return LingLingResultUtil.createErrorDto("uploadPhoto", LingLingUtil.PARAM_ANALYSIS_ERROR);
		}

		String photo     = requestParam.getString("photoData");// 照片base64字符串
		String photoName = requestParam.getString("photoName");// 照片名称
		String photoType = requestParam.getString("photoType");// 照片类型
		//1 如是身份证头像 photoType 会传值：headImg
		//2 如是抓拍图片 photoType 会传值：SnapImg
		try {
			lingLingManager.uploadPhoto(photo, photoName, photoType);
			return LingLingResultUtil.createSuccesDto("uploadPhoto", LingLingUtil.UPLOADPHOTO_SUCCESS);
		} catch (Exception e) {
			log.error("uploadPhoto error=======>", e.getMessage());
			return LingLingResultUtil.createErrorDto("uploadPhoto", LingLingUtil.SERVER_ERROR);
		}
	}
	
	/**
	 * 发送短信
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sendQRCode")
	@ResponseBody
	public LingLingResultDto sendQRCode(HttpServletRequest request) {

		JSONObject requestParam = handleParam(request);
		if (Objects.isNull(requestParam)) {
			return LingLingResultUtil.createErrorDto("sendQRCode", LingLingUtil.PARAM_ANALYSIS_ERROR);
		}

		String telephone = requestParam.getString("telephone");// 访客手机号
		String visitorId    = requestParam.getString("visitorId");// 二维码ID
		String deviceCode = requestParam.getString("deviceCode");// deviceCode
		if (StringUtils.isAnyBlank(telephone, visitorId)) {
			return LingLingResultUtil.createErrorDto(LingLingUtil.STATUS_CODE_10001, "sendQRCode", LingLingUtil.PARAM_EMPTY_ERROR);
		}
		VisitorOrder visitorOrder = visitorInFoManager.getVisitorOrder(Integer.parseInt(visitorId));
		if(null==visitorOrder){
			return LingLingResultUtil.createErrorDto(LingLingUtil.STATUS_CODE_10001, "sendQRCode", LingLingUtil.PARAM_EMPTY_ERROR);
		}
		String qrcodeKey = visitorOrder.getVisitorQrCode();// 二维码数据
		String codeId=visitorOrder.getVisitorQrCodeId()+"";
		if (StringUtils.isAnyBlank(qrcodeKey, codeId)) {
			return LingLingResultUtil.createErrorDto(LingLingUtil.STATUS_CODE_10001, "sendQRCode", LingLingUtil.PARAM_EMPTY_ERROR);
		}

		try {
			
			LingLingVisitorMachineInputDto inputDto = lingLingManager.getVisitorMachineByDeviceCode(deviceCode);
			if (Objects.isNull(inputDto)) {
				log.info("qrcodeVisitor error=======>{}", LingLingUtil.UN_INITIALIZE_MACHINE);
				return LingLingResultUtil.createErrorDto("sendQRCode", LingLingUtil.UN_INITIALIZE_MACHINE);
			}	
			
			lingLingManager.sendQRCode(telephone, qrcodeKey,2,inputDto.getOrgId());
			return LingLingResultUtil.createSuccesDto("sendQRCode", "sendQRCode success");
		} catch (Exception e) {
			log.error("sendQRCode error=======>", e.getMessage());
			return LingLingResultUtil.createErrorDto("sendQRCode", LingLingUtil.SERVER_ERROR);
		}

	}
	
	/**
	 * 访客预约申请
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/qrcodeVisitor")
	@ResponseBody
	public LingLingResultDto qrcodeVisitor(HttpServletRequest request) {

		JSONObject requestParam = handleParam(request);
		if (Objects.isNull(requestParam)) {
			return LingLingResultUtil.createErrorDto("qrcodeVisitor", LingLingUtil.PARAM_ANALYSIS_ERROR);
		}

		try {
			
			QrcodeInsInputDTO inputDTO = JSONObject.parseObject(requestParam.toJSONString(), QrcodeInsInputDTO.class);
			if(StringUtils.isBlank(inputDTO.getDeviceCode())){
				log.info("qrcodeVisitor error=======>{}", LingLingUtil.UN_INITIALIZE_MACHINE);
				return LingLingResultUtil.createErrorDto("loginWin", LingLingUtil.UN_INITIALIZE_MACHINE);
			}
			LingLingVisitorMachineInputDto inputDto = lingLingManager.getVisitorMachineByDeviceCode(inputDTO.getDeviceCode());
			if (Objects.isNull(inputDto)) {
				log.info("qrcodeVisitor error=======>{}", LingLingUtil.UN_INITIALIZE_MACHINE);
				return LingLingResultUtil.createErrorDto("loginWin", LingLingUtil.UN_INITIALIZE_MACHINE);
			}	
			inputDTO.setOrgnaisationId(inputDto.getOrgId());
			QrcodeInsVo       vo       = lingLingManager.createVisitorMachineQrCode(inputDTO,2);
			if (Objects.isNull(vo)) {
				return LingLingResultUtil.createErrorDto("qrcodeVisitor", "生成二维码异常");
			}
			return LingLingResultUtil.createSuccesDto("qrcodeVisitor", vo);
		} catch (Exception e) {
			log.error("qrcodeVisitor error=======>", e.getMessage());
			return LingLingResultUtil.createErrorDto("qrcodeVisitor", LingLingUtil.SERVER_ERROR);
		}
	}
	
	/**
	 * 获取二维码
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getCodeData")
	@ResponseBody
	public LingLingResultDto getCodeData(HttpServletRequest request) {

		JSONObject requestParam = handleParam(request);
		if (Objects.isNull(requestParam)) {
			return LingLingResultUtil.createErrorDto("getCodeData", LingLingUtil.PARAM_ANALYSIS_ERROR);
		}
		try {
			
			String visitorId = requestParam.getString("visitorId");// 访客记录 ID
			String deviceCode    = requestParam.getString("deviceCode");//访客机串码，客户机的 MAC 地址
			if (StringUtils.isAnyBlank(visitorId)) {
				return LingLingResultUtil.createErrorDto(LingLingUtil.STATUS_CODE_10001, "getCodeData", LingLingUtil.PARAM_EMPTY_ERROR);
			}
			VisitorOrder info = visitorInFoManager.getVisitorOrder(Integer.parseInt(visitorId));
			String codeData="";
			String departName="";
			String visitFloor="";
			if(null!=info){
				codeData=info.getVisitorQrCode()+"";
				departName=info.getVisitorToCompany();
				visitFloor=info.getVisitorToFloor();
			}
			Map<String,String> map=new HashMap<String, String>();
			map.put("codeData", codeData);
			map.put("departName", departName);
			map.put("visitFloor", visitFloor);
			return LingLingResultUtil.createSuccesDto("getCodeData", map);
		} catch (Exception e) {
			log.error("qrcodeIns error=======>", e.getMessage());
			return LingLingResultUtil.createErrorDto("getCodeData", LingLingUtil.SERVER_ERROR);
		}
	}
}
