
using XGame.Framework.Serialization;

namespace XGame.Framework.Asset
{

    [System.Serializable]
    public class AssetBundleInfo : ISerializable
    {
        /// <summary>
        /// bundleName，等于originBundleName或者bundleId
        /// </summary>
        public string bundleName;
        public string variantName;
        public string originBundleName;
        public uint bundleId;
        //public string[] assetPaths;
        public string[] assets;
        /// <summary>
        /// 依赖的bundle列表
        /// </summary>
        public uint[] dependencies;
        /// <summary>
        /// bundle的偏移值
        /// </summary>
        public uint offset;

        public string FullName
        {
            get
            {
                if (string.IsNullOrEmpty(bundleName))
                {
                    return string.Empty;
                }
                return string.IsNullOrEmpty(variantName) ? bundleName : $"{bundleName}.{variantName}";
            }
        }

        public int AssetsCount => assets?.Length ?? 0;

        public int DependenciesCount => dependencies?.Length ?? 0;

        public bool ContainsAsset(string assetName)
        {
            if (assets == null || string.IsNullOrEmpty(assetName))
                return false;
            var index = System.Array.FindIndex(assets, assetName.Equals);
            return index >= 0;
        }

        //public int CompareTo(object obj)
        //{
        //    if (obj is AssetBundleInfo compareInfo)
        //    {
        //        //多语言的bundle排前面
        //        var idx_0 = originBundleName.Contains("/") ? -1 : 1;
        //        var idx_1 = compareInfo.originBundleName.Contains("/") ? -1 : 1;
        //        var remain = idx_0 - idx_1;
        //        if (remain != 0)
        //        { 
        //            return remain;
        //        }
        //        //ResStatic的bundle排最后
        //        //idx_0 = originBundleName.Contains("/res/static/") ? 1 : -1;
        //        //idx_1 = compareInfo.originBundleName.StartsWith("/res/static/") ? 1 : -1;
        //        //remain = idx_0 - idx_1;
        //        //if (remain != 0)
        //        //{
        //        //    return remain;
        //        //}
        //        return string.CompareOrdinal(originBundleName, compareInfo.originBundleName);
        //    }
        //    return -1;
        //}

        void ISerializable.Serialize(IWriter writer)
        {
            writer.Write(bundleName);
            writer.Write(variantName);
            writer.Write(bundleId);
            writer.Write(assets);
            writer.Write(dependencies);
            writer.Write(offset);
        }

        void ISerializable.Deserialize(IReader reader)
        {
            bundleName = reader.ReadString();
            variantName = reader.ReadString();
            bundleId = reader.ReadUInt();
            assets = reader.ReadEnumerable<string[]>();
            dependencies = reader.ReadEnumerable<uint[]>();
            offset = reader.ReadUInt();
        }
    }
}


