package jnpf.notice.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import jnpf.ImRobotApi;
import jnpf.base.ActionResult;
import jnpf.from.ImRobotTypeEnum;
import jnpf.from.JumpUrlListModel;
import jnpf.from.SendRobotNoticeDataForm;
import jnpf.from.SingleSendRobotNoticeForm;
import jnpf.model.notice.dto.FtbNoticeAnnouncementsDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
@Slf4j
public class NoticeIMUtils {

    @Autowired
    private ImRobotApi imRobotApi;
    private static final String NOTICE_APP_NAME = "通知公告";
    private static final String NOTICE_APP_LOGO = "https://jnpf-resource-1304460613.cos.ap-chengdu.myqcloud.com/jnpf-resources/UserAvatar/66457c71e4b05b749193397b.png";
    private static final String NOTICE_BUTTON_NAME = "查看全文 >";
    private static final String NOTICE_BUTTON_LINK = "/pages/announcement/components/info?id=%s";//跳转页面链接


    /**
     * 发送消息
     *
     * @param tenantId  租户
     * @param toUserIds 接收人
     * @param dto       公告
     * @return
     */
    public Boolean sendMsg(String tenantId, List<String> toUserIds, FtbNoticeAnnouncementsDto dto) {
        if (CollectionUtil.isEmpty(toUserIds)) {
            return false;
        }
        SingleSendRobotNoticeForm form = new SingleSendRobotNoticeForm();
        form.setRobotTypeEnum(ImRobotTypeEnum.FTB_XZS);
        form.setTenantId(tenantId);
        SendRobotNoticeDataForm robotNoticeDataForm = new SendRobotNoticeDataForm();
        robotNoticeDataForm.setLogo(NOTICE_APP_LOGO);//固定图片
        robotNoticeDataForm.setAppName(NOTICE_APP_NAME);
        if(null==dto.getInnerAlert() || dto.getInnerAlert()==0) {
            robotNoticeDataForm.setTitle(buildNoticeTitle(dto));
            robotNoticeDataForm.setContent(dto.getShortContent());
        }else{
            robotNoticeDataForm.setTitle("您有一条未读公告");
            robotNoticeDataForm.setContent(buildNoticeTitle(dto));
        }
        robotNoticeDataForm.setExpand(buildExpand(dto));
        robotNoticeDataForm.setJumpUrlList(buildBtnUrl(dto.getId()));
        form.setRobotNoticeDataForm(robotNoticeDataForm);
        List<List<String>> batches = Lists.partition(toUserIds, 450);
        for (List<String> batch : batches) {
            form.setToUserIds(batch);
            ActionResult actionResult = imRobotApi.sendSingleRobotNotice(form);
            if (actionResult == null || !actionResult.getCode().equals(200)) {
                log.error("通知公告im发送失败,send msg req={},result={}", JSONUtil.toJsonStr(form), JSONUtil.toJsonStr(actionResult));
            }
        }
        return true;
    }

    private String buildNoticeTitle(FtbNoticeAnnouncementsDto dto) {
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotEmpty(dto.getCategoryName())) {
            sb.append("【");
            sb.append(dto.getCategoryName());
            sb.append("】");
        }
        sb.append(dto.getTitle());
        return sb.toString();
    }

    private String buildExpand(FtbNoticeAnnouncementsDto dto) {
        JSONObject expand = new JSONObject();
        expand.put("announcementId", dto.getId());
        expand.put("needRead", dto.getNeedRead());
        return expand.toJSONString();
    }

    private LinkedList<JumpUrlListModel> buildBtnUrl(String announcementId) {
        LinkedList<JumpUrlListModel> btn = new LinkedList<>();
        JumpUrlListModel urlModel = new JumpUrlListModel();
        urlModel.setDisplayMethodEnum(JumpUrlListModel.DisplayMethodEnum.TRUE);
        urlModel.setButtonName(NOTICE_BUTTON_NAME);
        urlModel.setReqMethod(JumpUrlListModel.ReqMethodEnum.GET);
        urlModel.setType(1);//1-跳转链接  2-表单提交
        urlModel.setUrl(String.format(NOTICE_BUTTON_LINK, announcementId));
        urlModel.setMpId("__UNI__EA6FB1C");
        btn.add(urlModel);
        return btn;
    }

}
