package com.tzld.piaoquan.growth.common.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class LarkRobotUtil {

    private static final String SECRET = "iYGMzvTCIIX35WyPlxB3Lh";

    private static final String URL = "https://open.feishu.cn/open-apis/bot/v2/hook/93787b70-33d3-42c1-beae-c09310c9b38b";

    private static final String TIP_URL = "https://open.feishu.cn/open-apis/bot/v2/hook/0eb8793a-5bb3-43d7-aa74-fed1c91e3d9b";

    private static final String WE_COM_NOT_PUSH_URL = "https://open.feishu.cn/open-apis/bot/v2/hook/6fa54ed6-12f7-45c1-a33b-020d4f4483ef";

    private static final String AUTO_REPLY_VIDEO_URL = "https://open.feishu.cn/open-apis/bot/v2/hook/0d80b7dc-47d1-4c27-a7e1-97f2fb6ba26e";

    private static final String WE_COM_THIRDPART_URL = "https://open.feishu.cn/open-apis/bot/v2/hook/d5a61df6-7a74-4fa1-808f-e388cfa9b16c";

    private static final HttpPoolClientUtil HTTP_POOL_CLIENT_UTIL_DEFAULT = HttpClientUtil.create(3000, 10000, 20, 100, 3, 3000);


    public static void sendMessage(String msg) {
        sendMessage("text", msg);
    }

    public static void sendMessage(String type, String msg) {
        try {
            long timestamp = System.currentTimeMillis() / 1000;
//            String sign = getSign(timestamp);
            JSONObject param = new JSONObject();
//            param.put("timestamp", String.valueOf(timestamp));
//            param.put("sign", sign);
            param.put("msg_type", type);
            JSONObject content = new JSONObject();
            content.put("text", msg);
            param.put("content", content);
            HTTP_POOL_CLIENT_UTIL_DEFAULT.post(URL, param.toJSONString());
        } catch (Exception e) {
            log.error("Lark sendMessage error", e);
        }
    }

    public static void sendMessage(JSONObject param) {
        try {
            HTTP_POOL_CLIENT_UTIL_DEFAULT.post(URL, param.toJSONString());
        } catch (Exception e) {
            log.error("Lark sendMessage error", e);
        }
    }

    public static void sendTipMessage(String msg) {
        sendTipMessage("text", "企微推送结果：" + msg);
    }

    public static void sendTipMessage(String type, String msg) {
        try {
            JSONObject param = new JSONObject();
            param.put("msg_type", type);
            JSONObject content = new JSONObject();
            content.put("text", msg);
            param.put("content", content);
            HTTP_POOL_CLIENT_UTIL_DEFAULT.post(TIP_URL, param.toJSONString());
        } catch (Exception e) {
            log.error("Lark sendMessage error", e);
        }
    }

    public static void sendTipMessage(JSONObject param) {
        try {
            HTTP_POOL_CLIENT_UTIL_DEFAULT.post(TIP_URL, param.toJSONString());
        } catch (Exception e) {
            log.error("Lark sendMessage error", e);
        }
    }


    public static void sendAutoReplyVideoMessage(JSONObject param) {
        try {
            HTTP_POOL_CLIENT_UTIL_DEFAULT.post(AUTO_REPLY_VIDEO_URL, param.toJSONString());
        } catch (Exception e) {
            log.error("Lark sendMessage error", e);
        }
    }

    public static void sendWeComThirdPartMessage(String msg) {
        sendWeComThirdPartMessage("text", msg);
    }

    public static void sendWeComThirdPartMessage(String type, String msg) {
        try {
            JSONObject param = new JSONObject();
            param.put("msg_type", type);
            JSONObject content = new JSONObject();
            content.put("text", msg);
            param.put("content", content);
            HTTP_POOL_CLIENT_UTIL_DEFAULT.post(WE_COM_THIRDPART_URL, param.toJSONString());
        } catch (Exception e) {
            log.error("Lark sendMessage error", e);
        }
    }

    public static void sendNotPushMessage(String msg) {
        try {
            JSONObject param = new JSONObject();
            param.put("msg_type", "text");
            JSONObject content = new JSONObject();
            content.put("text", msg);
            param.put("content", content);
            HTTP_POOL_CLIENT_UTIL_DEFAULT.post(WE_COM_NOT_PUSH_URL, param.toJSONString());
        } catch (Exception e) {
            log.error("Lark sendMessage error", e);
        }
    }

    public static void sendNotPushMessage(JSONObject param) {
        try {
            HTTP_POOL_CLIENT_UTIL_DEFAULT.post(WE_COM_NOT_PUSH_URL, param.toJSONString());
        } catch (Exception e) {
            log.error("Lark sendMessage error", e);
        }
    }


    private static String getSign(long timestamp) throws NoSuchAlgorithmException, InvalidKeyException {
        //把timestamp+"\n"+密钥当做签名字符串
        String stringToSign = timestamp + "\n" + SECRET;
        //使用HmacSHA256算法计算签名
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
        byte[] signData = mac.doFinal(new byte[]{});
        return new String(Base64.encodeBase64(signData));
    }


}
