package com.fengqi.wechat.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.fengqi.wechat.entity.MessageConstant;
import com.fengqi.wechat.entity.dto.WechatClickEventXmlDto;
import com.fengqi.wechat.entity.dto.WechatSignatureDto;
import com.fengqi.wechat.entity.vo.WechatImageMsgVo;
import com.fengqi.wechat.entity.vo.WechatMessageReturnVo;
import com.google.common.collect.Lists;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class WechatUtil {

    public static WechatMessageReturnVo setMessageReturn(JSONObject obj, WechatClickEventXmlDto wechatClickEventXmlDto) {
        WechatMessageReturnVo wechatMessageReturnVo = new WechatMessageReturnVo()
                .setFromUserName(wechatClickEventXmlDto.getToUserName())
                .setToUserName(wechatClickEventXmlDto.getFromUserName())
                .setCreateTime(wechatClickEventXmlDto.getCreateTime());

        switch (obj.getStr(MessageConstant.MESSAGE_MSGTYPE)) {
            case MessageConstant.MESSAGE_TYPE_TEXT:
                wechatMessageReturnVo.setMsgType(MessageConstant.MESSAGE_TYPE_TEXT)
                        .setContent(obj.getStr(MessageConstant.MESSAGE_CONTENT).replace("</br>", "\n"));
                break;
            case MessageConstant.MESSAGE_TYPE_NEWS:
                WechatImageMsgVo WechatImageMsgVo = new WechatImageMsgVo()
                        .setTitle(obj.getStr(MessageConstant.MESSAGE_TITLE))
                        .setDescription(obj.getStr(MessageConstant.MESSAGE_DESCRIPTION))
                        .setPicUrl(obj.getStr(MessageConstant.MESSAGE_PICURL))
                        .setUrl(obj.getStr(MessageConstant.MESSAGE_URL));
                //-------------------------------------------------------------------//
                wechatMessageReturnVo.setArticles(Lists.newArrayList(WechatImageMsgVo))
                        .setArticleCount(MessageConstant.MESSAGE_ARTICLECOUNT)
                        .setMsgType(MessageConstant.MESSAGE_TYPE_NEWS);
                break;
            case MessageConstant.MESSAGE_TYPE_IMAGE:
                wechatMessageReturnVo.setMsgType(MessageConstant.MESSAGE_TYPE_IMAGE)
                        .setImage(Lists.newArrayList(obj.getStr(MessageConstant.MESSAGE_MEDIAID)));
                break;
        }
        return wechatMessageReturnVo;
    }

    public static String checkSignature(WechatSignatureDto dto, String token) {
        String[] strings = {token, dto.getTimestamp(), dto.getNonce()};
        Arrays.sort(strings);
        StringBuilder sb = new StringBuilder();
        for (String string : strings) {
            sb.append(string);
        }

        String s = SecureUtil.sha1(sb.toString());

        return StrUtil.isNotBlank(s) && StrUtil.equals(s, dto.getSignature()) ? dto.getEchostr() : null;
    }


    public static List<Object> isReceive(JSONArray jsonArray, String unionid, JSONObject jsonObject){
        List<Object> collect = jsonArray.stream().filter(m -> {
            JSONObject jsonObj = (JSONObject) m;
            if (jsonObj.getStr(MessageConstant.USER_OPENID).equals(unionid)
                    && jsonObj.getStr(MessageConstant.GIFT_ID).equals(jsonObject.getStr(MessageConstant.MESSAGE_CONTENT))) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());
        return collect;
    }

    public static WechatMessageReturnVo ConvertToVo(WechatClickEventXmlDto wechatClickEventXmlDto){
        return new WechatMessageReturnVo()
                .setFromUserName(wechatClickEventXmlDto.getToUserName())
                .setToUserName(wechatClickEventXmlDto.getFromUserName())
                .setCreateTime(wechatClickEventXmlDto.getCreateTime())
                .setMsgType(MessageConstant.MESSAGE_TYPE_TEXT);
    }
}
