package com.template.controller;

import cn.snowheart.dingtalk.robot.starter.client.DingTalkRobotClient;
import cn.snowheart.dingtalk.robot.starter.entity.ActionCardMessage;
import cn.snowheart.dingtalk.robot.starter.entity.DingTalkResponse;
import cn.snowheart.dingtalk.robot.starter.entity.FeedCardMessageItem;
import cn.snowheart.dingtalk.robot.starter.entity.TextMessage;
import cn.snowheart.dingtalk.robot.starter.type.ButtonOrientationType;
import cn.snowheart.dingtalk.robot.starter.type.HideAvatarType;
import com.template.annotation.PassToken;
import com.template.api.DingTalkControllerAPI;
import com.template.model.result.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * @Author: xwt
 * @Date: 2024/10/15 星期二 14:39
 * @Description: com.template.controller
 * @Version: 1.0
 */
@RestController
public class DingTalkController implements DingTalkControllerAPI {
    @Autowired
    private DingTalkRobotClient dingTalkRobotClient;

    private static Logger logger = LoggerFactory.getLogger(DingTalkController.class);

    @Override
    @PassToken
    public CommonResult sendMessage() {
        String phone ="";
        String content = "sdsd";
        logger.info("钉钉消息推送phone：" + phone+"content");
        String[] arr = {"13262965932"};
        //arr[0] = "13262965932";
        Boolean isAll = false;
        TextMessage textMessage = new TextMessage();
        textMessage.setContent(content);
        textMessage.setIsAtAll(isAll);
        textMessage.setAtMobiles(arr);
        logger.info("Message:" + textMessage.toMessageMap().toString());
        DingTalkResponse response = dingTalkRobotClient.sendMessage(textMessage);
        logger.info(response.toString());
        if (response.getErrcode().intValue() != 0) {
            logger.info("钉钉消息推送失败：" + response.getErrmsg());
            return CommonResult.fail(response.getErrmsg());
        }
        return CommonResult.ok(response);
    }

    @Override
    @PassToken
    public CommonResult sendFeedCardMessage() {
        ArrayList<FeedCardMessageItem> items = new ArrayList<>();
        FeedCardMessageItem messageItem = new FeedCardMessageItem();
        messageItem.setMessageURL("http://mp.weixin.qq.com/s/CPUaB60pue0Zf3fUL9xqvw");
        messageItem.setTitle("标题");
        messageItem.setPicURL("https://mmbiz.qpic.cn/mmbiz_jpg/YriaiaJPb26VND0Q0hSBOoyVkr9cXQrFjWI7hOzax1IxIibqanXYD4W8nyeYX5iaicjgiaqia7ly94iawOsGwehbKGwGsA/640");
        items.add(messageItem);
        logger.info("Message:" + items);
        DingTalkResponse response = dingTalkRobotClient.sendFeedCardMessage(items);
        logger.info(response.toString());
        return CommonResult.ok(response);
    }

    @Override
    @PassToken
    public CommonResult sendActionCardMessage() {
        ActionCardMessage actionCardMessage = new ActionCardMessage();
        actionCardMessage.setTitle("标题");
        actionCardMessage.setText("![screenshot](@lADOpwk3K80C0M0FoA)**Apple Store** 的设计正从原来满满的科技感走向生活化，而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");
        actionCardMessage.setBtnOrientation(ButtonOrientationType.HORIZONTAL);
        actionCardMessage.setButtonView(true);
        actionCardMessage.setHideAvatar(HideAvatarType.HIDE);
        logger.info("Message:" + actionCardMessage);
        DingTalkResponse response = dingTalkRobotClient.sendActionCardMessage(actionCardMessage);
        logger.info(response.toString());
        return CommonResult.ok(response);
    }
}
