using UnityEngine.UI;
using static UnityEngine.UI.ContentSizeFitter;
using UnityEngine;
using TMPro;
/// <summary>
/// 更改背景颜色,填充文字,更改文本大小,更改背景大小
/// </summary>
public class BackgroundLabel : MonoBehaviour
{
    public Transform mouseTarget;
    public SpriteRenderer instructTextBackground;
    public TextMeshPro textMeshPro;
    public Sprite rectangle;
    public Sprite roundedRectangle;
    public Sprite gradientRectangle;
    public float marginHorizontal;
    public float marginVertical;
    public int shortTextCount = 4;

    private bool shortable => shortTextCount > 1 && !string.IsNullOrWhiteSpace(_text) && _text.Length > shortTextCount;
    private bool _fullText;

    public bool fullText
    {
        set
        {
            if (_fullText == value) return;
            _fullText = value;
            ShowText();
        }
    }
    private static Camera _camera;

    private string _text;
    // Start is called before the first frame update
    private void Update()
    {
        if (!shortable) return;
        if (Physics.Raycast(_camera.ScreenPointToRay(Input.mousePosition), out var hit))
        {
            fullText = hit.transform == mouseTarget;
        }
    }
    public void Init(string text, bool showBackground)
    {
        _text = text;
        if (_camera == null) _camera = Camera.main;
        //

        if (!showBackground)
        {
            instructTextBackground.gameObject.SetActive(false);
        }
        else
        {

            // if (string.IsNullOrEmpty(text))
            // {
            //     text = labelInfo.text;
            // }

            //非渐变
            instructTextBackground.sprite = rectangle;

            instructTextBackground.color = Color.white;

            instructTextBackground.gameObject.AddComponent<MeshCollider>().convex = true;
        }
        textMeshPro.gameObject.AddComponent<MeshCollider>().convex = true;
        ShowText();
    }

    private void ShowText()
    {
        if (string.IsNullOrWhiteSpace(_text))
        {
            textMeshPro.gameObject.SetActive(false);
        }
        else
        {

            //Debug.Log($"{_fullText} ==== {_text.Length} / {shortTextCount}");
            if (_fullText || shortTextCount <= 1 || _text.Length <= shortTextCount)
                textMeshPro.text = _text;
            else
                textMeshPro.text = $"{_text.Substring(0, shortTextCount - 1)}…";
            instructTextBackground.size = GetPreferredSize();
        }
    }

    public void SetPosition(Vector3 pos)
    {
        textMeshPro.transform.localPosition = pos;//new Vector3(0, y + 4f, 0);
    }

    public void SetAlignment()
    {
        textMeshPro.alignment = TextAlignmentOptions.Bottom;
    }

    //立即获取ContentSizeFitter的区域
    public Vector2 GetPreferredSize()
    {
        LayoutRebuilder.ForceRebuildLayoutImmediate(textMeshPro.GetComponent<RectTransform>());
        return new Vector2((HandleSelfFittingAlongAxis(0)) + marginHorizontal, (HandleSelfFittingAlongAxis(1)) + marginVertical);
    }

    public float HandleSelfFittingAlongAxis(int axis)
    {
        ContentSizeFitter c = textMeshPro.GetComponent<ContentSizeFitter>();
        RectTransform r = textMeshPro.GetComponent<RectTransform>();
        FitMode fitting = (axis == 0 ? c.horizontalFit : c.verticalFit);
        if (fitting == FitMode.MinSize)
        {
            return LayoutUtility.GetMinSize(r, axis);
        }
        else
        {
            return LayoutUtility.GetPreferredSize(r, axis);
        }
    }


}