package com.palmnest.webapp.controller.wechat;

import com.palmnest.application.core.util.Tools;
import com.palmnest.service.wechat.WeChatAccessManager;
import com.palmnest.service.wechat.WeChatApiManager;
import lombok.extern.slf4j.Slf4j;
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.RequestMethod;

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

/**
 * 微信平台：针对微信平台请求和发送处理
 *
 * @author yubing
 */
@Slf4j
@Controller
@RequestMapping("/wechat/acc/*")
public class WeChatAccessController {

	@Autowired
	private WeChatAccessManager weChatAccessManager;
	@Autowired
	private WeChatApiManager    weChatApiManager;

	/**
	 * 订阅号配置接口
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "subscribeno", method = {RequestMethod.GET, RequestMethod.POST})
	public void subscribeno(HttpServletRequest request, HttpServletResponse response) {
		try {
			log.info("##subscribeno from client req:" + request.getRemoteAddr());
			String method = request.getMethod().toUpperCase();
			String outPut = "error";
			//DO POST请求..
			if ("POST".equals(method)) {
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/xml");
				ServletInputStream in     = request.getInputStream();
				String             xmlMsg = Tools.inputStream2String(in);
				log.info("##executeWechat process");
				log.info("####xmlMsg:" + xmlMsg);
				outPut = weChatAccessManager.saveProcessing(xmlMsg);
				log.info("####outPut:" + outPut);
				response.getWriter().write(outPut);
			} else {
				String signature = request.getParameter("signature");// 微信加密签名
				String timestamp = request.getParameter("timestamp");// 时间戳
				String nonce     = request.getParameter("nonce");// 随机数
				String echostr   = request.getParameter("echostr");//
				if (weChatApiManager.checkSignature(signature, timestamp, nonce)) {
					outPut = echostr;
				}
				response.getWriter().write(outPut);
			}
		} catch (Exception e) {
			log.error("executeWechatLail error {}", e, e.getMessage());
		}
	}

	/**
	 * 服务号号配置接口
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "serverno", method = {RequestMethod.GET, RequestMethod.POST})
	public void serverno(HttpServletRequest request, HttpServletResponse response) {
		try {
			log.info("##serverno from client req:" + request.getRemoteAddr());
			String method = request.getMethod().toUpperCase();
			String outPut = "error";
			//DO POST请求..
			if ("POST".equals(method)) {
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/xml");
				ServletInputStream in     = request.getInputStream();
				String             xmlMsg = Tools.inputStream2String(in);
				log.info("##executeWechat process");
				log.info("####xmlMsg:" + xmlMsg);
				outPut = weChatAccessManager.saveProcessing(xmlMsg);
				log.info("####outPut:" + outPut);
				response.getWriter().write(outPut);
			} else {
				String signature = request.getParameter("signature");// 微信加密签名
				String timestamp = request.getParameter("timestamp");// 时间戳
				String nonce     = request.getParameter("nonce");// 随机数
				String echostr   = request.getParameter("echostr");//
				if (weChatApiManager.checkSignature(signature, timestamp, nonce)) {
					outPut = echostr;
				}
				response.getWriter().write(outPut);
			}
		} catch (Exception e) {
			log.error("executeWechatLail error {}", e, e.getMessage());
		}
	}
}
