package jnpf.attendance.service.handle.notice;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jnpf.attendance.antifreeze.UserAntifreeze;
import jnpf.attendance.service.AttendanceUserService;
import jnpf.constants.AttendanceConstant;
import jnpf.entity.AttendanceGroupUser;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.enums.attendance.UserSettingEnum;
import jnpf.model.attendance.model.AttendanceNoticeModel;
import jnpf.model.attendance.model.JumpUrl;
import jnpf.model.attendance.model.LockModel;
import jnpf.model.attendance.model.SystemNoticeStrategy;
import jnpf.permission.model.user.PartUserInfoVo;
import jnpf.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static jnpf.constants.AttendanceConstant.*;

/**
 * 考勤组锁定通知
 */
@Component(AttendanceConstant.GROUP_LOCK)
public class GroupLockNotice implements AttendanceNotice<LockModel, AttendanceNoticeModel> {
    @Autowired
    private UserAntifreeze userAntifreeze;
    @Resource
    private AttendanceUserService attendanceUserService;
    @Resource
    private AttendanceNoticeHandler attendanceNoticeHandler;

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

    @Override
    public List<LockModel> generatingData(AttendanceNoticeModel noticeModel) {
        List<LockModel> data = new ArrayList<>();
        List<PartUserInfoVo> allByIds = userAntifreeze.getAllByIds(CollUtil.newArrayList(noticeModel.getUserId()), noticeModel.getTenantId());
        PartUserInfoVo partUserInfoVo = allByIds.stream().findFirst().orElse(null);
        StringBuffer stringBuffer = new StringBuffer();
        if (Objects.isNull(partUserInfoVo)) {
            stringBuffer.append("无");
        } else {
            stringBuffer.append(partUserInfoVo.getRealName()).append("(").append(StringUtil.isEmpty(partUserInfoVo.getOrganizeName()) ? "暂无" : partUserInfoVo.getOrganizeName()).append("-").append(StringUtil.isEmpty(partUserInfoVo.getPositionName()) ? "暂无" : partUserInfoVo.getPositionName()).append(")");
        }
        data.add(LockModel.builder()
                .date(new Date())
                .userName(stringBuffer.toString())
                .build());
        return data;
    }

    @Override
    public List<String> getRecipient(AttendanceNoticeModel noticeModel) {
        Date date = new Date();
        List<AttendanceGroupUser> userList = attendanceUserService.getAttendanceGroupUsersOfSecondment(date, date, null, List.of(noticeModel.getGroupId()));
        return CollUtil.isEmpty(userList) ? null : attendanceNoticeHandler.getUserIdsForFilterStatus(
                userList.stream().map(AttendanceGroupUser::getUserId).distinct().collect(Collectors.toList()),
                UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE,
                UserSettingEnum.ATTENDANCE_SETTING_ATTENDANCE_GROUP_LOCK_UNLOCK_NOTICE);
    }


    @Override
    public SystemNoticeStrategy setSystemNoticeStrategy(AttendanceNoticeModel noticeModel, String noticeId) {
        ArrayList<JumpUrl> objects = CollUtil.newArrayList();
        objects.add(JumpUrl.builder().url(String.format(GROUP_LOCK_URL1, noticeModel.getUserId()))
                .urlName(GROUP_LOCK_BUTTON_NAME1).color(COLOR_BLACK)
                        .type(3)
                        .page("contactInfo")
                        .param(JSON.toJSONString(new JSONObject(){{
                            put("contactId", noticeModel.getUserId());
                        }}))
                .build());
        objects.add(JumpUrl.builder().url(String.format(GROUP_LOCK_URL2, noticeId))
                .urlName(GROUP_LOCK_BUTTON_NAME2).color(COLOR_BLUE).build());
        return SystemNoticeStrategy.builder()
                .title(TITLE_GROUP_LOCK)
                .noticeModuleEnum(NoticeModuleEnum.KQZ_SD)
                .jumpUrls(objects)
                .build();
    }

    @Override
    public String getContext(List<LockModel> data) {
        StringBuffer stringBuffer = new StringBuffer();
        LockModel lockModel = data.stream().findFirst().orElse(null);
        stringBuffer.append("操作人：").append(lockModel.getUserName()).append("</br>");
        stringBuffer.append("当前所在考勤组已被管理员锁定，无法再对").append(DateUtil.format(lockModel.getDate(), "yyyy年MM月dd日"))
                .append("之前的出勤结果做变更!");
        return stringBuffer.toString();
    }
}
