package jnpf.attendance.service.handle.notice;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import jnpf.attendance.antifreeze.UserAntifreeze;
import jnpf.attendance.service.AttendanceGroupService;
import jnpf.attendance.service.AttendanceSuperAdminService;
import jnpf.constants.AttendanceConstant;
import jnpf.entity.AttendanceGroup;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.model.attendance.model.*;
import jnpf.permission.model.user.PartUserInfoVo;
import jnpf.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static jnpf.constants.AttendanceConstant.*;
import static jnpf.constants.AttendancePermissionConstant.MANAGER;
import static jnpf.constants.AttendancePermissionConstant.SCHEDULING;

@Component(AttendanceConstant.CONSEC_UNSCHEDULED)
@Slf4j
public class ConsecUnscheduledNotice implements AttendanceNotice<ConsecUnscheduledModel, ConsecUnscheduledNoticeModel> {
    @Autowired
    private UserAntifreeze userAntifreeze;
    @Autowired
    private AttendanceGroupService attendanceGroupService;
    @Autowired
    private AttendanceSuperAdminService attendanceSuperAdminService;

    @Override
    public Boolean isPass(ConsecUnscheduledNoticeModel attendanceNoticeModel) {
        return Boolean.TRUE;
    }

    @Override
    public List<ConsecUnscheduledModel> generatingData(ConsecUnscheduledNoticeModel attendanceNoticeModel) {
        List<ConsecUnscheduledModel> shiftChangModels = CollUtil.newArrayList();
        if (CollUtil.isEmpty(attendanceNoticeModel.getUserIds())) {
            return shiftChangModels;
        }
        List<PartUserInfoVo> infoByIds = userAntifreeze.getInfoByIds(attendanceNoticeModel.getUserIds(), attendanceNoticeModel.getTenantId());
        if (CollUtil.isEmpty(infoByIds)) {
            return shiftChangModels;
        }
        List<String> collect = infoByIds.stream().map(PartUserInfoVo::getRealName).collect(Collectors.toList());
        AttendanceGroup byId = attendanceGroupService.getById(attendanceNoticeModel.getGroupId());
        if (Objects.isNull(byId)) {
            return shiftChangModels;
        }
        attendanceNoticeModel.setAttendanceGroup(new AttendanceGroupParam(attendanceNoticeModel.getGroupId(),byId.getGroupName()));
        shiftChangModels.add(new ConsecUnscheduledModel(byId.getGroupName(), collect));
        return shiftChangModels;
    }

    @Override
    public List<String> getRecipient(ConsecUnscheduledNoticeModel attendanceNoticeModel) {
        Map<String, List<String>> stringListMap = attendanceSuperAdminService.queryPermissionBySpecify(CollUtil.newArrayList(attendanceNoticeModel.getGroupId()), List.of(SCHEDULING), List.of(MANAGER));
        List<String> orDefault = stringListMap.getOrDefault("-1", CollUtil.newArrayList());
        List<String> orDefault1 = stringListMap.getOrDefault(attendanceNoticeModel.getGroupId(), CollUtil.newArrayList());
        orDefault.addAll(orDefault1);
        return orDefault.stream().distinct().filter(Objects::nonNull).collect(Collectors.toList());
    }

    @Override
    public SystemNoticeStrategy setSystemNoticeStrategy(ConsecUnscheduledNoticeModel attendanceNoticeModel, String noticeId) {
        ArrayList<JumpUrl> objects = CollUtil.newArrayList();
        objects.add(JumpUrl.builder().url(String.format(BTN_SCHEDULE_URL, JSON.toJSONString(attendanceNoticeModel.getAttendanceGroup()))).urlName(BTN_SCHEDULE_GO).color(COLOR_BLACK).build());
        return SystemNoticeStrategy.builder()
                .title(ATTENDANCE_NOTICE_CONSEC_UNSCHEDULED_TITLE)
                .noticeModuleEnum(NoticeModuleEnum.KQ_PB)
                .jumpUrls(objects)
                .build();
    }

    @Override
    public String getContext(List<ConsecUnscheduledModel> data) {
        if (CollUtil.isEmpty(data)) {
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer();
        ConsecUnscheduledModel shiftChangModel = data.stream().findFirst().orElse(null);
        stringBuffer.append("考勤组：").append(shiftChangModel.getGroupName()).append("</br>");
        List<String> userNames = shiftChangModel.getUserNames();
        stringBuffer.append("相关成员：").append(StringUtil.join(userNames.stream().limit(3).collect(Collectors.toList()), "、")).append(userNames.size() > 3 ? ("等共计" + userNames.size() + "名成员") : "");
        return stringBuffer.toString();
    }

}
