package com.example.quiz.utils;

import android.content.Context;
import android.os.Bundle;

import java.time.Duration;
import java.time.LocalDateTime;

public class SessionManager {
    // 10 minutes en millisecondes
    private static final long sessionExpiredTime =   10 * 60 * 1000;

    private static SessionManager instance;

    private LocalDateTime lastExpiredDate;

    public SessionManager(Context context) {
        if(lastExpiredDate != null){
            updateLastExpiredDate();
        }
    }

    public static SessionManager newInstance(Context context) {
        if(instance == null){
            instance = new SessionManager(context.getApplicationContext());
        }
        return instance;
    }

    public void updateLastExpiredDate() {
        lastExpiredDate = LocalDateTime.now();
    }

    public void setToExpire(){
        lastExpiredDate = null;
    }

    public boolean isExpired() {
        if (lastExpiredDate == null) {
            return true;
        }
        // Vérifie si le temps écoulé depuis lastExpiredDate > sessionExpiredTime
        Duration elapsed = Duration.between(lastExpiredDate, LocalDateTime.now());
        return elapsed.toMillis() > sessionExpiredTime;
    }
}
