using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class Customization : MonoBehaviour
{
    /// <summary>
    /// Represents a set of customization options.
    /// </summary>
    [System.Serializable]
    public class CustomizationSet
    {
        public string identifier;           // Identifier for the customization set.
        public Button[] colorButtons;       // Buttons to select colors.
        public Image targetImage;           // Image to apply the selected color to.
        public Color[] colorChoices;        // Array of color choices.
    }

    [Header("Customization Sets")]

    [Header("Ship Customization")]
    public CustomizationSet shipCustomization;

    [Header("Bullet Customization")]
    public CustomizationSet bulletCustomization;

    [Header("HUD Customization")]
    public CustomizationSet HUDCustomization;

    [Header("Player Bullet")]
    public SpriteRenderer playerBullet;


    private void Start()
    {
        // Customize ship, bullet, and HUD based on provided sets.
        Customize(shipCustomization);
        Customize(bulletCustomization);
        Customize(HUDCustomization);

        // Set default colors for ship, bullet, and HUD.
        DefaultColors();
    }

    /// <summary>
    /// Customizes the UI buttons and applies colors to the target images.
    /// </summary>
    /// <param name="customizationSet">The customization set to apply.</param>
    private void Customize(CustomizationSet customizationSet)
    {
        if (customizationSet.colorButtons.Length != customizationSet.colorChoices.Length)
        {
            Debug.LogError("The total color options and colors need to match!");
            return;
        }

        for (int i = 0; i < customizationSet.colorButtons.Length; i++)
        {
            int index = i; // Capture the current index in the loop
            customizationSet.colorButtons[i].onClick.AddListener(() =>
                ApplyColor(customizationSet.identifier, customizationSet.targetImage, customizationSet.colorChoices[index]));
            SetButtonColor(customizationSet.colorButtons[i], customizationSet.colorChoices[i]);
        }
    }

    /// <summary>
    /// Applies the selected color to the target image based on the identifier.
    /// </summary>
    /// <param name="identifier">The identifier for the customization set.</param>
    /// <param name="targetImage">The image to apply the color to.</param>
    /// <param name="color">The color to apply.</param>
    private void ApplyColor(string identifier, Image targetImage, Color color)
    {
        MainMenu.instance.PlayClick(); // Play click sound

        targetImage.color = color; // Apply color to the target image

        // Apply color based on the identifier
        switch (identifier)
        {
            case "Ship":
                color.a = 1f;
                PlayerController.playerColor = color;
                PlayerController.playerColor.a = 1f;
                break;
            case "Bullet":
                color.a = 1f;
                playerBullet.color = color;
                break;
            case "HUD":
                color.a = 1f;
                HUD.HUDColor = color;
                HUD.HUDColor.a = 1f;
                break;
        }
    }

    /// <summary>
    /// Sets the button color for normal, highlighted, and selected states.
    /// </summary>
    /// <param name="button">The button to set the color for.</param>
    /// <param name="color">The color to set.</param>
    private void SetButtonColor(Button button, Color color)
    {
        ColorBlock colorBlock = button.colors;
        colorBlock.normalColor = color;
        colorBlock.highlightedColor = color;
        colorBlock.selectedColor = color;
        button.colors = colorBlock;
    }

    /// <summary>
    /// Resets ship, bullet, and HUD colors to default (white).
    /// </summary>
    public void DefaultColors()
    {
        PlayerController.playerColor = Color.white;
        playerBullet.color = Color.white;
        HUD.HUDColor = Color.white;
    }
}
