package jnpf.util.im;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import jnpf.ImRobotApi;
import jnpf.from.*;
import jnpf.model.im.UserAndLinkDTO;
import jnpf.model.im.UserSuccessDTO;
import jnpf.permission.UserApi;
import jnpf.permission.entity.UserEntity;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class ImMessageNoticeUtils {
    @Autowired
    private UserApi userApi;
    @Resource
    private ImRobotApi imRobotApi;
    @Resource
    private UserProvider userProvider;

    public void becomeRegularEmployee(List<UserSuccessDTO> successDTOS) {
        successDTOS.forEach(
                dto -> {
                    if (CollUtil.isNotEmpty(dto.getUserIds())) {
                        List<UserEntity> userEntityList = userApi.getUserName(dto.getUserIds());
                        userEntityList.forEach(userEntity -> {
                            SingleSendRobotNoticeForm singleSendRobotNoticeForm = new SingleSendRobotNoticeForm();
                            singleSendRobotNoticeForm.setRobotTypeEnum(ImRobotTypeEnum.FTB_XZS);
                            singleSendRobotNoticeForm.setMessageAttributionRobotMpId("__UNI__1EF82DA");
                            singleSendRobotNoticeForm.setTenantId(userProvider.get().getTenantId());
                            singleSendRobotNoticeForm.setToUserIds(List.of(userEntity.getId()));
                            SendRobotNoticeDataForm robotNoticeDataForm = new SendRobotNoticeDataForm();
                            robotNoticeDataForm.setAppName(ImConst.SYSTEM_APP_NAME);
                            robotNoticeDataForm.setLogo(ImConst.SYSTEM_LOGO);
                            robotNoticeDataForm.setTitle(ImConst.TITLE_BECOME_REGULAR_EMPLOYEE);
                            String content = String.format(dto.getSuccess() ? ImConst.SUCCESSFULLY_BECAME_A_REGULAR_EMPLOYEE : ImConst.FAILED_TO_BECOME_A_REGULAR_EMPLOYEE, userEntity.getRealName());
                            robotNoticeDataForm.setContent(content);
                            singleSendRobotNoticeForm.setRobotNoticeDataForm(robotNoticeDataForm);
                            imRobotApi.sendSingleRobotNotice(singleSendRobotNoticeForm);
                        });

                    }
                }
        );
    }

    public void inviteToJoin(List<UserAndLinkDTO> dtoList) {
        if (CollUtil.isNotEmpty(dtoList)) {
            List<UserEntity> userEntityList = userApi.getUserName(dtoList.stream().map(UserAndLinkDTO::getUserId).distinct().collect(Collectors.toList()));
            dtoList.forEach(dto -> {
                UserEntity thisUser = userEntityList.stream().filter(user -> user.getId().equals(dto.getUserId())).findFirst().orElse(null);
                if (thisUser != null) {
                    SingleSendRobotNoticeForm singleSendRobotNoticeForm = new SingleSendRobotNoticeForm();
                    singleSendRobotNoticeForm.setRobotTypeEnum(ImRobotTypeEnum.FTB_XZS);
                    singleSendRobotNoticeForm.setTenantId(userProvider.get().getTenantId());
                    singleSendRobotNoticeForm.setToUserIds(List.of(thisUser.getId()));

                    SendRobotNoticeDataForm robotNoticeDataForm = new SendRobotNoticeDataForm();
                    robotNoticeDataForm.setAppName(ImConst.HR_APP_NAME);
                    robotNoticeDataForm.setLogo(ImConst.HR_LOGO);
                    robotNoticeDataForm.setTitle(ImConst.TITLE_INVITE_TO_JOIN);
                    String content = String.format(ImConst.INVITE_TO_JOIN);

                    robotNoticeDataForm.setContent(content);

                    if (StrUtil.isNotBlank(dto.getLinkUrl())) {
                        JumpUrlListModel jumpUrlListModel = new JumpUrlListModel();
                        jumpUrlListModel.setDisplayMethodEnum(JumpUrlListModel.DisplayMethodEnum.TRUE);
                        jumpUrlListModel.setButtonName(ImConst.BUTTON_INVITE_TO_JOIN);
                        jumpUrlListModel.setReqMethod(JumpUrlListModel.ReqMethodEnum.GET);
                        jumpUrlListModel.setType(1);
                        jumpUrlListModel.setUrl(dto.getLinkUrl());
                        MiniAppUrl miniAppUrl = new MiniAppUrl();
                        miniAppUrl.setMpId("__UNI__1EF82DA");
                        String[] split = dto.getLinkUrl().split("url=");
                        miniAppUrl.setMpPage(split[0]);
                        JSONObject entries = new JSONObject();
                        entries.set("url",split[1]);
                        miniAppUrl.setMpParam(JSON.toJSONString(entries));
                        jumpUrlListModel.setMiniAppUrl(miniAppUrl);

                        log.error("入职请链接生成: " + dto.getLinkUrl());

                        LinkedList<JumpUrlListModel> jumpUrlListModels = new LinkedList<>();
                        jumpUrlListModels.add(jumpUrlListModel);
                        robotNoticeDataForm.setJumpUrlList(jumpUrlListModels);
                    }

                    singleSendRobotNoticeForm.setRobotNoticeDataForm(robotNoticeDataForm);

                    imRobotApi.sendSingleRobotNotice(singleSendRobotNoticeForm);
                }
            });
        }
    }
}
