using UnityEngine;

public class GameSetup
{
    public static void InitializeGame()
    {

        for (int i = 0; i < 8; i++) {
            if (!PlayerPrefs.HasKey($"Score_{i}")) {
                PlayerPrefs.SetInt($"Score_{i}", 0);
            }
            if (!PlayerPrefs.HasKey($"Level_{i}")) {
                PlayerPrefs.SetInt($"Level_{i}", 0);
            }
            if (!PlayerPrefs.HasKey($"Name_{i}")) {
                PlayerPrefs.SetString($"Name_{i}", "");
            }
        }

        Debug.Log("Game Initialized");
    }

    public static void ZapScores()
    {
        for (int i = 0; i < 8; i++) {
            PlayerPrefs.SetInt($"Score_{i}", 0);
            PlayerPrefs.SetInt($"Level_{i}", 0);
            PlayerPrefs.SetString($"Name_{i}", "");
        }

        Debug.Log("Scores Reset");
    }

    public static void InsertScore(int score, int level, string name)
    {
        for (int i = 0; i < 8; i++) {
            int currentScore = PlayerPrefs.GetInt($"Score_{i}");
            if (score > currentScore) {
                // Shift lower scores down
                for (int j = 7; j > i; j--) {
                    PlayerPrefs.SetInt($"Score_{j}", PlayerPrefs.GetInt($"Score_{j - 1}"));
                    PlayerPrefs.SetInt($"Level_{j}", PlayerPrefs.GetInt($"Level_{j - 1}"));
                    PlayerPrefs.SetString($"Name_{j}", PlayerPrefs.GetString($"Name_{j - 1}"));
                }
                // Insert new score
                PlayerPrefs.SetInt($"Score_{i}", score);
                PlayerPrefs.SetInt($"Level_{i}", level);
                PlayerPrefs.SetString($"Name_{i}", name);
                break;
            }
        }

        Debug.Log($"Inserted Score: {score}, Level: {level}, Name: {name}");
    }

    public static bool IsHighScore(int score)
    {
        for (int i = 0; i < 8; i++) {
            int currentScore = PlayerPrefs.GetInt($"Score_{i}");
            if (score > currentScore) {
                return true;
            }
        }
        return false;
    }
}
