package com.caliverse.admin.scheduler.polling;

import com.caliverse.admin.scheduler.SchedulerManager;
import com.caliverse.admin.scheduler.entity.SchedulerType;
import com.caliverse.admin.domain.RabbitMq.MessageHandlerService;
import com.caliverse.admin.domain.service.*;
import com.caliverse.admin.global.common.utils.ExcelUtils;
import com.caliverse.admin.logs.logservice.indicators.*;
import com.caliverse.admin.redis.service.RedisUserInfoService;
import com.caliverse.admin.scheduler.DynamicScheduler;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.batch.core.JobParameters;
// import org.springframework.batch.core.JobParametersBuilder;
// import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
@EnableScheduling
@RequiredArgsConstructor
public class ScheduleRunnerPolling {
    @Autowired private IndicatorsAuService auService;
    @Autowired private IndicatorsDauService dauService;
    @Autowired private IndicatorsWauService wauService;
    @Autowired private IndicatorsMauService mauService;
    @Autowired private IndicatorsMcuService mcuService;
    @Autowired private IndicatorsNruService statNruService;

    @Autowired private RedisUserInfoService userInfoService;
    @Autowired private DynamicScheduler dynamicScheduler;
    @Autowired private MailService mailService;
    @Autowired private NoticeService noticeService;
    @Autowired private BlackListService blackListService;
    @Autowired private EventService eventService;
    @Autowired
    private RedisUserInfoService redisUserInfoService;
    @Autowired
    private MessageHandlerService messageHandlerService;
    @Autowired
    private ExcelUtils excelUtils;

    private final SchedulerManager schedulerManager;

    @Scheduled(cron = "7 * * * * *")
    public void landOwnerChangesJob(){
        schedulerManager.executeScheduler(SchedulerType.LAND_OWNER_CHANGES);
    }

    /*
        매일 UTC 기준 00시 50분 00초에 실행, (한국 시간 9시 50분) 30분에 돌릴경우 데이터가 다 넘어 오지 않는 경우 있어서 수정처리
        이게 가장 먼저 실행 되어야 한다.
        로그가 많을 경우 성능 이슈 있을 수 있음
    */
//    @Scheduled(cron = "* * * * * *")
//    public void auScheduler() {
//        //이걸 나중에 어떻게 활용할지 생각해보자.
//        log.info("run auScheduler");
//        StartEndTime startEndTime = LogServiceHelper.getCurrentLogSearchEndTime(AdminConstants.STAT_DAY_NUM);
//        auService.collectActiveUser(startEndTime.getStartTime(), startEndTime.getEndTime());
//        log.info("end auScheduler");
//    }

////    @Scheduled(cron = "00 55 0 * * *") // 매일 UTC 기준 00시 56분 00초에 실행
//    @Scheduled(cron = "1 * * * * *") // 매일 UTC 기준 00시 56분 00초에 실행
//    public void dauScheduler() {
//        log.info("run dauScheduler");
//        StartEndTime startEndTime = LogServiceHelper.getCurrentLogSearchEndTime(AdminConstants.STAT_DAY_NUM);
//        dauService.collectDailyActiveUser(startEndTime.getStartTime(), startEndTime.getEndTime());
//        log.info("end dauScheduler");
//    }
//
//    @Scheduled(cron = "00 56 0 * * *") // 매일 UTC 기준 00시 56분 00초에 실행
//    public void wauScheduler() {
//        log.info("run wauScheduler");
//        StartEndTime startEndTime = LogServiceHelper.getCurrentLogSearchEndTime(AdminConstants.STAT_WEEK_NUM);
//        wauService.collectWeeklyActiveUser(startEndTime.getStartTime(), startEndTime.getEndTime());
//        log.info("end wauScheduler");
//    }
//
//    @Scheduled(cron = "00 57 0 * * *") // 매일 UTC 기준 00시 57분 00초에 실행
//    public void mauScheduler() {
//        log.info("run mauScheduler");
//        StartEndTime startEndTime = LogServiceHelper.getCurrentLogSearchEndTime(AdminConstants.STAT_MONTH_NUM);
//        mauService.collectMonthlyActiveUser(startEndTime.getStartTime(), startEndTime.getEndTime());
//        log.info("end mauScheduler");
//    }
//
//    @Scheduled(cron = "00 58 0 * * *") // 매일 UTC 기준 00시 58분 00초에 실행
//    public void mcuScheduler() {
//        log.info("run mcuScheduler");
//        StartEndTime startEndTime = LogServiceHelper.getCurrentLogSearchEndTime(AdminConstants.STAT_DAY_NUM);
//        mcuService.collectMaxCountUser(startEndTime.getStartTime(), startEndTime.getEndTime());
//        log.info("end mcuScheduler");
//    }
//
//    @Scheduled(cron = "00 59 0 * * *") // 매일 UTC 기준 00시 59분 00초에 실행
//    public void nruScheduler() {
//        log.info("run mcuScheduler");
//        //StartEndTime startEndTime = LogServiceHelper.getCurrentLogSearchEndTime(AdminConstants.STAT_DAY_NUM);
//        //statNruService.collectStatLogs(startEndTime.getStartTime(), startEndTime.getEndTime());
//        log.info("end mcuScheduler");
//    }

//    @Scheduled(cron = "1 * * * * *")
//    public void noticeJob(){
//        log.info("run noticeJob");
//        List<InGame> noticeList = noticeService.getScheduleNoticeList();
//        noticeList.forEach(notice -> {
//            if (notice.getIsRepeat() &&
//                    notice.getRepeatType() == InGame.REPEATTYPE.COUNT &&
//                    notice.getSendCnt() >= notice.getRepeatCnt()) {
//                log.info("Skipping notice - already reached max count. NoticeId: {}", notice.getId());
//                noticeService.updateNoticeStatus(notice.getId(), InGame.SENDSTATUS.FINISH);
//                return;
//            }
//
//            NoticeScheduler task = NoticeScheduler.builder()
//                    .notice(notice)
//                    .noticeService(noticeService)
//                    .redisUserInfoService(redisUserInfoService)
//                    .messageHandlerService(messageHandlerService)
//                    .build();
//            schedulerManager.scheduleTask(task);
//        });
//        log.info("end noticeJob");
//    }
//
//    @Scheduled(cron = "2 * * * * *")
//    public void mailJob(){
//        log.info("run mailJob");
//        List<Mail> mailList = mailService.getScheduleMailList();
//        mailList.stream()
//                .filter(mail -> mail.getSendStatus().equals(Mail.SENDSTATUS.WAIT))
//                .forEach(mail -> {
//                    MailScheduler task = MailScheduler.builder()
//                            .mail(mail)
//                            .mailService(mailService)
//                            .redisUserInfoService(redisUserInfoService)
//                            .messageHandlerService(messageHandlerService)
//                            .excelUtils(excelUtils)
//                            .build();
//                    schedulerManager.scheduleTask(task);
//                });
//        log.info("end mailJob");
//    }
//
//    @Scheduled(cron = "3 * * * * *")
//    public void blackListJob(){
//        log.info("run blackListJob");
//        List<BlackList> blackList = blackListService.getScheduleBlackList();
//        blackList.forEach(blockUser -> {
//            BlackListScheduler task = BlackListScheduler.builder()
//                    .blackList(blockUser)
//                    .blackListService(blackListService)
//                    .build();
//            schedulerManager.scheduleTask(task);
//        });
//        log.info("end blackListJob");
//    }
//
//    @Scheduled(cron = "4 * * * * *")
//    public void eventJob(){
//        log.info("run eventJob");
//        List<Event> eventList = eventService.getScheduleMailList();
//        eventList.forEach(event -> {
//            EventScheduler task = EventScheduler.builder()
//                    .event(event)
//                    .eventService(eventService)
//                    .redisUserInfoService(redisUserInfoService)
//                    .messageHandlerService(messageHandlerService)
//                    .build();
//            schedulerManager.scheduleTask(task);
//        });
//        log.info("end eventJob");
//    }
}
