package com.house365.wechat.rest;

import com.house365.beans.dto.MessageDTO;
import com.house365.common.util.RestTemplateBean;
import com.house365.wechat.WeChatConstant;
import com.house365.wechat.service.RemoteCallService;
import net.sf.json.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Date: 2017/3/24</p>
 *
 * @author XN
 * @version 1.0
 */
@RestController
@RequestMapping("message")
public class MessageController {

    Logger logger = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    private RemoteCallService remoteCallService;

    @RequestMapping(value = "sendText", method = RequestMethod.POST)
    public String sendText(
        @RequestParam(value = "corpId") String corpId, @RequestParam(value = "secret") String secret,
        @RequestBody MessageDTO message
    ) {
        List<NameValuePair> params = new ArrayList<>();
        //获取access_token
        String accessToken = remoteCallService.getAccessToken(corpId, secret);
        params.add(new BasicNameValuePair(WeChatConstant.WE_CHAT_ACCESS_TOKEN, accessToken));

        String result = null;
        try {
            Map<String, Object> content = new HashMap<>();
            content.put("content", message.getContent());
            message.setContent(null);
            JSONObject obj = JSONObject.fromObject(message);
            obj.put("text", content);

            result = RestTemplateBean.doPost(WeChatConstant.getWeChatPostUrl(WeChatConstant.MESSAGE_SEND, params), obj);

            logger.info("sendText call result [{}],message[{}]", result, message);
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }

        return result;
    }

    @RequestMapping(value = "sendCardText", method = RequestMethod.POST)
    public String sendCardText(
            @RequestParam(value = "corpId") String corpId, @RequestParam(value = "secret") String secret,
            @RequestBody MessageDTO message
    ) {
        List<NameValuePair> params = new ArrayList<>();
        //获取access_token
        String accessToken = remoteCallService.getAccessToken(corpId, secret);
        params.add(new BasicNameValuePair(WeChatConstant.WE_CHAT_ACCESS_TOKEN, accessToken));

        String result = null;
        try {
            Map<String, Object> textcard = new HashMap<>();
            textcard.put("title", message.getTitle());
            textcard.put("description", message.getDescription());
            textcard.put("url", message.getUrl());
            textcard.put("btntxt", message.getBtntxt());
            textcard.put("agentid", message.getAgentid());
            textcard.put("touser", message.getTouser());
            JSONObject obj = JSONObject.fromObject(message);
            obj.put("textcard", textcard);
            result = RestTemplateBean.doPost(WeChatConstant.getWeChatPostUrl(WeChatConstant.MESSAGE_SEND, params), obj);

            logger.info("sendCardText call result [{}],message[{}]", result, message);
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }

        return result;
    }
}
