package com.malk.service.dingtalk.impl;

import com.alibaba.fastjson.JSON;
import com.malk.server.dingtalk.DDConf;
import com.malk.server.dingtalk.DDR;
import com.malk.server.dingtalk.DDR_New;
import com.malk.service.dingtalk.DDClient_Extension;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class DDImplClient_Extension implements DDClient_Extension {

    /**
     * 发送钉钉互动卡片
     *
     * @apiNote https://open.dingtalk.com/document/orgapp/send-interactive-dynamic-cards-1
     */
    @Override
    public Map sendInteractiveCards(String accessToken, String cardTemplateId, String outTrackId, int conversationType, String robotCode, String chatBotId, String openConversationId, Map cardData, Map extInfo) {

        Map data = UtilMap.map("cardTemplateId, outTrackId, conversationType, robotCode, chatBotId, openConversationId, cardData", cardTemplateId, outTrackId, conversationType, robotCode, chatBotId, openConversationId, cardData);
        UtilMap.putAll(data, extInfo);
        return (Map) DDR_New.doPost("https://api.dingtalk.com/v1.0/im/interactiveCards/send", DDConf.initTokenHeader(accessToken), null, data).getResult();
    }

    /**
     * 注册互动卡片回调地址
     *
     * @implNote https://open.dingtalk.com/document/orgapp/register-card-callback-address
     */
    @Override
    public Map registerInterActiveCard(String access_token, String callback_url, Map extInfo) {

        Map data = UtilMap.map("callback_url", callback_url);
        UtilMap.putAll(data, extInfo);
        return (Map) DDR.doPost("https://oapi.dingtalk.com/topapi/im/chat/scencegroup/interactivecard/callback/register", null, DDConf.initTokenParams(access_token), data).getResult();
    }

    /**
     * 机器人发送群聊消息 [其它参数公司, 参考 发送钉钉互动卡片]
     *
     * @apiNote https://open.dingtalk.com/document/orgapp/the-robot-sends-a-group-message
     * @apiNote https://open.dingtalk.com/document/orgapp/types-of-messages-sent-by-robots
     */
    @Override
    public Map sendGroupMessages(String accessToken, Map msgParam, String msgKey, String openConversationId, String robotCode, String coolAppCode) {

        Map data = UtilMap.map("msgParam, msgKey, openConversationId, robotCode, coolAppCode", JSON.toJSONString(msgParam), msgKey, openConversationId, robotCode, coolAppCode);
        return (Map) DDR_New.doPost("https://api.dingtalk.com/v1.0/robot/groupMessages/send", DDConf.initTokenHeader(accessToken), null, data).getResult();
    }

    /**
     * 自定义机器人发送群消息
     *
     * @apiNote https://open.dingtalk.com/document/orgapp/custom-robots-send-group-messages
     */
    @Override
    public Map sendMessages(String accessToken, Map msgType, Map extInfo) {

        Map data = UtilMap.map("msgtype", msgType);
        UtilMap.putAll(data, extInfo);
        return (Map) DDR.doPost("https://oapi.dingtalk.com/robot/send", null, DDConf.initTokenParams(accessToken), extInfo).getResult();
    }
}
