package jnpf.attendance.service.handle.notice;

import cn.hutool.core.collection.CollUtil;
import jnpf.constants.AttendanceConstant;
import jnpf.enums.attendance.AttendanceNoticeEnum;
import jnpf.enums.attendance.NoticeModuleEnum;
import jnpf.enums.attendance.UserSettingEnum;
import jnpf.model.attendance.model.FastClockInNoticeModel;
import jnpf.model.attendance.model.JumpUrl;
import jnpf.model.attendance.model.QuickClockInModel;
import jnpf.model.attendance.model.SystemNoticeStrategy;
import jnpf.util.ConstantUtil;
import jnpf.util.DateDetail;
import jnpf.util.StringUtil;
import org.springframework.stereotype.Service;

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

/**
 * 极速打卡消息
 *
 * @author yanwenfu
 * @create 2024-08-12
 */
@Service(value = AttendanceConstant.FAST_CLOCK_IN)
public class FastClockInNotice implements AttendanceNotice<QuickClockInModel, FastClockInNoticeModel> {

    @Resource
    private AttendanceNoticeHandler attendanceNoticeHandler;

    @Override
    public Boolean isPass(FastClockInNoticeModel model) {

        return attendanceNoticeHandler.checkBeforeCondition(model.getUserId(), UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE,
                UserSettingEnum.ATTENDANCE_SETTING_SPEED_CHECK_SUCCESS_REMINDER);
    }

    @Override
    public List<QuickClockInModel> generatingData(FastClockInNoticeModel model) {

        QuickClockInModel quickClockInModel = QuickClockInModel.builder()
                .workTimeStr(DateDetail.getDate2Str(model.getWorkTime(), DateDetail.DF9))
                .remark(getTitle(model.getAttendanceNoticeEnum()))
                .clockInTimeStr(DateDetail.getDate2Str(model.getClockIn().getClockInTime(), DateDetail.DF10))
                .clockInMethod(getClockInMethod(model.getClockIn().getDeviceType()))
                .clockInAddress(StringUtil.isEmpty(model.getClockIn().getAddress()) ? "--" : model.getClockIn().getAddress())
                .build();
        return List.of(quickClockInModel);
    }

    private String getClockInMethod(Integer deviceType) {

        switch (deviceType) {
            case ConstantUtil.DEVICE_PLACE:
                return "GPS地点打卡";
            case ConstantUtil.DEVICE_WIFI:
                return "WIFI打卡";
            case ConstantUtil.DEVICE_MACHINE:
                return "考勤机打卡";
            default:
                return "未知";
        }
    }

    @Override
    public List<String> getRecipient(FastClockInNoticeModel attendanceNoticeModel) {

        return CollUtil.newArrayList(attendanceNoticeModel.getUserId());
    }

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

        List<JumpUrl> list = new ArrayList<>();
        list.add(new JumpUrl(AttendanceConstant.BTN_FAST_DETAIL_URL, AttendanceConstant.BTN_NAME_TO_DETAIL, AttendanceConstant.COLOR_BLACK));
        list.add(new JumpUrl(AttendanceConstant.BTN_FAST_UPDATE_URL, AttendanceConstant.BTN_NAME_FAST_UPDATE, AttendanceConstant.COLOR_BLUE));

        return SystemNoticeStrategy.builder()
                .title(getTitle(model.getAttendanceNoticeEnum()))
                .noticeModuleEnum(NoticeModuleEnum.KQ_DK)
                .jumpUrls(list)
                .build();
    }

    private String getTitle(AttendanceNoticeEnum attendanceNoticeEnum) {

        String title = "";
        if (AttendanceNoticeEnum.CHECK_IN_QUICK.equals(attendanceNoticeEnum)) {
            title = AttendanceConstant.ON_WORK;
        }
        if (AttendanceNoticeEnum.CHECK_OUT_QUICK.equals(attendanceNoticeEnum)) {
            title = AttendanceConstant.OFF_WORK;
        }
        return String.format(AttendanceConstant.TITLE_FAST, title);
    }

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

        if (null == dataList || dataList.isEmpty()) {
            return "";
        }
        QuickClockInModel model = dataList.get(0);
        return model.getWorkTimeStr() + model.getRemark()+ "!" + "</br>" +
                "打卡时间：" + model.getClockInTimeStr() + "</br>" +
                "考勤方式：" + model.getClockInMethod() + "</br>" +
                "考勤点：" + model.getClockInAddress();
    }
}
