package bj.pranie;

import bj.pranie.entity.User;
import bj.pranie.entity.myEnum.UserRole;
import bj.pranie.service.UserServiceImpl;
import bj.pranie.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;

/**
 * Created by Sebastian Sokolowski on 13.09.17.
 */
@SpringBootApplication
public class Application {
    static Logger log = Logger.getLogger(Application.class.getName());

    private final static int RESET_TIME = 10;

    @Autowired
    private UserServiceImpl userService;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @PostConstruct
    public void init() {
        startTimerToResetUsersTokens();
    }

    private void startTimerToResetUsersTokens() {
        Calendar nowCalendar = TimeUtil.getCalendar();

        Calendar nextReset = TimeUtil.getCalendar();
        nextReset.set(Calendar.HOUR_OF_DAY, RESET_TIME);
        nextReset.set(Calendar.MINUTE, 0);
        nextReset.set(Calendar.SECOND, 0);

        do {
            nextReset.add(Calendar.DAY_OF_WEEK, 1);
        } while (nextReset.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY);

        long delay = nextReset.getTimeInMillis() - nowCalendar.getTimeInMillis();

        float hourDelay = (delay / (float) (1000 * 60 * 60 * 24));
        log.info("Delay to start reset users tokens: " + hourDelay + " days.");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                resetUsersTokens();
            }
        }, delay);
    }

    private void resetUsersTokens() {
        Iterable<User> iterable = userService.findAll();
        Iterator<User> iterator = iterable.iterator();
        while (iterator.hasNext()) {
            User user = iterator.next();
            if (user.getRole() == UserRole.USER) {
                user.setTokens(2);
            }
        }

        userService.save(iterable);
    }
}

@@ -1103,25 +1103,24 @@
 Calendar();%0A
-%0A
         Cale
@@ -1140,312 +1140,25 @@
 t = 
-TimeUtil.getCalendar();%0A        nextReset.set(Calendar.HOUR_OF_DAY, RESET_TIME);%0A        nextReset.set(Calendar.MINUTE, 0);%0A        nextReset.set(Calendar.SECOND, 0);%0A%0A        do %7B%0A            nextReset.add(Calendar.DAY_OF_WEEK, 1);%0A        %7D while (nextReset.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY
+getNextResetTime(
 );%0A%0A
@@ -1557,39 +1557,798 @@
 ();%0A
-            %7D%0A        %7D, delay)
+%0A                startTimerToResetUsersTokens();%0A            %7D%0A        %7D, delay);%0A    %7D%0A%0A    private Calendar getNextResetTime() %7B%0A        Calendar nowCalendar = TimeUtil.getCalendar();%0A%0A        Calendar nextReset = TimeUtil.getCalendar();%0A        nextReset.set(Calendar.HOUR_OF_DAY, RESET_TIME);%0A        nextReset.set(Calendar.MINUTE, 0);%0A        nextReset.set(Calendar.SECOND, 0);%0A%0A%0A        if (nowCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY &&%0A                nowCalendar.get(Calendar.HOUR_OF_DAY) %3E= RESET_TIME %7C%7C%0A                nowCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) %7B%0A            do %7B%0A                nextReset.add(Calendar.DAY_OF_WEEK, 1);%0A            %7D while (nextReset.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY);%0A        %7D%0A%0A        return nextReset
 ;%0A  

Fix get next reset user token delay.