using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;

namespace SC.XR.Unity.Module_Tooltip
{
    [ExecuteAlways]
    public class Modules_TooltipUI : MonoBehaviour
    {
        [TextArea]
        [SerializeField]
        [Tooltip("Text for the ToolTip to display")]
        private string toolTipText;
        private string ToolTipText
        {
            set { toolTipText = value; }
            get { return toolTipText; }
        }

        [SerializeField]
        [Range(0f, 100f)]
        [Tooltip("Size of text")]
        private float textSize;
        private float TextSize
        {
            set { textSize = value; }
            get { return textSize; }
        }

        [SerializeField]
        [Range(0f, 100f)]
        [Tooltip("The width of lable")]
        private float lableWidth;
        private float LableWidth
        {
            set { lableWidth = value; }
            get { return lableWidth; }
        }

        [SerializeField]
        [Range(0f, 100f)]
        [Tooltip("The height of lable")]
        private float lableHeight;
        private float LableHeight
        {
            set { lableHeight = value; }
            get { return lableHeight; }
        }

        [SerializeField]
        [Range(0, 0.05f)]
        [Tooltip("The scale of lable")]
        private float lableScale;
        private float LableScale
        {
            set { lableScale = value; }
            get { return lableScale; }
        }

        private Transform lable;
        private BoxCollider boxCollider;
        private Transform background;
        private TextMeshPro textMeshPro;
        private RectTransform textsize;
        private bool getready = false;

        private void GetReady()
        {
            lable = transform.Find("Lable");
            boxCollider = lable.GetComponent<BoxCollider>();
            background = lable.Find("Background");
            textMeshPro = GetComponentInChildren<TextMeshPro>();
            textsize = gameObject.GetComponentInChildren<RectTransform>();
            getready = true;
        }

        private void OnEnable()
        {
            GetReady();
        }

        protected virtual void Update()
        {
            textMeshPro.text = ToolTipText;
            textMeshPro.fontSize = TextSize;
            background.localScale = new Vector3(LableWidth, LableHeight, 1);
            boxCollider.size = new Vector3(LableWidth, LableHeight, 1);
            textsize.sizeDelta = new Vector2(LableWidth, LableHeight);
            lable.localScale = new Vector3(LableScale, LableScale, LableScale);
        }

        public string API_GetLableText()
        {
            if (!getready)
            {
                GetReady();
            }
            return ToolTipText;
        }
        public void API_SetLableText(string newText)
        {
            if (!getready)
            {
                GetReady();
            }
            ToolTipText = newText;
        }
        public void API_SetLableTextColor(Color color)
        {
            if (!getready)
            {
                GetReady();
            }
            textMeshPro.color = color;
        }
        public void API_SetLableTextSize(float size)
        {
            if (!getready)
            {
                GetReady();
            }
            TextSize = size;
        }
        public void API_SetLableSize(float width, float height)
        {
            LableWidth = width;
            LableHeight = height;
        }
        public void API_SetLableScale(float scale)
        {
            LableScale = scale;
        }
    }
}