package de.winniepat.citrus.beta;

import de.winniepat.citrus.Client;
import de.winniepat.citrus.managers.ConfigManager;
import de.winniepat.citrus.ModConfig;
import net.minecraft.client.MinecraftClient;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class BetaManager {
    private static BetaManager instance;

    private final VerificationClient verificationClient;
    private boolean isVerified = false;
    private ScheduledExecutorService scheduler;

    private BetaManager() {
        this.verificationClient = VerificationClient.getInstance();
        setupPeriodicValidation();
    }

    public static synchronized BetaManager getInstance() {
        if (instance == null) {
            instance = new BetaManager();
        }
        return instance;
    }

    public void initialize() {
        Client.debugLog("Initializing Beta Verification System");

        ConfigManager.load();

        checkVerificationOnStartup();
    }

    public void checkVerificationOnStartup() {
        Executors.newSingleThreadExecutor().submit(() -> {
            if (!validateSession()) {
                Client.debugLog("No valid beta session found");

                ModConfig.BetaConfig betaConfig = ConfigManager.getBetaConfig();
                if (betaConfig.firstLaunch || (!hasBetaKey() && !hasValidToken())) {
                    MinecraftClient.getInstance().execute(() -> {
                        MinecraftClient client = MinecraftClient.getInstance();
                        if (client.currentScreen == null) {
                            client.setScreen(new BetaKeyScreen());
                        }
                    });
                }
            } else {
                Client.debugLog("Beta verification successful");
            }
        });
    }

    public boolean validateSession() {
        ModConfig.BetaConfig betaConfig = ConfigManager.getBetaConfig();

        if (!ConfigManager.hasValidToken()) {
            return false;
        }

        VerificationResult result = verificationClient.validateSession(
                betaConfig.verificationToken
        );

        if (result.isSuccess()) {
            isVerified = true;
            return true;
        } else {
            isVerified = false;
            betaConfig.verificationToken = "";
            betaConfig.tokenExpiresAt = 0;
            ConfigManager.save();
            return false;
        }
    }

    private void setupPeriodicValidation() {
        scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleAtFixedRate(() -> {
            if (isVerified) {
                validateSession();
            }
        }, 5, 5, TimeUnit.MINUTES);
    }

    public void shutdown() {
        if (scheduler != null) {
            scheduler.shutdown();
        }
    }

    public boolean isVerified() {
        return isVerified;
    }

    public boolean hasBetaKey() {
        return ConfigManager.hasBetaKey();
    }

    public boolean hasValidToken() {
        return ConfigManager.hasValidToken();
    }
}