package edu.fiu;

import edu.fiu.models.Session;
import edu.fiu.models.User;
import edu.fiu.repositories.SessionRepository;
import edu.fiu.repositories.UserRepository;

import org.jdbi.v3.core.Handle;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class CleanUp {


    /*
    Removes old sessions. 
    */
    public static void cleanSessions(Handle h) {


        final long sessionAliveTime = 15; //minutes
        SessionRepository sessions = h.attach(SessionRepository.class);

        try {
            List<Session> sessionRecords = sessions.list();
            //System.out.println("Checking Sessions....");

            for (Session currentSession : sessionRecords) {

                // dateTime of Token
                String stringTokenTime = currentSession.getDateTime();
                DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                LocalDateTime tokenTime = LocalDateTime.parse(stringTokenTime, format);

                // current dateTime
                LocalDateTime now = LocalDateTime.now();

                // difference between two times
                Duration gap = Duration.between(tokenTime, now);

                if (gap.toMinutes() > sessionAliveTime) {
                    System.out.println("Old Sessions Removed");
                    sessions.removeSession(currentSession.getToken());
                }
            }

        } catch (Exception e) {
            System.out.println("Unable to Remove Old Sessions.");
        }

    }

    /*
    If the user is showns as logged in, 
    but there are no sessions for that user,
    mark user as logged off. 
    */
    public static void cleanLogIns(Handle h) {

        UserRepository users = h.attach(UserRepository.class);
        SessionRepository sessions = h.attach(SessionRepository.class);

        try {
            //System.out.println("Checking User Statuses....");
            List<User> userRecords = users.list();
            List<Session> sessionRecords = sessions.list();

            for (User currentUser : userRecords) {

                if (currentUser.getActive() == 1) {

                    boolean hasSession = false;

                    for (Session currentSession : sessionRecords) {

                        if (hasSession == true) {
                            break;
                        }

                        List<Integer> userIDs = currentSession.getUserIds();
                        for (Integer currentID : userIDs) {
                            if (currentID == currentUser.getId()) {
                                hasSession = true;
                                break;
                            }
                        }
                    }

                    if (hasSession == false) {
                        System.out.println("Marking user as logged off due to missing token.");
                        users.markInactive(currentUser.getId());
                    }
                }
            }

        } catch (Exception e) {
            System.out.println("Unable to Update User Statuses");
        }

    }
}
