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


public class UI_Twinkle : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    #region UI
    private Button button;
    private TextMeshProUGUI textMeshProUGUI;
    private Image image;
    private Dropdown dropdown;
    #endregion

    public string hexColor;
    private Color normalColor;
    public Color highlightedColor;
    public Sprite highlightedSprite;
    private Sprite normalSprite;

    public MyEnum myEnum;
    public Settype settype = Settype.变色;

    private void Start()
    {
        #region 获取UI
        button = GetComponent<Button>();
        textMeshProUGUI = GetComponent<TextMeshProUGUI>();
        image = GetComponent<Image>();
        dropdown = GetComponent<Dropdown>();
        #endregion

        if (ColorUtility.TryParseHtmlString(hexColor, out Color color))
        {
            highlightedColor = color;
        }

        #region 获取高亮色\常色
        if (button && myEnum == MyEnum.按钮)
        {
            normalColor = button.colors.normalColor;
        }
        else if (textMeshProUGUI && myEnum == MyEnum.TextMeshProUGUI)
        {
            normalColor = textMeshProUGUI.color;
        }
        else if (image && myEnum == MyEnum.图片)
        {
            image.color = new Color(image.color.r, image.color.g, image.color.b, 0.7f);
        }
        else if (dropdown && myEnum == MyEnum.下拉框)
        {
            normalSprite = dropdown.image.sprite;
        }
        #endregion

    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (myEnum == MyEnum.按钮 && settype == Settype.变色 && button.transition == Selectable.Transition.ColorTint)
        {
            if (button.interactable)
            {
                ColorBlock colors = button.colors;
                colors.highlightedColor = highlightedColor;
                button.colors = colors;
            }
            else
            {
                ColorBlock colors = button.colors;
                colors.disabledColor = highlightedColor;
                button.colors = colors;
            }
        }
        else if (myEnum == MyEnum.按钮 && settype == Settype.变色 && button.transition == Selectable.Transition.SpriteSwap)
        {
            if (button.interactable)
            {
                SpriteState spriteState = button.spriteState;
                spriteState.highlightedSprite = highlightedSprite;
                button.spriteState = spriteState;
            }
            else
            {
                SpriteState spriteState = button.spriteState;
                spriteState.disabledSprite = highlightedSprite;
                button.spriteState = spriteState;
            }
        }
        else if (myEnum == MyEnum.TextMeshProUGUI && settype == Settype.变色)
        {
            textMeshProUGUI.color = highlightedColor;
        }
        else if (myEnum == MyEnum.图片 && settype == Settype.渐变)
        {
            image.color = new Color(image.color.r, image.color.g, image.color.b, 1);
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (myEnum == MyEnum.按钮 && settype == Settype.变色 && button.transition == Selectable.Transition.ColorTint)
        {
            if (button.interactable)
            {
                ColorBlock colors = button.colors;
                colors.normalColor = normalColor;
                button.colors = colors;
            }
            else
            {
                ColorBlock colors = button.colors;
                colors.disabledColor = normalColor;
                button.colors = colors;
            }
        }
        else if (myEnum == MyEnum.按钮 && settype == Settype.变色 && button.transition == Selectable.Transition.SpriteSwap)
        {
            if (!button.interactable)
            {
                Image image = button.image;
                Sprite sprite = image.sprite;
                SpriteState spriteState = button.spriteState;
                spriteState.disabledSprite = sprite;
                button.spriteState = spriteState;
            }
        }
        else if (myEnum == MyEnum.TextMeshProUGUI && settype == Settype.变色)
        {
            textMeshProUGUI.color = normalColor;
        }
        else if (myEnum == MyEnum.图片 && settype == Settype.渐变)
        {
            image.color = new Color(image.color.r, image.color.g, image.color.b, 0.7f);
        }
    }

    public enum MyEnum
    {
        按钮,
        TextMeshProUGUI,
        图片,
        下拉框
    }

    public enum Settype
    {
        变色,
        渐变,
    }
}