using System;
using UnityEditor;
using UnityEngine;

namespace XGame.Editor.Asset.Addressable
{
    public abstract class AddressableAssetEntry : IComparable
    {
        public enum EntryType
        {
            None = 0,
            Fold,
            File
        }
        /// <summary>
        /// 路径
        /// </summary>
        public string Path { private set; get; }
        /// <summary>
        /// 显示的名字
        /// </summary>
        public string ShowName { private set; get; }
        /// <summary>
        /// 文件夹开启标志
        /// </summary>
        public bool IsFoldout { set; get; }

        public virtual EntryType EnType => EntryType.None;

        private AddressableAssetEntry parent;

        public AddressableAssetEntry Parent
        {
            set
            {
                parent = value;
                OnSetParent();
            }
            get => parent;
        }

        protected AddressableAssetEntry(string path)
        {
            Path = path;
            ShowName = System.IO.Path.GetFileNameWithoutExtension(path);
        }

        public void GUI()
        {
            bool hasParent = parent != null;
            if (hasParent)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(10);
            }
            OnGUI();
            if (hasParent)
            {
                EditorGUILayout.EndHorizontal();
            }
        }

        public void Clear()
        {
            OnClear();
        }
        protected abstract void OnSetParent();
        protected abstract void OnGUI();
        protected abstract void OnClear();
        public int CompareTo(object obj)
        {
            int result = 0;
            if (obj is AddressableAssetEntry compare)
            {
                result = EnType - compare.EnType;
                if (result == 0)
                {
                    result = string.Compare(ShowName, compare.ShowName, StringComparison.OrdinalIgnoreCase);
                }
            }
            return result;
        }
    }

}
