package jnpf.attendance.service.handle.notice;

import cn.hutool.core.collection.CollUtil;
import jnpf.attendance.antifreeze.UserAntifreeze;
import jnpf.attendance.mapper.AttendanceManagerPermissionMapper;
import jnpf.attendance.service.AttendanceGroupService;
import jnpf.attendance.service.AttendanceUserService;
import jnpf.base.UserInfo;
import jnpf.constants.AttendanceConstant;
import jnpf.entity.AttendanceGroup;
import jnpf.entity.AttendanceGroupUser;
import jnpf.enums.attendance.AttendanceNoticeEnum;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.enums.attendance.UserSettingEnum;
import jnpf.model.attendance.model.RuleChangeModel;
import jnpf.model.attendance.model.RuleChangeNoticeModel;
import jnpf.model.attendance.model.SystemNoticeStrategy;
import jnpf.model.attendance.vo.AttendanceGroupAdminVo;
import jnpf.permission.model.user.PartUserInfoVo;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

import static jnpf.constants.AttendanceConstant.ATTENDANCE_NOTICE_ADMIN_RULE_CHANGE_TITLE;
import static jnpf.constants.AttendanceConstant.ATTENDANCE_NOTICE_RULE_CHANGE_TITLE;

@Component(AttendanceConstant.RULE_CHANGE_RATIO)
public class RuleChangeAttendanceNotice implements AttendanceNotice<RuleChangeModel, RuleChangeNoticeModel> {

    @Autowired
    private AttendanceGroupService attendanceGroupService;
    @Autowired
    private AttendanceNoticeHandler attendanceNoticeHandler;
    @Autowired
    private UserAntifreeze userAntifreeze;

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

    @Override
    public List<RuleChangeModel> generatingData(RuleChangeNoticeModel attendanceNoticeModel) {
        String tips = "";
        if (Objects.equals(attendanceNoticeModel.getAttendanceNoticeEnum(), AttendanceNoticeEnum.RULE_CHANGE_RATIO)) {
            tips = String.format("出勤换算比由%s小时/天改为%s小时/天", attendanceNoticeModel.getOldValue(), attendanceNoticeModel.getNewValue());
        }
        if (Objects.equals(attendanceNoticeModel.getAttendanceNoticeEnum(), AttendanceNoticeEnum.RULE_CHANGE_FIELD)) {
            tips = String.format("外勤打卡已调整为%s外勤打卡", Objects.equals(attendanceNoticeModel.getNewValue(), 1) ? "允许" : "不允许");
        }
        if (Objects.equals(attendanceNoticeModel.getAttendanceNoticeEnum(), AttendanceNoticeEnum.RULE_CHANGE_CARD_MAKEUP)) {
            tips = String.format("补卡规则已调整为%s补卡", Objects.equals(attendanceNoticeModel.getNewValue(), 1) ? "允许" : "不允许");
        }
        if (Objects.equals(attendanceNoticeModel.getAttendanceNoticeEnum(), AttendanceNoticeEnum.RULE_CHANGE_GPS_SWIFT)) {
            tips = String.format("考勤方式已%s使用GPS地点打卡", Objects.equals(attendanceNoticeModel.getNewValue(), 1) ? "允许" : "禁止");
            if (Objects.equals(attendanceNoticeModel.getNewValue(), 1)) {
                StringBuilder sb = new StringBuilder("</br><span style='font-weight: bold;'>可使用考勤点：</span>").append("</br>");
                if (CollUtil.isNotEmpty(attendanceNoticeModel.getAddressList())) {
                    sb.append(String.join("</br>", attendanceNoticeModel.getAddressList()));
                }
                tips += sb.toString();
            }
        }
        if (Objects.equals(attendanceNoticeModel.getAttendanceNoticeEnum(), AttendanceNoticeEnum.RULE_CHANGE_WIFI_SWIFT)) {
            tips = String.format("考勤方式已%s使用WIFI打卡", Objects.equals(attendanceNoticeModel.getNewValue(), 1) ? "允许" : "不允许");
            if (Objects.equals(attendanceNoticeModel.getNewValue(), 1)) {
                StringBuilder sb = new StringBuilder("</br><span style='font-weight: bold;'>可使用考勤点：</span>").append("</br>");
                if (CollUtil.isNotEmpty(attendanceNoticeModel.getAddressList())) {
                    sb.append(String.join("、", attendanceNoticeModel.getAddressList()));
                }
                tips += sb.toString();
            }
        }
        if (Objects.equals(attendanceNoticeModel.getAttendanceNoticeEnum(), AttendanceNoticeEnum.RULE_CHANGE_KQJ_SWIFT)) {
            tips = String.format("考勤方式已%s使用考勤机打卡", Objects.equals(attendanceNoticeModel.getNewValue(), 1) ? "允许" : "不允许");
            if (Objects.equals(attendanceNoticeModel.getNewValue(), 1)) {
                StringBuilder sb = new StringBuilder("</br><span style='font-weight: bold;'>可使用考勤点：</span>").append("</br>");
                if (CollUtil.isNotEmpty(attendanceNoticeModel.getAddressList())) {
                    sb.append(String.join("、", attendanceNoticeModel.getAddressList()));
                }
                tips += sb.toString();
            }
        }
        AttendanceGroup byId = attendanceGroupService.getById(attendanceNoticeModel.getGroupId());
        return CollUtil.newArrayList(RuleChangeModel.builder()
                .groupName(byId.getGroupName())
                .tips(tips)
                .build());
    }

    @Override
    public List<String> getRecipient(RuleChangeNoticeModel attendanceNoticeModel) {
        return attendanceNoticeHandler.getRecipientForSelfAndChild(attendanceNoticeModel.getGroupId(), attendanceNoticeModel.getIsAdmin());
    }

    @Override
    public SystemNoticeStrategy setSystemNoticeStrategy(RuleChangeNoticeModel attendanceNoticeModel, String noticeId) {
        return SystemNoticeStrategy.builder()
                .noticeModuleEnum(NoticeModuleEnum.KQZ_BD)
                .title(attendanceNoticeModel.getIsAdmin() ? ATTENDANCE_NOTICE_ADMIN_RULE_CHANGE_TITLE : ATTENDANCE_NOTICE_RULE_CHANGE_TITLE)
                .isConfirm(Boolean.TRUE)
                .build();
    }

    @Override
    public String getContext(List<RuleChangeModel> data) {
        if (CollUtil.isEmpty(data)) {
            return null;
        }
        UserInfo user = UserProvider.getUser();
        List<PartUserInfoVo> allByIds = userAntifreeze.getAllByIds(CollUtil.newArrayList(user.getUserId()), user.getTenantId());
        PartUserInfoVo partUserInfoVo = allByIds.stream().findFirst().orElse(null);
        StringBuffer stringBuffer = new StringBuffer();
        RuleChangeModel ruleChangeModel = data.stream().findFirst().orElse(null);
        stringBuffer.append("考勤组：").append(ruleChangeModel.getGroupName()).append("</br>");
        if (Objects.isNull(partUserInfoVo)) {
            stringBuffer.append("操作人：无</br>");
        } else {
            stringBuffer.append("操作人：").append(partUserInfoVo.getRealName()).append("(").append(StringUtil.isEmpty(partUserInfoVo.getOrganizeName()) ? "暂无" : partUserInfoVo.getOrganizeName()).append("-").append(StringUtil.isEmpty(partUserInfoVo.getPositionName()) ? "暂无" : partUserInfoVo.getPositionName()).append(")</br>");
        }
        stringBuffer.append(ruleChangeModel.getTips());
        return stringBuffer.toString();
    }

}
