using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using UnityEngine.UI;

namespace TFramework
{
    /// <summary>
    /// Ч��������
    /// </summary>
    public partial class EffectManager : BaseManager
    {
        public Material m_outlineMaskMaterial;
        public Material m_outlineFillMaterial;
        public Material m_hihghlightMaterial;

        public bool m_openHighlight;

        public Color m_defaultHighlightColor;

        public Color m_defaultOutlineColor;

        private Dictionary<GameObject, TweenerCore<Color, Color, ColorOptions>> _gameObjHighlightDic = new Dictionary<GameObject, TweenerCore<Color, Color, ColorOptions>>();

        private Color Alpha = new Color(1, 1, 1, 0);


        #region 3D对象高亮
        /// <summary>
        /// 开启3D高亮
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="time"></param>
        public void OpenFlash(GameObject obj, float time = 0.5f)
        {
            if (!m_openHighlight || _gameObjHighlightDic.ContainsKey(obj)) return;
            ObjectHighlight highlight = obj.GetComponent<ObjectHighlight>() ?? obj.AddComponent<ObjectHighlight>();
            TweenerCore<Color, Color, ColorOptions> tweener = DOTween.ToAlpha(() => highlight.HighlightColor, x => highlight.HighlightColor = x, 1, time).SetLoops(-1, LoopType.Yoyo).SetEase(Ease.Linear);
            tweener.startValue = highlight.HighlightColor * Alpha;
            _gameObjHighlightDic.Add(obj, tweener);
        }

        public void OpenFlash(GameObject obj, Color color, float time = 0.5f)
        {
            if (!m_openHighlight || _gameObjHighlightDic.ContainsKey(obj)) return;
            ObjectHighlight highlight = obj.GetComponent<ObjectHighlight>() ?? obj.AddComponent<ObjectHighlight>();
            color.a = 0;
            highlight.HighlightColor = color;
            TweenerCore<Color, Color, ColorOptions> tweener = DOTween.ToAlpha(() => highlight.HighlightColor, x => highlight.HighlightColor = x, 1, time).SetLoops(-1, LoopType.Yoyo).SetEase(Ease.Linear);
            tweener.startValue = highlight.HighlightColor * Alpha;
            _gameObjHighlightDic.Add(obj, tweener);
        }

        /// <summary>
        /// 关闭3D高亮
        /// </summary>
        /// <param name="obj"></param>
        public void CloseFlash(GameObject obj)
        {
            if (!_gameObjHighlightDic.ContainsKey(obj)) return;
            _gameObjHighlightDic[obj].Kill();
            obj.GetComponent<ObjectHighlight>().HighlightColor = _gameObjHighlightDic[obj].startValue;
            _gameObjHighlightDic.Remove(obj);
        }

        /// <summary>
        /// 开启描边
        /// </summary>
        /// <param name="obj"></param>
        public void OpenOutLine(GameObject obj)
        {
            //CloseAllHighlight();
            CloseFlash(obj);
            Outline outline = obj.GetComponent<Outline>() ?? obj.AddComponent<Outline>();
            outline.OutlineColor = m_defaultOutlineColor + Color.black;
        }

        public void OpenOutLine(GameObject obj, Color color)
        {
            CloseAllHighlight();
            CloseFlash(obj);
            Outline outline = obj.GetComponent<Outline>() ?? obj.AddComponent<Outline>();
            outline.OutlineColor = color + Color.black;
        }

        /// <summary>
        /// 关闭描边
        /// </summary>
        /// <param name="obj"></param>
        public void CloseOutline(GameObject obj)
        {
            //CloseAllHighlight();
            CloseFlash(obj);
            Outline outline = obj.GetComponent<Outline>() ?? obj.AddComponent<Outline>();
            outline.OutlineColor = m_defaultOutlineColor * Alpha;
        }

        /// <summary>
        /// 关闭所有高亮
        /// </summary>
        public void CloseAllFlash()
        {
            foreach (var item in _gameObjHighlightDic)
            {
                if (item.Key == null) continue;
                item.Value.Kill();
                item.Key.GetComponent<ObjectHighlight>().HighlightColor = item.Value.startValue;
            }
            _gameObjHighlightDic.Clear();
        }
        #endregion

        /// <summary>
        /// 打开高亮 3D对象 UI
        /// </summary>
        /// <param name="obj"></param>
        public void OpenHighlight(GameObject obj)
        {
            Graphic graphics = obj.GetComponent<Graphic>();
            Selectable selectable = obj.GetComponent<Selectable>();
            if (graphics)
                graphics.OpenFlash(m_defaultHighlightColor);
            else if (selectable)
                selectable.OpenFlash(m_defaultHighlightColor);
            else
                OpenFlash(obj);
        }

        public void OpenHighlight(GameObject obj, Color color)
        {
            Graphic graphics = obj.GetComponent<Graphic>();
            Selectable selectable = obj.GetComponent<Selectable>();
            if (graphics)
                graphics.OpenFlash(color);
            else if (selectable)
                selectable.OpenFlash(color);
            else
                OpenFlash(obj, color);
        }

        /// <summary>
        /// 关闭高亮 3D对象 UI
        /// </summary>
        /// <param name="obj"></param>
        public void CloseHighlight(GameObject obj)
        {
            Graphic graphics = obj.GetComponent<Graphic>();
            Selectable selectable = obj.GetComponent<Selectable>();
            if (graphics)
                graphics.CloseFlash();
            else if (selectable)
                selectable.CloseFlash();
            else
                CloseFlash(obj);
        }

        /// <summary>
        /// 关闭所有高亮
        /// </summary>
        public void CloseAllHighlight()
        {
            UIHighlight.CloseAllFlash();
            CloseAllFlash();
        }
    }
}
