using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ShapeSelect : MonoBehaviour
{
    // The array of player prefabs
    public GameObject[] shapePrefabs;

    // The selected prefab
    public static GameObject selectedShape;

    // The current shape name
    public TextMeshProUGUI shapeName;

    // The image for the selected shape
    public Image shapeImage;

    // The prefab stats slider to visualize the value of each shape stat
    public Slider speedSlider;
    public Slider shieldSlider;
    public Slider damageSlider;
    public Slider fireRateSlider;

    // Buttons to navigate through the shapes
    public Button leftButton;
    public Button rightButton;

    // Current index of the selected shape
    private int currentIndex = 0;

    void Start()
    {
        // Set up button listeners
        leftButton.onClick.AddListener(OnLeftButtonClick);
        rightButton.onClick.AddListener(OnRightButtonClick);

        // Initialize UI with the first shape
        UpdateUI();
    }

    // Handle keyboard input
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
        {
            OnLeftButtonClick();
        }
        if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            OnRightButtonClick();
        }
    }

    void OnLeftButtonClick()
    {
        MainMenu.instance.PlayClick();

        // Move to the previous shape
        currentIndex--;
        if (currentIndex < 0) currentIndex = shapePrefabs.Length - 1;

        UpdateUI();
    }

    void OnRightButtonClick()
    {
        MainMenu.instance.PlayClick();

        // Move to the next shape
        currentIndex++;
        if (currentIndex >= shapePrefabs.Length) currentIndex = 0;

        UpdateUI();
    }

    void UpdateUI()
    {
        // Get the current shape prefab and its PlayerController component
        GameObject currentPrefab = shapePrefabs[currentIndex];

        selectedShape = currentPrefab;

        PlayerController playerController = currentPrefab.GetComponent<PlayerController>();

        if (playerController != null)
        {
            // Display the shape name
            shapeName.text = currentPrefab.gameObject.name;

            // Update the stats sliders with the prefab's values
            speedSlider.value = playerController.maxSpeed;
            shieldSlider.value = playerController.GetComponentInChildren<Shield>().currentShieldHealth;
            damageSlider.value = playerController.GetComponentInChildren<Weapon>().bulletDamage;

            float maxFireRate = 1.2f; 
            fireRateSlider.value = maxFireRate - playerController.GetComponentInChildren<Weapon>().shootCooldown;

            // Update the shape image (assuming the prefab has a SpriteRenderer component with the sprite)
            SpriteRenderer spriteRenderer = currentPrefab.GetComponent<SpriteRenderer>();
            if (spriteRenderer != null)
            {
                shapeImage.sprite = spriteRenderer.sprite;
            }
        }
    }
}
