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.JumpUrl;
import jnpf.model.attendance.model.MonthSettlementModel;
import jnpf.model.attendance.model.SettlementNoticeModel;
import jnpf.model.attendance.model.SystemNoticeStrategy;
import org.springframework.stereotype.Component;

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

import static jnpf.constants.AttendanceConstant.*;

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

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

    @Override
    public List<MonthSettlementModel> generatingData(SettlementNoticeModel settlementNoticeModel) {
        return settlementNoticeModel.getMonthNoticeModelList().stream().map(item -> MonthSettlementModel.builder()
                .cycleDate(item.getCycleDate())
                .groupName(item.getGroupName())
                .effectiveAttendDays(item.getEffectiveAttendDays())
                .leaveDays(item.getLeaveDays())
                .leaveHours(item.getLeaveHours())
                .lateTimes(item.getLateTimes())
                .earlyLeaveTimes(item.getEarlyLeaveTimes())
                .absenceCardTimes(item.getAbsenceCardTimes())
                .absenceTimes(item.getAbsenceTimes())
                .busTimes(item.getBusTimes())
                .outTimes(item.getOutTimes())
                .overtimeTimes(item.getOvertimeTimes())
                .endDate(item.getEndDate())
                .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_MONTHLY_STATISTICS_URL1, noticeId))
                .urlName(INDIVIDUAL_MONTHLY_STATISTICS_BUTTON_NAME1).color(COLOR_BLACK).build());
        objects.add(JumpUrl.builder().url(INDIVIDUAL_MONTHLY_STATISTICS_URL2)
                .urlName(INDIVIDUAL_MONTHLY_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<MonthSettlementModel> data) {
        MonthSettlementModel monthSettlementModel = data.stream().findFirst().orElse(null);
        StringBuilder stringBuffer = new StringBuilder();
        stringBuffer.append("时间：").append(monthSettlementModel.getCycleDate()).append("</br>");
        stringBuffer.append("考勤组：").append(monthSettlementModel.getGroupName()).append("</br>");
        stringBuffer.append("出勤情况：").append("</br>");
        getTipsStr(monthSettlementModel, stringBuffer);
        return stringBuffer.toString();
    }

    private void getTipsStr(MonthSettlementModel monthSettlementModel, StringBuilder tips) {
        if (monthSettlementModel.getEffectiveAttendDays().compareTo(BigDecimal.ZERO) > 0) {
            tips.append("出勤天数：").append(monthSettlementModel.getEffectiveAttendDays()).append("天</br>");
        } else if (monthSettlementModel.getLeaveDays().compareTo(BigDecimal.ZERO) > 0) {
            tips.append("请假天数：").append(getFieldValue(monthSettlementModel.getLeaveDays(), monthSettlementModel.getLeaveHours())).append("</br>");
        } else if (monthSettlementModel.getLateTimes() > 0) {
            tips.append("迟到次数：").append(monthSettlementModel.getLateTimes()).append("次</br>");
        } else if (monthSettlementModel.getEarlyLeaveTimes() > 0) {
            tips.append("早退次数：").append(monthSettlementModel.getEarlyLeaveTimes()).append("次</br>");
        } else if (monthSettlementModel.getAbsenceCardTimes() > 0) {
            tips.append("缺卡次数：").append(monthSettlementModel.getAbsenceCardTimes()).append("次</br>");
        } else if (monthSettlementModel.getAbsenceTimes() > 0) {
            tips.append("缺勤次数：").append(monthSettlementModel.getAbsenceTimes()).append("次</br>");
        } else if (monthSettlementModel.getBusTimes() > 0) {
            tips.append("出差次数：").append(monthSettlementModel.getBusTimes()).append("次</br>");
        } else if (monthSettlementModel.getOutTimes() > 0) {
            tips.append("外出次数：").append(monthSettlementModel.getOutTimes()).append("次</br>");
        } else if (monthSettlementModel.getOvertimeTimes() > 0) {
            tips.append("加班次数：").append(monthSettlementModel.getOvertimeTimes()).append("次</br>");
        }
    }
    /**
     * 生成组合字段值
     *
     * @param durationDays  时长天数
     * @param durationHours 时长小时
     * @return 组合字段值
     */
    private static String getFieldValue(BigDecimal durationDays, BigDecimal durationHours) {
        StringBuilder sb = new StringBuilder();
        if (durationDays.compareTo(BigDecimal.ZERO) == 0 && durationHours.compareTo(BigDecimal.ZERO) == 0) {
            sb.append("0天");
        } else if (durationDays.compareTo(BigDecimal.ZERO) > 0 && durationHours.compareTo(BigDecimal.ZERO) > 0) {
            sb.append(durationDays).append("天").append(durationHours).append("小时");
        } else if (durationDays.compareTo(BigDecimal.ZERO) == 0 && durationHours.compareTo(BigDecimal.ZERO) > 0) {
            sb.append(durationHours).append("小时");
        } else if (durationDays.compareTo(BigDecimal.ZERO) > 0 && durationHours.compareTo(BigDecimal.ZERO) == 0) {
            sb.append(durationDays).append("天");
        }
        return sb.toString();
    }
}
