package jnpf.attendance.service.handle.notice;


import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jnpf.attendance.antifreeze.UserAntifreeze;
import jnpf.base.UserInfo;
import jnpf.constants.AttendanceConstant;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.model.attendance.model.AdminUpdateModel;
import jnpf.model.attendance.model.AdminUpdateNoticeModel;
import jnpf.model.attendance.model.JumpUrl;
import jnpf.model.attendance.model.SystemNoticeStrategy;
import jnpf.permission.model.user.PartUserInfoVo;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static jnpf.constants.AttendanceConstant.*;
import static jnpf.enums.attendance.UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE;

/**
 * 考勤组锁定通知
 */
@Component(AttendanceConstant.ADMINISTRATOR_CHANGE)
public class GroupAdminUpdateNotice implements AttendanceNotice<AdminUpdateModel, AdminUpdateNoticeModel> {
    @Autowired
    private UserAntifreeze userAntifreeze;
    @Resource
    private AttendanceNoticeHandler attendanceNoticeHandler;

    @Override
    public Boolean isPass(AdminUpdateNoticeModel noticeModel) {
        return Boolean.TRUE;
    }

    @Override
    public List<AdminUpdateModel> generatingData(AdminUpdateNoticeModel noticeModel) {
        return CollUtil.newArrayList(AdminUpdateModel.builder()
                .type(noticeModel.getType())
                .groupName(noticeModel.getGroupName())
                .currentAuthority(noticeModel.getCurrentAuthority())
                .sonAuthority(noticeModel.getSonAuthority())
                .build());
    }

    @Override
    public List<String> getRecipient(AdminUpdateNoticeModel noticeModel) {
        return attendanceNoticeHandler.getUserIdsForFilterStatus(noticeModel.getUserIds(), ATTENDANCE_SETTING_MESSAGE_RECEIVE);
    }


    @Override
    public SystemNoticeStrategy setSystemNoticeStrategy(AdminUpdateNoticeModel noticeModel, String noticeId) {
        UserInfo user = UserProvider.getUser();
        ArrayList<JumpUrl> objects = CollUtil.newArrayList();
        if (noticeModel.getType().equals(1)) {
            objects.add(JumpUrl.builder().url(String.format(ADMINISTRATOR_CHANGE_URL1, noticeId))
                    .urlName(ADMINISTRATOR_CHANGE_BUTTON_NAME1).color(COLOR_BLACK).build());
            objects.add(JumpUrl.builder().url(String.format(ADMINISTRATOR_CHANGE_URL2, noticeModel.getGroupId()))
                    .urlName(ADMINISTRATOR_CHANGE_BUTTON_NAME2).color(COLOR_BLUE).build());
            return SystemNoticeStrategy.builder()
                    .title(String.format(ADMINISTRATOR_CHANGE_TITLE1, noticeModel.getGroupName()))
                    .noticeModuleEnum(NoticeModuleEnum.KQZ_GLY_BD)
                    .jumpUrls(objects)
                    .build();
        } else {
            objects.add(new JumpUrl(String.format(AttendanceConstant.GROUP_LOCK_URL1, user.getUserId()), AttendanceConstant.GROUP_LOCK_BUTTON_NAME1, AttendanceConstant.COLOR_BLACK,3,"contactInfo", JSON.toJSONString(new JSONObject(){{
                put("contactId", user.getUserId());
            }})));
            return SystemNoticeStrategy.builder()
                    .title(String.format(ADMINISTRATOR_CHANGE_TITLE2, noticeModel.getGroupName()))
                    .noticeModuleEnum(NoticeModuleEnum.KQZ_GLY_BD)
                    .jumpUrls(objects)
                    .build();
        }
    }

    @Override
    public String getContext(List<AdminUpdateModel> data) {
        StringBuffer stringBuffer = new StringBuffer();
        AdminUpdateModel lockModel = data.stream().findFirst().orElse(null);
        if (lockModel.getType().equals(1)) {
            stringBuffer.append("当前考勤组权限：").append(lockModel.getCurrentAuthority());
        } else {
            UserInfo user = UserProvider.getUser();
            List<PartUserInfoVo> allByIds = userAntifreeze.getAllByIds(CollUtil.newArrayList(user.getUserId()), user.getTenantId());
            PartUserInfoVo partUserInfoVo = allByIds.stream().findFirst().orElse(null);
            if (Objects.isNull(partUserInfoVo)) {
                stringBuffer.append("操作人：无</br>");
            } else {
                stringBuffer.append("操作人：").append(partUserInfoVo.getRealName()).append("(").append(StringUtil.isEmpty(partUserInfoVo.getOrganizeName()) ? "暂无" : partUserInfoVo.getOrganizeName()).append("-").append(StringUtil.isEmpty(partUserInfoVo.getPositionName()) ? "暂无" : partUserInfoVo.getPositionName()).append(")</br>");
            }
            stringBuffer.append("您已不能再对【").append(lockModel.getGroupName()).append("】进行相应操作（包含查看成员考勤信息，对考勤组规则进行配置，对成员进行排班等操作）。").append("</br>");
        }
        return stringBuffer.toString();
    }
}
