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

namespace XGame.Editor.Build.AssetBundles
{
    public partial class ABPackerInfoManifest : ScriptableObject
    {
        public AssetBundleNameMode bundleNameMode;
        /// <summary>
        /// 是否合并shader
        /// 将*.shadervariants文件和其所依赖的shader一起打在一个包内
        /// </summary>
        public bool isMergeShader;
        [SerializeField]
        private List<ABPackerInfo> packerInfos;

        public List<ABPackerInfo> PackerInfos
        {
            get
            {
                if (packerInfos == null)
                {
                    packerInfos = new List<ABPackerInfo>();
                }
                return packerInfos;
            }
        }

        public bool AddPackerInfo(ABPackerInfo info)
        {
            if (packerInfos == null)
            {
                packerInfos = new List<ABPackerInfo>();
            }
            int index = packerInfos.FindIndex((item => item.GUID.Equals(info.GUID)));
            if (index < 0)
            {
                packerInfos.Add(info);
                return true;
            }
            else
            {
                Debug.LogError($"Add PackerInfo. but it's repeat. {info}");
            }
            return false;
        }

        public bool IsContainGUID(string guid)
        {
            if (!string.IsNullOrEmpty(guid))
            {
                var index = packerInfos?.FindIndex((info) => info.GUID.Equals(guid)) ?? -1;
                if (index != -1)
                {
                    return true;
                }
            }
            return false;
        }

        public bool IsContainAssetPath(string assetPath)
        {
            var guid = AssetDatabase.AssetPathToGUID(assetPath);
            return IsContainGUID(guid);
        }
        public bool TryGetPackerInfoByGUID(string guid, out ABPackerInfo packerInfo)
        {
            if (!string.IsNullOrEmpty(guid))
            {
                var index = packerInfos?.FindIndex((info) => info.GUID.Equals(guid)) ?? -1;
                if (index != -1)
                {
                    packerInfo = packerInfos[index];
                    return true;
                }
            }
            packerInfo = default;
            return false;
        }

        public bool TryGetPackerInfoByAssetPath(string assetPath, out ABPackerInfo packerInfo)
        {
            return TryGetPackerInfoByGUID(AssetDatabase.AssetPathToGUID(assetPath), out packerInfo);
        }

        public bool VerifyInfoRepeat()
        {
            var listGUID = new List<string>();
            bool result = true;
            if (packerInfos != null)
            {
                foreach (var info in packerInfos)
                {
                    if (listGUID.Contains(info.GUID))
                    {
                        Debug.LogError($"PackerInfo repeat. {info}");
                        result = false;
                        continue;
                    }
                    listGUID.Add(info.GUID);
                }
            }
            listGUID.Clear();
            return result;
        }

        public void Save()
        {
            EditorUtility.SetDirty(this);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }

}
