package com.cweb.controller.wx;

import com.alibaba.fastjson.JSONObject;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.common.utils.WxUtils;
import com.hfkj.config.CommonSysConst;
import com.hfkj.config.WeChatQrcodeUtils;
import com.hfkj.model.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author sum1dream
 */

@Controller
@RequestMapping(value = "/wxMsg")
@Api(value = "微信")
public class WxMsgController {

    private static final Logger log = LoggerFactory.getLogger(WxMsgController.class);

    @Autowired
    private WeChatQrcodeUtils weChatQrcodeUtils;


//    @RequestMapping(value = "/verifyWxToken", method = RequestMethod.GET)
//    @ResponseBody
//    @ApiOperation(value = "验证servlet")
//    public String verifyWxToken(
//            @RequestParam(name = "signature", required = false) String signature,
//            @RequestParam(name = "timestamp", required = false) String timestamp,
//            @RequestParam(name = "nonce", required = false) String nonce,
//            @RequestParam(name = "echostr", required = false) String echostr
//    )  {
//
//        try {
//
//            String signatureStr = WxUtils.getSHA1("RgAWdnR5oEOLBdyEjfr4" , timestamp , nonce);
//            log.info("signatureStr！！！！！" + signatureStr);
//            log.info("signature！！！！！" + signature);
//            if (signature.equals(signatureStr)) {
//                log.info("验证通过！！！！！");
//                return echostr;
//            }
//            log.info("验证失败！！！！！");
//            return null;
//        } catch (Exception e) {
//            return null;
//        }
//
//    }


    @RequestMapping(value = "/verifyWx", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "微信编译URL")
    public ResponseData verifyWx(@RequestParam(value = "url", required = true) String url)  {

        try {

            JSONObject jsapiTicket = weChatQrcodeUtils.getTicket(false);
            log.info("获取jsapiTicket"+ jsapiTicket);
            // 拼接签名
            Map<String, Object> map = new HashMap<>();
            map.put("noncestr", WxUtils.makeNonStr());
            map.put("jsapi_ticket",jsapiTicket.getString("ticket"));
            map.put("timestamp", new Date().getTime());
            map.put("url",url);

            log.info("拼接map"+ map);

            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("appId" , CommonSysConst.getSysConfig().getWechatMpAppid());
            objectMap.put("timestamp" , map.get("timestamp"));
            objectMap.put("nonceStr" , map.get("noncestr"));
            objectMap.put("signature" , WxUtils.getSHA1(map));

            log.info("拼接objectMap"+ objectMap);

            return ResponseMsgUtil.success(objectMap);
        } catch (Exception e) {
            return null;
        }

    }


    @RequestMapping(value = "/createMenu", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建菜单")
    public ResponseData createMenu(@RequestBody JSONObject object) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("appid", CommonSysConst.getSysConfig().getWechatMpAppid());
            params.put("secret", CommonSysConst.getSysConfig().getWechatMpSecret());
            params.put("grant_type", "client_credential");

            JSONObject accessTokenObject = HttpsUtils.doGet("https://api.weixin.qq.com/cgi-bin/token", params);
            if (accessTokenObject == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取微信access_token失败");
            }

            JSONObject jsonObject = HttpsUtils.doPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessTokenObject.getString("access_token"), object);

            return ResponseMsgUtil.success(jsonObject);

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }



}
