package dev.micle.loginprotection.data;

import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

public class ProtectedPlayer {
    // Initialize variables
    private final UUID playerUUID;
    private final Timer timer = new Timer();
    private TimerTask afkTimerTask, gracePeriodTimerTask;
    private State state;

    /**
     * Constructor for a ProtectedPlayer.
     * @param playerUUID UUID of player to use.
     */
    public ProtectedPlayer(UUID playerUUID, State state) {
        this.playerUUID = playerUUID;
        this.state = state;
    }

    /**
     * @return UUID of player,
     */
    public UUID getPlayerUUID() {
        return playerUUID;
    }

    public Timer getTimer() {
        return timer;
    }

    public void setGracePeriodTimerTask(TimerTask gracePeriodTimerTask, long delay) {
        if (this.gracePeriodTimerTask != null) {
            this.gracePeriodTimerTask.cancel();
        }
        if (gracePeriodTimerTask != null) {
            this.gracePeriodTimerTask = gracePeriodTimerTask;
            timer.schedule(this.gracePeriodTimerTask, delay);
        }
    }

    public void setAfkTimerTask(TimerTask afkTimerTask, long delay) {
        if (this.afkTimerTask != null) {
            this.afkTimerTask.cancel();
        }
        if (afkTimerTask != null) {
            this.afkTimerTask = afkTimerTask;
            timer.schedule(this.afkTimerTask, delay);
        }
    }

    /**
     * @return Current state of the player.
     */
    public State getState() {
        return state;
    }

    /**
     * Set the state of the player.
     * @param state Player's new state.
     */
    public void setState(State state) {
        this.state = state;
    }

    public enum State {
        JOINING,
        AFK,
        ACTIVE,
        LOGIN_GRACE,
        AFK_GRACE
    }
}
