package com.ycwl.basic.task;

import cn.hutool.core.date.DateUtil;
import com.ycwl.basic.biz.TemplateBiz;
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
import com.ycwl.basic.mapper.FaceMapper;
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.integration.common.manager.ScenicConfigManager;
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
import com.ycwl.basic.model.pc.template.resp.TemplateRespVO;
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.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 TemplateBiz templateBiz;
    @Autowired
    private TaskTaskServiceImpl taskTaskService;
    @Autowired
    private TemplateMapper templateMapper;
    @Autowired
    private ScenicRepository scenicRepository;
    @Autowired
    private FaceService faceService;
    @Autowired
    private TemplateRepository templateRepository;

    @Scheduled(cron = "0 0 * * * *")
    public void generateVideoTask() {
        ScenicReqQuery query = new ScenicReqQuery();
        query.setPageSize(1000);
        List<ScenicV2DTO> scenicList = scenicRepository.list(query);
        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 = Long.valueOf(scenic.getId());
            ScenicConfigManager scenicConfig = scenicRepository.getScenicConfigManager(scenicId);
            if (scenicConfig == null) {
                log.info("当前景区{}，无配置信息", scenic.getName());
                return;
            }
            if (Integer.valueOf(1).equals(scenicConfig.getInteger("book_routine")) || Integer.valueOf(3).equals(scenicConfig.getInteger("book_routine"))) {
                Integer hour = scenicConfig.getInteger("force_finish_time");
                if (hour != currentHour) {
                    return;
                }
                // 定时逻辑
                List<TemplateRespVO> templateList = templateRepository.getTemplateListByScenicId(scenicId);
                if (templateList.isEmpty()) {
                    return;
                }
                FaceReqQuery faceReqQuery = new FaceReqQuery();
                faceReqQuery.setScenicId(scenicId);
                faceReqQuery.setStartTime(DateUtil.beginOfDay(new Date()));
                faceReqQuery.setEndTime(DateUtil.endOfDay(new Date()));
                List<FaceRespVO> list = faceMapper.list(faceReqQuery);
                list.forEach(face -> {
                    faceService.matchFaceId(face.getId(), false, null);
                    if (Integer.valueOf(3).equals(scenicConfig.getInteger("book_routine"))) {
                        // 全部生成
                        templateList.forEach(content -> {
                            Long templateId = content.getId();
                            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 = templateList.getFirst().getId();
                        boolean canAutoGenerate = templateBiz.determineTemplateCanAutoGenerate(templateId, face.getId(), false);
                        if (canAutoGenerate) {
                            log.info("task callback: 自动生成");
                            taskTaskService.forceCreateTaskByFaceIdAndTempalteId(face.getId(), templateId);
                        } else {
                            log.info("task callback: 不自动生成");
                        }
                    }
                    try {
                        Thread.sleep(2000L);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                });
            }
        });
    }
}
