using EPOOutline;
using System.Collections.Generic;
using UnityEngine;
using WS;

[ObjectSystem]
public class HighlightComponentAwakSystem : AwakeSystem<HighlightComponent>
{
    public override void Awake(HighlightComponent self)
    {
        self.Awake();
    }
}

///<summary>高亮管理组件</summary>
public class HighlightComponent : SingleComponent<HighlightComponent>
{
    ///<summary>高亮数据</summary>
    private class HighlightData
    {
        ///<summary>高亮组件</summary>
        public Outlinable outlinable;
        ///<summary></summary>
        public bool IsOften;
    }

    ///<summary>所有高亮物体</summary>
    private Dictionary<GameObject, HighlightData> AllHighlight = new Dictionary<GameObject, HighlightData>();

    ///<summary>默认颜色</summary>
    private Color DefaultColor;

    public void Awake()
    {
        DefaultColor = new Color(1, 1, 0, 1);
    }

    ///<summary>开启高亮</summary>
    public void OpenHighlight(GameObject obj)
    {
        OpenHighlight(obj, DefaultColor);
    }

    ///<summary>开启高亮</summary>
    public void OpenHighlight(GameObject obj, Color color)
    {
        Add(obj, color);
    }

    ///<summary>关闭高亮</summary>
    public void CloseHighlight(GameObject obj)
    {
        Remove(obj);
    }

    /////<summary>开启常亮高亮</summary>
    //public void OpenOftenHighlight(GameObject obj)
    //{
    //    OpenOftenHighlight(obj, DefaultColor);
    //}

    ///<summary>开启常亮高亮</summary>
    public void OpenOftenHighlight(GameObject obj, Color color)
    {
        Add(obj, color, true);
    }

    ///<summary>关闭常亮高亮</summary>
    public void CloseOftenHighlight(GameObject obj)
    {
        Remove(obj, true);
    }

    ///<summary>开启高亮</summary>
    private void Add(GameObject obj, Color color, bool isoften = false)
    {
        if (!AllHighlight.ContainsKey(obj))
        {
            Outlinable outlinable = obj.AddComponent<Outlinable>();
            outlinable.FrontParameters.Color = color;
            outlinable.AddAllChildRenderersToRenderingList(RenderersAddingMode.SkinnedMeshRenderer | RenderersAddingMode.MeshRenderer | RenderersAddingMode.SpriteRenderer);
            AllHighlight.Add(obj, new HighlightData { outlinable = outlinable, IsOften = isoften });
        }
        AllHighlight[obj].outlinable.FrontParameters.Color = color;
    }

    ///<summary>移除</summary>
    private void Remove(GameObject obj, bool isoften = false)
    {
        if (AllHighlight.ContainsKey(obj))
        {
            HighlightData data = AllHighlight[obj];
            if (!isoften && data.IsOften)
            {
                return;
            }
            AllHighlight.Remove(obj);
            UnityEngine.Object.Destroy(data.outlinable);
        }
    }
    public void OpenOftenHighlight(GameObject obj)
    {
        
    }
    public void SetOperateObjLayer(Transform transform, int layer)
    {
        transform.gameObject.layer = layer;

        foreach (Transform child in transform)
        {
            SetOperateObjLayer(child, layer);
        }
    }
}