package com.iformall.controller;

import com.iformall.annotation.AuthIgnore;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxAppinfo;
import com.iformall.domain.po.WxMall;
import com.iformall.service.WxAppinfoService;
import com.iformall.service.WxMallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/mall")
@Api(description = "商场信息相关接口")
public class WxMallController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${version}")
    private String version;

    @Autowired
    private WxMallService wxMallService;

    @Autowired
    private WxAppinfoService wxAppinfoService;


    @AuthIgnore
    @ApiOperation("根据appId获取")
    @GetMapping("/getAppIcon")
    @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true)
    public ResultData getAppIcon(String appId) {
        WxAppinfo appInfo = wxAppinfoService.getByAppId(appId);
        if (appInfo == null) {
            return new ResultData(ErrorCode.APP_ID_NOT_FOUND);
        }
        WxMall mall = wxMallService.getByTenantId(appInfo.getTenantId());
        if (mall == null) {
            return new ResultData(ErrorCode.MALL_INFO_NOT_FOUND);
        }
        Map resultMap = new HashMap();
        resultMap.put("mallImgUrl", mall.getImgUrl());
        resultMap.put("mallName", mall.getName());
        return new ResultData(Result.SUCCESS, "查询成功", resultMap);
    }

    @ApiOperation("根据appId获取")
    @GetMapping("/mallInfo")
    @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true)
    public ResultData getMallInfo(String appId) {
        WxAppinfo appInfo = wxAppinfoService.getByAppId(appId);
        if (appInfo == null) {
            return new ResultData(ErrorCode.APP_ID_NOT_FOUND);
        }
        WxMall mall = wxMallService.getByTenantId(appInfo.getTenantId());
        if (mall == null) {
            return new ResultData(ErrorCode.MALL_INFO_NOT_FOUND);
        }
        return new ResultData(Result.SUCCESS, "查询成功", mall);
    }

    @AuthIgnore
    @ApiOperation("获取后端版本号")
    @GetMapping("/version")
    public ResultData version() {
        return new ResultData(version);
    }
    /**
     * 微信消息服务验证
     * @param response
     * @param request
     * @throws IOException
     */
    @AuthIgnore
    @RequestMapping(value = "/signature")
    @ResponseBody
    public void signature(HttpServletResponse response, HttpServletRequest request) throws IOException {


        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");

        logger.warn("收到的微信服务验证信息："+signature+"\n"+timestamp+"\n"+nonce+"\n"+echostr);
        PrintWriter out = response.getWriter();
        // 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
        // 当前无法确定此消息的appId及token, 所以直接返回echostr
        //if (CheckUtil.checkSignature(wp.getToken(), signature, timestamp, nonce)) {
        out.print(echostr);
        logger.warn("微信服务验证成功===================="+echostr);
        System.out.println("微信服务验证成功！");
        //}
        out.close();
    }


}
