package com.ycwl.basic.task;

import cn.hutool.core.date.DateUtil;
import com.ycwl.basic.biz.TemplateBiz;
import com.ycwl.basic.mapper.FaceMapper;
import com.ycwl.basic.mapper.FaceSampleMapper;
import com.ycwl.basic.mapper.ScenicMapper;
import com.ycwl.basic.mapper.TemplateMapper;
import com.ycwl.basic.model.mobile.scenic.content.ContentPageVO;
import com.ycwl.basic.model.pc.face.req.FaceReqQuery;
import com.ycwl.basic.model.pc.face.resp.FaceRespVO;
import com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity;
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
import com.ycwl.basic.model.pc.scenic.resp.ScenicRespVO;
import com.ycwl.basic.model.task.resp.SearchFaceRespVo;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.repository.TemplateRepository;
import com.ycwl.basic.service.pc.FaceService;
import com.ycwl.basic.service.task.TaskFaceService;
import com.ycwl.basic.service.task.impl.TaskTaskServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@EnableScheduling
@Component
@Profile("prod")
public class VideoTaskGenerator {
    @Autowired
    private FaceMapper faceMapper;
    @Autowired
    private TaskFaceService taskFaceService;
    @Autowired
    private TemplateBiz templateBiz;
    @Autowired
    private TaskTaskServiceImpl taskTaskService;
    @Autowired
    private TemplateMapper templateMapper;
    @Autowired
    private ScenicMapper scenicMapper;
    @Autowired
    private ScenicRepository scenicRepository;
    @Autowired
    private FaceService faceService;

    @Scheduled(cron = "0 0 * * * *")
    public void generateVideoTask() {
        List<ScenicRespVO> scenicList = scenicMapper.list(new ScenicReqQuery());
        if (scenicList.isEmpty()) {
            return;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        calendar.clear();
        scenicList.parallelStream().forEach(scenic -> {
            Long scenicId = scenic.getId();
            ScenicConfigEntity scenicConfig = scenicRepository.getScenicConfig(scenicId);
            if (scenicConfig == null) {
                log.info("当前景区{}，无配置信息", scenic.getName());
                return;
            }
            if (Integer.valueOf(1).equals(scenicConfig.getBookRoutine()) || Integer.valueOf(3).equals(scenicConfig.getBookRoutine())) {
                Integer hour = scenicConfig.getForceFinishTime();
                if (hour != currentHour) {
                    return;
                }
                // 定时逻辑
                List<ContentPageVO> contentList = templateMapper.listFor(scenicId);
                if (contentList.isEmpty()) {
                    return;
                }
                FaceReqQuery query = new FaceReqQuery();
                query.setScenicId(scenicId);
                query.setStartTime(DateUtil.beginOfDay(new Date()));
                query.setEndTime(DateUtil.endOfDay(new Date()));
                List<FaceRespVO> list = faceMapper.list(query);
                list.forEach(face -> {
                    faceService.matchFaceId(face.getId(), false);
                    if (Integer.valueOf(3).equals(scenicConfig.getBookRoutine())) {
                        // 全部生成
                        contentList.forEach(content -> {
                            Long templateId = content.getTemplateId();
                            boolean canAutoGenerate = templateBiz.determineTemplateCanAutoGenerate(templateId, face.getId(), false);
                            if (canAutoGenerate) {
                                log.info("task callback: 自动生成");
                                taskTaskService.forceCreateTaskByFaceIdAndTempalteId(face.getId(), templateId);
                            } else {
                                log.info("task callback: 不自动生成");
                            }
                        });
                    } else {
                        Long templateId = contentList.getFirst().getTemplateId();
                        boolean canAutoGenerate = templateBiz.determineTemplateCanAutoGenerate(templateId, face.getId(), false);
                        if (canAutoGenerate) {
                            log.info("task callback: 自动生成");
                            taskTaskService.forceCreateTaskByFaceIdAndTempalteId(face.getId(), templateId);
                        } else {
                            log.info("task callback: 不自动生成");
                        }
                    }
                });
            }
        });
    }
}
