package com.house365.web;

import com.house365.service.interfaces.ComputeClient;
import com.house365.service.interfaces.WechatClient;
import com.house365.util.RestObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Date: 2016/11/29</p>
 *
 * @author XN
 * @version 1.0
 */
@Controller
@EnableAutoConfiguration
public class IndexController {
    private final static Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    ComputeClient computeClient;

    @Autowired
    WechatClient wechatClient;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/")
    public String home(Model model, HttpServletRequest request) {
        //        QueryParams queryParams = QueryParams.getQueryParams(request, "id", "desc");
        //        computeClient.list(queryParams);
        return "/wechat/_demolist";
    }

    @RequestMapping("/form")
    public String form() {
        return "form";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @ResponseBody
    public RestObject add() {
        String res = computeClient.add(10, 20) + "万";
        System.out.println(res);
        RestObject restObject= new RestObject();
        restObject.setEntity(res);
        return restObject;
    }

    @RequestMapping(value = "checkWeChatCallback", method = RequestMethod.GET)
    @ResponseBody
    public String checkWeChatCallback(
        HttpServletResponse response, @RequestParam(value = "msg_signature") String sVerifyMsgSig,
        @RequestParam(value = "timestamp") String sVerifyTimeStamp, @RequestParam(value = "nonce") String sVerifyNonce,
        @RequestParam(value = "echostr") String sVerifyEchoStr
    ) {
        response.setStatus(200);
        return computeClient.checkWeChatCallback(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr);
    }


}
