package jnpf.cultivate.utils;

import cn.hutool.json.JSONUtil;
import jnpf.ImRobotApi;
import jnpf.base.ActionResult;
import jnpf.from.ImRobotTypeEnum;
import jnpf.from.JumpUrlListModel;
import jnpf.from.SendRobotNoticeDataForm;
import jnpf.from.SingleSendRobotNoticeForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * 培训im通知工具
 * @Author: peng.hao
 * @create: 2024/7/24:15:50
 */
@Component
@Slf4j
@SuppressWarnings("Duplicates")
public class CultivateImUtil {

    @Autowired
    private ImRobotApi imRobotApi;

    // app名称
    private static final String APP_NAME = "培训";

    private static final String CERTIFICATE_TITLE = "培训管理";
    // logo
    public static final String LOGO_URL = "https://jnpf-resource-1304460613.cos.ap-chengdu.myqcloud.com/jnpf-resources/UserAvatar/66457c71e4b05b749193397b.png";

    // 我的证书
    public static final String BASE_URL = "/pages/myCert/index?menuId=461212890154671237&fullName=我的证书";

    public static final String BTN_NAME_ZS = "查看证书";

    public static final String URL_CULTIVATE_APPROVE = "/pages/teaching/myTeaching/teachingDetails?bizId=%s";

    public static final String BTN_NAME_DETAIL = "查看详情";

    private static final String CULTIVATE_MP_ID = "__UNI__74A05F6";//__UNI__74A05F6 培训管理

    /**
     * 发送消息
     *
     * @param userIds  发送消息 userIds
     * @param tenantId 租户id
     */
    public void sendMessage(List<String> userIds, String tenantId,String contentMessage) {
        SingleSendRobotNoticeForm form = new SingleSendRobotNoticeForm();
        form.setRobotTypeEnum(ImRobotTypeEnum.FTB_XZS);
        form.setTenantId(tenantId);
        form.setToUserIds(userIds);
        SendRobotNoticeDataForm robotNoticeDataForm = new SendRobotNoticeDataForm();
        robotNoticeDataForm.setLogo(LOGO_URL);//固定图片
        robotNoticeDataForm.setAppName(APP_NAME);
        robotNoticeDataForm.setTitle(CERTIFICATE_TITLE);
        robotNoticeDataForm.setContent(contentMessage);
        form.setRobotNoticeDataForm(robotNoticeDataForm);
        // 跳转链接
        JumpUrlListModel jumpUrlListModel = createJumpUrlListModel(BASE_URL, BTN_NAME_ZS);
        LinkedList<JumpUrlListModel> objects = new LinkedList<>();
        objects.add(jumpUrlListModel);
        robotNoticeDataForm.setJumpUrlList(objects);
        ActionResult actionResult = imRobotApi.sendSingleRobotNotice(form);
        if (actionResult == null || !actionResult.getCode().equals(200)) {
            log.error("岗位学习证书发送消息: {}, {}", JSONUtil.toJsonStr(form), JSONUtil.toJsonStr(actionResult));
        }
    }

    /**
     * 发送培训带教审核IM消息
     *
     * @param userIds  发送消息 userIds
     * @param tenantId 租户id
     */
    public void sendCultivateApproveMsg(List<String> userIds, String tenantId, String contentMessage, String teachId) {
        SingleSendRobotNoticeForm form = new SingleSendRobotNoticeForm();
        form.setRobotTypeEnum(ImRobotTypeEnum.FTB_XZS);
        form.setTenantId(tenantId);
        form.setToUserIds(userIds);
        SendRobotNoticeDataForm robotNoticeDataForm = new SendRobotNoticeDataForm();
        robotNoticeDataForm.setLogo(LOGO_URL);
        robotNoticeDataForm.setAppName(APP_NAME);
        robotNoticeDataForm.setTitle(CERTIFICATE_TITLE);
        robotNoticeDataForm.setContent(contentMessage);
        form.setRobotNoticeDataForm(robotNoticeDataForm);
        // 跳转链接
        JumpUrlListModel jumpUrlListModel = createJumpUrlListModel(String.format(URL_CULTIVATE_APPROVE, teachId), BTN_NAME_DETAIL);
        LinkedList<JumpUrlListModel> objects = new LinkedList<>();
        objects.add(jumpUrlListModel);
        robotNoticeDataForm.setJumpUrlList(objects);
        ActionResult actionResult = imRobotApi.sendSingleRobotNotice(form);
        if (actionResult == null || !actionResult.getCode().equals(200)) {
            log.error("发送消息失败 -> 带教审核结果提醒： {}, {}", JSONUtil.toJsonStr(form), JSONUtil.toJsonStr(actionResult));
        }
    }

    /**
     * 发送任务中止消息
     *
     * @param userIds  发送消息 userIds
     * @param tenantId 租户id
     */
    public void sendTaskAbortMessage(List<String> userIds, String tenantId, String contentMessage) {
        SingleSendRobotNoticeForm form = new SingleSendRobotNoticeForm();
        form.setRobotTypeEnum(ImRobotTypeEnum.FTB_XZS);
        form.setMessageAttributionRobotMpId("__UNI__74A05F6");
        form.setTenantId(tenantId);
        form.setToUserIds(userIds);
        SendRobotNoticeDataForm robotNoticeDataForm = new SendRobotNoticeDataForm();
        robotNoticeDataForm.setLogo(LOGO_URL);
        robotNoticeDataForm.setAppName(APP_NAME);
        robotNoticeDataForm.setTitle(CERTIFICATE_TITLE);
        robotNoticeDataForm.setContent(contentMessage);
        // 无跳转链接
        form.setRobotNoticeDataForm(robotNoticeDataForm);
        ActionResult actionResult = imRobotApi.sendSingleRobotNotice(form);
        if (actionResult == null || !actionResult.getCode().equals(200)) {
            log.error("培训任务中止发送消息", JSONUtil.toJsonStr(form), JSONUtil.toJsonStr(actionResult));
        }
    }

    private JumpUrlListModel createJumpUrlListModel(String url, String btnName) {
        JumpUrlListModel jumpUrlListModel = new JumpUrlListModel();
        jumpUrlListModel.setType(1);
        jumpUrlListModel.setUrl(url);
        jumpUrlListModel.setButtonName(btnName);
        jumpUrlListModel.setMpId(CULTIVATE_MP_ID);
        jumpUrlListModel.setReqMethod(JumpUrlListModel.ReqMethodEnum.GET);
        jumpUrlListModel.setDisplayMethodEnum(JumpUrlListModel.DisplayMethodEnum.TRUE);
        return jumpUrlListModel;
    }
}
