package jnpf.attendance.service.handle.notice;

import cn.hutool.core.collection.CollUtil;
import jnpf.attendance.antifreeze.UserAntifreeze;
import jnpf.attendance.service.AttendanceGroupService;
import jnpf.base.UserInfo;
import jnpf.constants.AttendanceConstant;
import jnpf.entity.AttendanceGroup;
import jnpf.enums.attendance.AttendanceNoticeEnum;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.model.attendance.model.SystemNoticeStrategy;
import jnpf.model.attendance.model.SystemTypeChangeModel;
import jnpf.model.attendance.model.SystemTypeChangeNoticeModel;
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 java.util.List;
import java.util.Objects;

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_SHIFT_SYSTEM)
public class SystemTypeChangeAttendanceNotice implements AttendanceNotice<SystemTypeChangeModel, SystemTypeChangeNoticeModel> {

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


    @Override
    public Boolean isPass(SystemTypeChangeNoticeModel attendanceNoticeModel) {
        if (!attendanceNoticeModel.getAttendanceNoticeEnum().equals(AttendanceNoticeEnum.RULE_CHANGE_SHIFT_SYSTEM)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Override
    public List<SystemTypeChangeModel> generatingData(SystemTypeChangeNoticeModel attendanceNoticeModel) {
        AttendanceGroup byId = attendanceGroupService.getById(attendanceNoticeModel.getGroupId());
        return CollUtil.newArrayList(SystemTypeChangeModel.builder()
                .groupName(byId.getGroupName())
                .oldSystemType(Objects.equals(attendanceNoticeModel.getSystemType(), 2) ? "固定班制" : "排班制")
                .newSystemType(Objects.equals(attendanceNoticeModel.getSystemType(), 1) ? "固定班制" : "排班制")
                .build());
    }

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

    @Override
    public SystemNoticeStrategy setSystemNoticeStrategy(SystemTypeChangeNoticeModel 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<SystemTypeChangeModel> 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();
        SystemTypeChangeModel systemTypeChangeModel = data.stream().findFirst().orElse(null);
        stringBuffer.append("考勤组：").append(systemTypeChangeModel.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("考勤组类型已由").append(systemTypeChangeModel.getOldSystemType()).append("改为").append(systemTypeChangeModel.getNewSystemType());
        return stringBuffer.toString();
    }

}
