package jnpf.attendance.service.handle.notice;

import jnpf.constants.AttendanceConstant;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.enums.attendance.UserSettingEnum;
import jnpf.model.attendance.model.BeforeClockInModel;
import jnpf.model.attendance.model.JumpUrl;
import jnpf.model.attendance.model.RemindClockInNoticeModel;
import jnpf.model.attendance.model.SystemNoticeStrategy;
import jnpf.util.ConstantUtil;
import jnpf.util.DateDetail;
import org.springframework.stereotype.Service;

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

/**
 * 打卡前提醒
 *
 * @author yanwenfu
 * @create 2024-08-15
 */
@Service(value = AttendanceConstant.BEFORE_CLOCK_IN)
public class BeforeClockInNotice implements AttendanceNotice<BeforeClockInModel, RemindClockInNoticeModel> {

    @Resource
    private AttendanceNoticeHandler attendanceNoticeHandler;

    @Override
    public Boolean isPass(RemindClockInNoticeModel model) {

        if (ConstantUtil.ON_WORK.equals(model.getClockInType())) {
            return attendanceNoticeHandler.checkBeforeCondition(model.getUserId(), UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE,
                    UserSettingEnum.ATTENDANCE_SETTING_PRE_WORK_REMINDER);
        } else {
            return attendanceNoticeHandler.checkBeforeCondition(model.getUserId(), UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE,
                    UserSettingEnum.ATTENDANCE_SETTING_END_WORK_REMINDER);
        }
    }

    @Override
    public List<BeforeClockInModel> generatingData(RemindClockInNoticeModel model) {

        BeforeClockInModel beforeClockInModel = BeforeClockInModel.builder()
                .workTimeStr(null == model.getWorkTime() ? "--" : DateDetail.getDate2Str(model.getWorkTime(), DateDetail.DF9))
                .remark(ConstantUtil.ON_WORK.equals(model.getClockInType()) ? AttendanceConstant.TITLE_BEFORE_ON_WORK : AttendanceConstant.TITLE_BEFORE_OFF_WORK)
                .clockInType(model.getClockInType())
                .build();
        return List.of(beforeClockInModel);
    }

    @Override
    public List<String> getRecipient(RemindClockInNoticeModel model) {

        return List.of(model.getUserId());
    }

    @Override
    public SystemNoticeStrategy setSystemNoticeStrategy(RemindClockInNoticeModel model, String noticeId) {

        List<JumpUrl> list = new ArrayList<>();
        list.add(new JumpUrl(AttendanceConstant.BTN_RULE_URL + "?day=" + DateDetail.getDate2Str(new Date(), DateDetail.DF), AttendanceConstant.BTN_NAME_TO_DETAIL, AttendanceConstant.COLOR_BLACK));
        list.add(new JumpUrl(AttendanceConstant.BTN_FAST_UPDATE_URL, AttendanceConstant.BTN_NAME_GO, AttendanceConstant.COLOR_BLUE));

        return SystemNoticeStrategy.builder()
                .title(ConstantUtil.ON_WORK.equals(model.getClockInType()) ? AttendanceConstant.TITLE_BEFORE_ON_WORK : AttendanceConstant.TITLE_BEFORE_OFF_WORK)
                .noticeModuleEnum(NoticeModuleEnum.KQ_DK)
                .jumpUrls(list)
                .build();
    }

    @Override
    public String getContext(List<BeforeClockInModel> dataList) {

        if (null == dataList || dataList.isEmpty()) {
            return "";
        }
        BeforeClockInModel model = dataList.get(0);
        if (ConstantUtil.ON_WORK.equals(model.getClockInType())) {
            return model.getWorkTimeStr() + " " + AttendanceConstant.TITLE_BEFORE_ON_WORK + "!";
        } else {
            return model.getWorkTimeStr() + " " + AttendanceConstant.TITLE_BEFORE_OFF_WORK + "!";
        }
    }
}
