package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.framework.util.DataUtil;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.common.code.AttendSourceTypeEnum;
import com.mortals.xhx.common.code.TypeEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.module.attendance.model.AttendanceRecordHikEntity;
import com.mortals.xhx.module.attendance.model.AttendanceRecordHikQuery;
import com.mortals.xhx.module.attendance.service.AttendanceRecordHikService;
import com.mortals.xhx.module.attendance.service.work.FixWorkOtherAttendance;
import com.mortals.xhx.module.attendance.service.work.FreedomWorkAttendance;
import com.mortals.xhx.module.dingding.attendance.service.IDingAttenanceService;
import com.mortals.xhx.module.door.model.DoorEntity;
import com.mortals.xhx.module.door.model.DoorQuery;
import com.mortals.xhx.module.door.service.DoorService;
import com.mortals.xhx.module.hik.door.model.req.door.DoorEventReq;
import com.mortals.xhx.module.hik.door.model.req.door.HikDoorEventReq;
import com.mortals.xhx.module.hik.door.model.rsp.door.DoorEventDataInfo;
import com.mortals.xhx.module.hik.door.service.IHikDoorService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 同步获取当天海康门禁事件
 */
@Slf4j
@Service("SyncDoorsEventTask")
public class SyncDoorsEventTaskImpl implements ITaskExcuteService {

    @Autowired
    private AttendanceRecordHikService recordHikService;
    private Integer day;
    @Value("${hik.host:}")
    protected String hikhost;

    @Value("${source.type:''}")
    protected String sourceType;

    @Autowired
    private DoorService doorService;
    @Autowired
    private IDingAttenanceService dingAttenanceService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        //同步多久时间段的 偏移量为天 默认当天
        String excuteParam = task.getExcuteParam();
        if (!ObjectUtils.isEmpty(excuteParam)) {
            day = DataUtil.converStr2Int(excuteParam, 0);
        } else {
            day = 0;
        }
        Date beginDateTime = DateUtil.parseDateTime(DateUtil.today() + " 08:00:00");
        Date endDateTime = DateUtil.parseDateTime(DateUtil.today() + " 23:30:00");

        boolean in = DateUtil.isIn(new Date(), beginDateTime, endDateTime);
        if (in) {
            AttendanceRecordHikQuery hikQuery = new AttendanceRecordHikQuery();
            DateTime startTime = DateUtil.offsetDay(new Date(), day > 0 ? -day : 0);
            hikQuery.setAttendanceDateStart(startTime.toDateStr());
            // 获取当天的结束时间
            hikQuery.setAttendanceDateEnd(DateUtil.today());

            //todo 根据配置 判断是否海康门禁 钉钉考勤等
            if (AttendSourceTypeEnum.海康云平台.getValue().equals(sourceType)) {
                //海康云平台
                log.info("同步海康云平台门禁事件！");
                recordHikService.syncDoorEvents(hikQuery);
            } else if (AttendSourceTypeEnum.海康考勤机.getValue().equals(sourceType)) {
                //海康考勤机
                log.info("同步海康设备上门禁事件！");
                HikDoorEventReq hikDoorEventReq = new HikDoorEventReq();
                hikDoorEventReq.setStartTime(DateUtil.parseDate(startTime.toDateStr()).toJdkDate());
                hikDoorEventReq.setEndTime(DateUtil.endOfDay(new Date()));
                List<DoorEntity> doorEntities = doorService.find(new DoorQuery());
                for (DoorEntity doorEntity : doorEntities) {
                    doorService.syncDoorDeviceEvents(doorEntity, hikDoorEventReq);
                }
            } else if (AttendSourceTypeEnum.钉钉.getValue().equals(sourceType)) {
                //钉钉
                log.info("同步钉钉考勤事件！");
                recordHikService.syncDoorEventsFromDingTalk(hikQuery);

            }else {
                log.info("未配置考勤源类型！");
            }
/*            if (!ObjectUtils.isEmpty(hikhost)) {
                recordHikService.syncDoorEvents(hikQuery);
            } else {
                log.info("同步设备上门禁事件！");
                HikDoorEventReq hikDoorEventReq = new HikDoorEventReq();
                hikDoorEventReq.setStartTime(DateUtil.parseDate( startTime.toDateStr()).toJdkDate());
                hikDoorEventReq.setEndTime(DateUtil.endOfDay(new Date()));
                List<DoorEntity> doorEntities = doorService.find(new DoorQuery());
                for (DoorEntity doorEntity : doorEntities) {
                    doorService.syncDoorDeviceEvents(doorEntity, hikDoorEventReq);
                }
            }*/
        }
    }

    @Override
    public void stopTask(ITask task) throws AppException {

    }
}
