using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
public class GameBoard : MonoBehaviour
{
    private SpriteRenderer spriteRenderer;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        ResetBoard();
    }

    public void ResetBoard()
    {
        // Reset the game board to its initial state
        int currentLevel = PlayerPrefs.GetInt("CurrentLevel");
        currentLevel = currentLevel == 0 ? 1 : currentLevel; // Ensure currentLevel is at least 1
        currentLevel = (((currentLevel - 1) / 5) % 10 + 10) % 10 + 1;

        Debug.Log($"Current Level: {currentLevel}");

        string resourcePath = $"Patterns/fore_{currentLevel}";

        Texture2D texture = Resources.Load<Texture2D>(resourcePath);
        if (texture == null)
        {
            Debug.LogWarning($"Could not load Sprite or Texture2D at Resources/{resourcePath}");
            return;
        }

        // Fallback if the asset is imported as Texture instead of Sprite.
        Sprite runtimeSprite = Sprite.Create(
            texture,
            new Rect(0, 0, texture.width, texture.height),
            new Vector2(0.5f, 0.5f),
            100f
        );

        spriteRenderer.sprite = runtimeSprite;
    }
}
