package com.fdkankan.dingtalk;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.fdkankan.common.util.Base64Converter;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

@Component
public class DingTalkSendUtils {

    private static final Logger log = LoggerFactory.getLogger(DingTalkSendUtils.class);

    private static String token;

    private static String secret="SECca5af660ed882a67d8bda469095bf8454831528bd25bc7b1a3ff5f686109b9e5";

    @Value("${dingtalk.token:e712c41d917b303e21c1550ad9966b33dad874043f2f73fb347dce67a0a201bc}")
    public void setToken(String token) {
        DingTalkSendUtils.token = token;
    }

    @Value("${dingtalk.secret:SECca5af660ed882a67d8bda469095bf8454831528bd25bc7b1a3ff5f686109b9e5}")
    public void setSign(String secret) {
        DingTalkSendUtils.secret = secret;
    }


    public void sendActioncardMsgToDingRobot(String content,String title) throws ApiException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        Long timestamp = System.currentTimeMillis();
        String sign = getSign(timestamp);
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?" +
                "access_token="+token +
                "&timestamp=".concat(String.valueOf(timestamp)).concat("&sign=").concat(sign));
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("actionCard");
        OapiRobotSendRequest.Actioncard actioncard = new OapiRobotSendRequest.Actioncard();
        actioncard.setTitle(title);
        actioncard.setText(content);
        request.setActionCard(actioncard);
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        at.setAtMobiles(new ArrayList<>(0));
        // isAtAll类型如果不为Boolean，请升级至最新SDK
        at.setIsAtAll(true);
        request.setAt(at);
        OapiRobotSendResponse re = client.execute(request);
        System.out.println(re.getBody());
    }

    public static String getSign(Long timestamp) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        System.out.println(new String(signData));
        return URLEncoder.encode(Base64Converter.encode(signData));
    }
}
