package jnpf.attendance.service.handle.notice;


import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Maps;
import jnpf.constants.AttendanceConstant;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.model.attendance.model.JumpUrl;
import jnpf.model.attendance.model.SettlementNoticeModel;
import jnpf.model.attendance.model.SystemNoticeStrategy;
import jnpf.model.attendance.model.TeamMonthSettlementModel;
import org.springframework.stereotype.Component;

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

import static jnpf.constants.AttendanceConstant.*;

/**
 * 日统计考勤结算通知
 */
@Component(AttendanceConstant.TEAM_MONTHLY_STATISTICS)
public class SettlementTeamMonthNotice implements AttendanceNotice<TeamMonthSettlementModel, SettlementNoticeModel> {
    @Override
    public Boolean isPass(SettlementNoticeModel settlementNoticeModel) {
        return Boolean.TRUE;
    }

    @Override
    public List<TeamMonthSettlementModel> generatingData(SettlementNoticeModel settlementNoticeModel) {
        return settlementNoticeModel.getTeamMonthNoticeModelList().stream().map(item -> TeamMonthSettlementModel.builder()
                .groupName(item.getGroupName())
                .normalCount(item.getNormalCount())
                .leaveCount(item.getLeaveCount())
                .lateCount(item.getLateCount())
                .earlyLeaveCount(item.getEarlyLeaveCount())
                .absenceCardCount(item.getAbsenceCardCount())
                .absenceCount(item.getAbsenceCount())
                .busCount(item.getBusCount())
                .outCount(item.getOutCount())
                .overtimeCount(item.getOvertimeCount())
                .endDate(item.getEndDate())
                .build()).collect(Collectors.toList());
    }

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


    @Override
    public SystemNoticeStrategy setSystemNoticeStrategy(SettlementNoticeModel settlementNoticeModel, String noticeId) {
        ArrayList<JumpUrl> objects = CollUtil.newArrayList();
        objects.add(JumpUrl.builder().url(String.format(TEAM_MONTHLY_STATISTICS_URL1, noticeId))
                .urlName(TEAM_MONTHLY_STATISTICS_BUTTON_NAME1).color(COLOR_BLACK).build());
        objects.add(JumpUrl.builder().url(TEAM_MONTHLY_STATISTICS_URL2)
                .urlName(TEAM_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<TeamMonthSettlementModel> data) {
        TeamMonthSettlementModel teamMonthSettlementModel = data.stream().findFirst().orElse(null);
        StringBuilder stringBuffer = new StringBuilder();
        stringBuffer.append("考勤组：").append(teamMonthSettlementModel.getGroupName()).append("</br>");
        stringBuffer.append("出勤情况：").append("</br>");;
        getTipsList(teamMonthSettlementModel, stringBuffer);
        return stringBuffer.toString();
    }

    private void getTipsList(TeamMonthSettlementModel teamMonthSettlementModel, StringBuilder tips) {
        LinkedHashMap<String, Integer> map = Maps.newLinkedHashMap();
        if (teamMonthSettlementModel.getNormalCount() > 0) {
            map.put("正常", teamMonthSettlementModel.getNormalCount());
        }
        if (teamMonthSettlementModel.getLeaveCount() > 0) {
            map.put("请假", teamMonthSettlementModel.getLeaveCount());
        }
        if (teamMonthSettlementModel.getLateCount() > 0) {
            map.put("迟到", teamMonthSettlementModel.getLateCount());
        }
        if (teamMonthSettlementModel.getEarlyLeaveCount() > 0) {
            map.put("早退", teamMonthSettlementModel.getEarlyLeaveCount());
        }
        if (teamMonthSettlementModel.getAbsenceCardCount() > 0) {
            map.put("缺卡", teamMonthSettlementModel.getAbsenceCardCount());
        }
        if (teamMonthSettlementModel.getAbsenceCount() > 0) {
            map.put("旷工", teamMonthSettlementModel.getAbsenceCount());
        }
        if (teamMonthSettlementModel.getBusCount() > 0) {
            map.put("出差", teamMonthSettlementModel.getBusCount());
        }
        if (teamMonthSettlementModel.getOutCount() > 0) {
            map.put("外出", teamMonthSettlementModel.getOutCount());
        }
        if (teamMonthSettlementModel.getOvertimeCount() > 0) {
            map.put("加班", teamMonthSettlementModel.getOvertimeCount());
        }
        int i = 0;
        for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
            if (i > 1) {
                break;
            }
            tips.append(stringIntegerEntry.getKey()).append("：").append(stringIntegerEntry.getValue()).append("人</br>");
            i++;
        }
    }
}
