using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;


namespace XGame.Editor.Asset.Addressable
{
    /// <summary>
    /// 文件夹
    /// </summary>
    public class AddressableAssetFold : AddressableAssetEntry
    {

        private readonly List<AddressableAssetEntry> childs;

        //private bool isFoldout;
        private bool isSort;
        public override EntryType EnType => EntryType.Fold;
        private event Action addChildrenEvent;
        public event Action AddChildrenEvent
        {
            add { addChildrenEvent += value; }
            remove { addChildrenEvent -= value; }
        }

        public AddressableAssetFold(string path) : base(path)
        {
            childs = new List<AddressableAssetEntry>();
            isSort = false;
        }

        public void AddChild(AddressableAssetEntry entry)
        {
            if (entry.Parent != null && entry.Parent.Equals(this))
            {
                Debug.LogError($"Add Child Repeat. Parent:{Path} Child:{entry.Path}");
                return;
            }
            childs.Add(entry);
            entry.Parent = this;
            isSort = true;
        }

        public void RemoveChild(AddressableAssetEntry entry)
        {
            if (childs.Contains(entry))
            {
                childs.Remove(entry);
            }
            entry.Parent = null;
        }

        public AddressableAssetEntry GetChild(string path)
        {
            return childs.Find((entry => entry.Path.Equals(path)));
        }
        public bool Contains(string path)
        {
            return childs.Find((entry => entry.Path.Equals(path))) != null;
        }
        /// <summary>
        /// 节点数量
        /// </summary>
        public int ChildCount => childs.Count;

        protected override void OnSetParent()
        {

        }

        protected override void OnGUI()
        {
            EditorGUILayout.BeginVertical();

            IsFoldout = EditorGUILayout.Foldout(IsFoldout, ShowName);
            if (IsFoldout)
            {
                if (addChildrenEvent != null)
                {
                    addChildrenEvent.Invoke();
                    addChildrenEvent = null;
                }
                if (isSort)
                {
                    isSort = false;
                    childs.Sort();
                }
                for (int i = 0; i < childs.Count;)
                {
                    var child = childs[i];
                    child.GUI();
                    if (child.Parent != null)
                    {
                        i++;
                    }
                }
            }

            EditorGUILayout.EndVertical();
        }

        protected override void OnClear()
        {
            foreach (var child in childs)
            {
                child.Clear();
            }
            childs.Clear();
            addChildrenEvent = null;
            isSort = false;
        }
    }

}

