package jnpf.attendance.service.handle.notice;


import cn.hutool.core.collection.CollUtil;
import jnpf.constants.AttendanceConstant;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.enums.attendance.UserSettingEnum;
import jnpf.model.attendance.model.DaySettlementModel;
import jnpf.model.attendance.model.JumpUrl;
import jnpf.model.attendance.model.SettlementNoticeModel;
import jnpf.model.attendance.model.SystemNoticeStrategy;
import jnpf.util.DateUtil;
import org.springframework.stereotype.Component;

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

import static jnpf.constants.AttendanceConstant.*;

/**
 * 日统计考勤结算通知
 */
@Component(AttendanceConstant.INDIVIDUAL_DAILY_STATISTICS)
public class SettlementDayNotice implements AttendanceNotice<DaySettlementModel, SettlementNoticeModel> {
    @Resource
    private AttendanceNoticeHandler attendanceNoticeHandler;

    @Override
    public Boolean isPass(SettlementNoticeModel settlementNoticeModel) {
        return attendanceNoticeHandler.checkBeforeCondition(settlementNoticeModel.getUserId(),
                UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE,
                UserSettingEnum.ATTENDANCE_SETTING_DAILY_REPORT);
    }

    @Override
    public List<DaySettlementModel> generatingData(SettlementNoticeModel settlementNoticeModel) {
        return settlementNoticeModel.getDayNoticeModelList().stream().map(item -> DaySettlementModel.builder()
                .groupName(item.getGroupName())
                .shiftStr(String.join(" ", item.getShiftList()))
                .shiftTimeStr(String.join(" ", item.getShiftTimeList()))
                .clockTimeStr(String.join("|", item.getClockTimeList()))
                .clockTime(item.getClockTime())
                .shouldAttendHours(item.getShouldAttendHours())
                .actualAttendHours(item.getActualAttendHours())
                .workingHours(item.getWorkingHours())
                .lateTimes(item.getLateTimes())
                .lateMinutes(item.getLateMinutes())
                .earlyLeaveTimes(item.getEarlyLeaveTimes())
                .earlyLeaveMinutes(item.getEarlyLeaveMinutes())
                .absenceCardTimes(item.getAbsenceCardTimes())
                .absenceCardHours(item.getAbsenceCardHours())
                .absenceTimes(item.getAbsenceTimes())
                .absenceHours(item.getAbsenceHours())
                .leaveHours(item.getLeaveHours())
                .leaveDays(item.getLeaveDays())
                .busDays(item.getBusDays())
                .outDays(item.getOutDays())
                .outHours(item.getOutHours())
                .overtimeHours(item.getOvertimeHours())
                .endDate(DateUtil.dateToString(new Date(), "yyyy年MM月dd日HH:mm"))
                .build()).collect(Collectors.toList());
    }

    @Override
    public List<String> getRecipient(SettlementNoticeModel settlementNoticeModel) {
        return CollUtil.newArrayList(settlementNoticeModel.getUserId());
    }


    @Override
    public SystemNoticeStrategy setSystemNoticeStrategy(SettlementNoticeModel settlementNoticeModel, String noticeId) {
        ArrayList<JumpUrl> objects = CollUtil.newArrayList();
        objects.add(JumpUrl.builder().url(String.format(INDIVIDUAL_DAILY_STATISTICS_URL1, noticeId))
                .urlName(INDIVIDUAL_DAILY_STATISTICS_BUTTON_NAME1).color(COLOR_BLACK).build());
        objects.add(JumpUrl.builder().url(INDIVIDUAL_DAILY_STATISTICS_URL2)
                .urlName(INDIVIDUAL_DAILY_STATISTICS_BUTTON_NAME2).color(COLOR_BLUE).build());
        return SystemNoticeStrategy.builder()
                .title(settlementNoticeModel.getTitle())
                .noticeModuleEnum(NoticeModuleEnum.GR_RB)
                .jumpUrls(objects)
                .build();

    }

    @Override
    public String getContext(List<DaySettlementModel> data) {
        DaySettlementModel daySettlementModel = data.stream().findFirst().orElse(null);
        return "考勤组" + daySettlementModel.getGroupName() + "</br>" +
                "当日班次：" + daySettlementModel.getShiftStr() + "</br>" +
                "班次时间：" + daySettlementModel.getShiftTimeStr() + "</br>" +
                "打卡时间：" + daySettlementModel.getClockTimeStr() + "</br>";
    }
}
