package com.sldlt.scheduled;

import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.sldlt.downloader.service.NAVPSDownloaderService;
import com.sldlt.downloader.service.TaskService;
import com.sldlt.navps.dto.FundDto;
import com.sldlt.navps.service.FundService;

@Component
public class TaskManager {

    private static Logger LOG = Logger.getLogger(TaskManager.class);

    @Autowired
    private TaskService taskService;

    @Autowired
    private FundService fundService;

    @Value("${navps.mindate}")
    private String minDateStr;

    @Autowired
    private NAVPSDownloaderService navpsDownloader;

    @Scheduled(fixedRate = 7200000)
    public void run() {
        LOG.info("regenerating task list");
        getFunds();
        setupPastTasks();
        setupCurrentTask();
    }

    private void getFunds() {
        navpsDownloader.findAvailableFunds().stream().forEach(fund -> fundService.saveFund(fund));
    }

    private void setupCurrentTask() {
        fundService.listAllFunds().forEach(fund -> {
            taskService.createTask(fund.getCode(), LocalDate.now(), LocalDate.now());
        });
    }

    private void setupPastTasks() {
        LocalDate minDate = LocalDate.parse(minDateStr);
        LocalDate dateFrom = LocalDate.now().with(WeekFields.ISO.dayOfWeek(), 1);
        LocalDate dateTo = LocalDate.now().with(WeekFields.ISO.dayOfWeek(), 7);
        List<FundDto> fundList = fundService.listAllFunds();

        while (dateFrom.isAfter(minDate)) {
            final LocalDate internalDateFrom = dateFrom = dateFrom.minusDays(7);
            final LocalDate internalDateTo = dateTo = dateTo.minusDays(7);
            fundList.forEach(fund -> {
                taskService.createTask(fund.getCode(), internalDateFrom, internalDateTo);
            });
        }

        LocalDate dateFromSingle = LocalDate.now().with(WeekFields.ISO.dayOfWeek(), 1);
        LocalDate dateToSingle = LocalDate.now().minusDays(1);

        while (!dateFromSingle.isAfter(dateToSingle)) {
            final LocalDate internalDateFrom = dateFromSingle;
            fundList.forEach(fund -> {
                taskService.createTask(fund.getCode(), internalDateFrom, internalDateFrom);
            });
            dateFromSingle = dateFromSingle.plusDays(1);
        }
    }
}

@@ -1067,36 +1067,8 @@
 ();%0A
-        setupCurrentTask();%0A
     
@@ -1232,19 +1232,17 @@
 etup
-Curren
+Pas
 tTask
+s
 () %7B
@@ -1245,24 +1245,49 @@
 () %7B%0A       
+ List%3CFundDto%3E fundList =
  fundService
@@ -1301,16 +1301,307 @@
 lFunds()
+;%0A%0A        LocalDate dateFromSingle = LocalDate.now().with(WeekFields.ISO.dayOfWeek(), 1);%0A        LocalDate dateToSingle = LocalDate.now().minusDays(1);%0A%0A        while (!dateToSingle.isBefore(dateFromSingle)) %7B%0A            final LocalDate internalDateTo = dateToSingle;%0A            fundList
 .forEach
@@ -1603,32 +1603,36 @@
 rEach(fund -%3E %7B%0A
+    
             task
@@ -1670,40 +1670,38 @@
 (), 
-LocalDate.now(), LocalDate.now()
+internalDateTo, internalDateTo
 );%0A 
@@ -1707,24 +1707,28 @@
         
+    
 %7D);%0A    
 %7D%0A%0A    p
@@ -1723,46 +1723,68 @@
     
-%7D%0A%0A
+  
     
-private void setupPastTasks() %7B
+  dateToSingle = dateToSingle.minusDays(1);%0A        %7D%0A
 %0A   
@@ -2002,69 +2002,8 @@
  7);
-%0A        List%3CFundDto%3E fundList = fundService.listAllFunds();
 %0A%0A  
@@ -2360,493 +2360,8 @@
  %7D%0A%0A
-        LocalDate dateFromSingle = LocalDate.now().with(WeekFields.ISO.dayOfWeek(), 1);%0A        LocalDate dateToSingle = LocalDate.now().minusDays(1);%0A%0A        while (!dateFromSingle.isAfter(dateToSingle)) %7B%0A            final LocalDate internalDateFrom = dateFromSingle;%0A            fundList.forEach(fund -%3E %7B%0A                taskService.createTask(fund.getCode(), internalDateFrom, internalDateFrom);%0A            %7D);%0A            dateFromSingle = dateFromSingle.plusDays(1);%0A        %7D%0A
     

remove current date from task refresh