package jnpf.attendance.service.handle.notice;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import jnpf.constants.AttendanceConstant;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.enums.attendance.UserSettingEnum;
import jnpf.model.attendance.dto.LineDrawingPeriodDto;
import jnpf.model.attendance.model.*;
import jnpf.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

import static jnpf.constants.AttendanceConstant.*;

@Component(AttendanceConstant.LINE_SHIFT_CHANG)
@Slf4j
public class LineShiftChangAttendanceNotice implements AttendanceNotice<LineDrawingPeriodDto, LineShiftChangeNoticeModel> {
    @Autowired
    private AttendanceNoticeHandler attendanceNoticeHandler;

    @Override
    public Boolean isPass(LineShiftChangeNoticeModel attendanceNoticeModel) {
        return true;
    }

    @Override
    public List<LineDrawingPeriodDto> generatingData(LineShiftChangeNoticeModel attendanceNoticeModel) {
        //查询时段数据
        if (CollUtil.isEmpty(attendanceNoticeModel.getPeriods())) {
            return List.of(LineDrawingPeriodDto.builder().day(attendanceNoticeModel.getDay()).build());
        }
        return attendanceNoticeModel.getPeriods();
    }

    @Override
    public List<String> getRecipient(LineShiftChangeNoticeModel attendanceNoticeModel) {
        return CollUtil.isEmpty(attendanceNoticeModel.getUserIds()) ? List.of() : attendanceNoticeModel.getUserIds().stream().filter(userId -> attendanceNoticeHandler.checkBeforeCondition(userId, UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE)).collect(Collectors.toList());
    }

    @Override
    public SystemNoticeStrategy setSystemNoticeStrategy(LineShiftChangeNoticeModel attendanceNoticeModel, String noticeId) {
        ArrayList<JumpUrl> objects = CollUtil.newArrayList();
        boolean hasSchedule = attendanceNoticeModel.getPeriods().stream().anyMatch(vo -> StringUtil.isNotEmpty(vo.getStart()));
        if (hasSchedule) {
            objects.add(JumpUrl.builder().url(BTN_FAST_UPDATE_URL).urlName(BTN_NAME_GO).color(COLOR_BLACK).build());
        } else {
            objects.add(JumpUrl.builder().build());
        }
        return SystemNoticeStrategy.builder()
                .title(ATTENDANCE_NOTICE_LINE_SHIFT_CHANGE_TITLE)
                .noticeModuleEnum(NoticeModuleEnum.BC_BD)
                .jumpUrls(objects)
                .build();
    }

    @Override
    public String getContext(List<LineDrawingPeriodDto> data) {
        if (CollUtil.isEmpty(data)) {
            return null;
        }
        StringBuilder stringBuffer = new StringBuilder();
        String dayStr = data.stream().map(LineDrawingPeriodDto::getDay).distinct().map(day -> DateUtil.format(day, "yyyy年MM月dd日") + "(" + DateUtil.dayOfWeekEnum(day).toChinese() + ")").collect(Collectors.joining("，"));
        stringBuffer.append("提醒通知：您在").append(dayStr);
        if (data.stream().allMatch(vo -> Objects.isNull(vo.getStart()))) {
            stringBuffer.append("暂无排班。");
            return stringBuffer.toString();
        }
        if (data.stream().allMatch(vo -> StringUtil.isEmpty(vo.getStart()))) {
            stringBuffer.append("的排班已取消。");
            return stringBuffer.toString();
        }
        List<LineDrawingPeriodDto> data1 = data.stream().collect(Collectors.groupingBy(LineDrawingPeriodDto::getDay)).values().stream().findFirst().orElse(List.of());
        stringBuffer.append("的排班调整为：").append(data1.stream().sorted(Comparator.comparing(LineDrawingPeriodDto::getStartType).thenComparing(LineDrawingPeriodDto::getStart)).map(vo -> vo.getStart() + "~" + vo.getEnd()).collect(Collectors.joining("，"))).append("。");
        return stringBuffer.toString();
    }

}
