using UnityEngine;
using UnityEngine.UI;
using TMPro;

/// <summary>
/// Simple script to help create action button prefabs programmatically
/// This can be attached to a prefab or used to create buttons at runtime
/// </summary>
public class ActionButtonCreator : MonoBehaviour
{
    [Header("Button Creation")]
    public bool createButtonOnStart = false;
    public string buttonText = "Action";
    public Sprite buttonIcon;

    void Start()
    {
        if (createButtonOnStart)
        {
            CreateActionButton();
        }
    }

    /// <summary>
    /// Creates a complete action button with icon and text
    /// </summary>
    public static GameObject CreateActionButton(Transform parent, string buttonName = "ActionButton")
    {
        // Create main button object
        GameObject buttonObj = new GameObject(buttonName);
        buttonObj.transform.SetParent(parent, false);

        // Add RectTransform
        RectTransform rectTransform = buttonObj.AddComponent<RectTransform>();
        rectTransform.sizeDelta = new Vector2(60, 60);

        // Add Image component for background
        Image backgroundImage = buttonObj.AddComponent<Image>();
        backgroundImage.color = new Color(0.2f, 0.2f, 0.2f, 0.8f);
        backgroundImage.type = Image.Type.Sliced;

        // Add Button component
        Button button = buttonObj.AddComponent<Button>();

        // Create Icon child
        GameObject iconObj = new GameObject("Icon");
        iconObj.transform.SetParent(buttonObj.transform, false);

        RectTransform iconRect = iconObj.AddComponent<RectTransform>();
        iconRect.anchorMin = new Vector2(0.1f, 0.3f);
        iconRect.anchorMax = new Vector2(0.9f, 0.9f);
        iconRect.offsetMin = Vector2.zero;
        iconRect.offsetMax = Vector2.zero;

        Image iconImage = iconObj.AddComponent<Image>();
        iconImage.color = Color.white;

        // Create Label child
        GameObject labelObj = new GameObject("Label");
        labelObj.transform.SetParent(buttonObj.transform, false);

        RectTransform labelRect = labelObj.AddComponent<RectTransform>();
        labelRect.anchorMin = new Vector2(0f, 0f);
        labelRect.anchorMax = new Vector2(1f, 0.3f);
        labelRect.offsetMin = Vector2.zero;
        labelRect.offsetMax = Vector2.zero;

        TextMeshProUGUI labelText = labelObj.AddComponent<TextMeshProUGUI>();
        labelText.text = "Action";
        labelText.fontSize = 8;
        labelText.alignment = TextAlignmentOptions.Center;
        labelText.color = Color.white;

        // Setup button colors
        ColorBlock colors = button.colors;
        colors.normalColor = new Color(0.2f, 0.2f, 0.2f, 0.8f);
        colors.highlightedColor = new Color(0.3f, 0.3f, 0.3f, 0.9f);
        colors.pressedColor = new Color(0.1f, 0.1f, 0.1f, 0.9f);
        colors.selectedColor = new Color(0.3f, 0.3f, 0.3f, 0.9f);
        colors.disabledColor = new Color(0.1f, 0.1f, 0.1f, 0.5f);
        button.colors = colors;

        return buttonObj;
    }

    private void CreateActionButton()
    {
        GameObject button = CreateActionButton(transform, buttonText);

        // Set icon if provided
        if (buttonIcon != null)
        {
            Image iconImage = button.transform.Find("Icon").GetComponent<Image>();
            if (iconImage != null)
                iconImage.sprite = buttonIcon;
        }

        // Set text
        TextMeshProUGUI labelText = button.transform.Find("Label").GetComponent<TextMeshProUGUI>();
        if (labelText != null)
            labelText.text = buttonText;
    }
}
