using XGame.Framework.Asset;
using XGame.Framework.Asset.Addressable.Data;
using System.Text;
using UnityEngine;

namespace XGame.Editor.Asset.Addressable
{
    public static class AddressableAssetFoldExt
    {
        /// <summary>
        /// 按文件夹结构加入一个文件
        /// </summary>
        /// <param name="asssetFold"></param>
        /// <param name="assetInfo"></param>
        /// <param name="onApply"></param>
        internal static void AddFileWithFold(this AddressableAssetFold asssetFold, AssetInfo assetInfo, System.Func<ApplyEvnetContext, bool> onApply)
        {
            string path = assetInfo.GetAssetPath();
            char separator = '/';
            var names = path.Split(separator);
            //去掉Assets文件夹和最后的文件名
            if (names.Length <= 2)
            {
                Debug.LogError($"AddFileWithFold Error: Asset is in the wrong path. {assetInfo}");
                return;
            }
            var parent = asssetFold;
            StringBuilder builder = new StringBuilder();
            builder.Append(names[0]);
            for (int i = 1; i < names.Length - 1; i++)
            {
                builder.Append(separator);
                builder.Append(names[i]);
                var foldPath = builder.ToString();
                var fold = parent.GetChild(foldPath);
                if (fold == null)
                {
                    fold = new AddressableAssetFold(foldPath);
                    parent.AddChild(fold);
                }
                parent = (AddressableAssetFold)fold;
            }

            if (parent.Contains(path))
            {
                Debug.LogError($"AddFileWithFold Repeat. {assetInfo}");
                return;
            }

            var file = new AddressableAssetFile(path, assetInfo)
            {
                IsFoldout = true
            };
            file.OnApply += onApply;
            parent.AddChild(file);
            //Debug.Log($"AddFileWithFold Path: {path}");
        }
        /// <summary>
        /// 加入一个文件
        /// </summary>
        /// <param name="asssetFold"></param>
        /// <param name="assetInfo"></param>
        /// <param name="onApply"></param>
        internal static void AddFile(this AddressableAssetFold asssetFold, AssetInfo assetInfo, System.Func<ApplyEvnetContext, bool> onApply, FileSign fileSign)
        {
            string path = assetInfo.GetAssetPath();
            if (string.IsNullOrEmpty(path))
            {
                Debug.LogWarning($"AddFile {assetInfo}. But can't find assetPath. ");
                path = assetInfo.addressableName;
            }
            var file = new AddressableAssetFile(path, assetInfo)
            {
                IsFoldout = true,
                Sign = fileSign
            };
            file.OnApply += onApply;
            asssetFold.AddChild(file);
            //Debug.Log($"AddFile Path: {path}");
        }
        /// <summary>
        /// 加入失效的文件
        /// </summary>
        /// <param name="asssetFold"></param>
        /// <param name="assetInfo"></param>
        /// <param name="onRemove"></param>
        public static void AddInvalidFile(this AddressableAssetFold asssetFold, AssetInfo assetInfo,
            System.Action<string> onRemove)
        {
            var path = assetInfo.GetAssetPath();
            if (string.IsNullOrEmpty(path))
            {
                //Debug.LogWarning($"{assetInfo} can't find assetPath. ");
                path = assetInfo.addressableName;
            }

            var file = new AddressableAssetFile(path, assetInfo)
            {
                Sign = FileSign.Invalid
            };
            if (onRemove != null)
            {
                file.OnRemove += onRemove;
            }
            asssetFold.AddChild(file);
            //Debug.Log($"AddInvalidFile. {assetInfo}");
        }
    }

}

