using XGame.Editor.Asset;
using XGame.Framework.Asset;
using XGame.Framework.Asset.Addressable;
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using XGame.Framework.Serialization;

namespace XGame.Editor.Build.AssetBundles
{
    internal static class SerializableUtils
    {
        public static bool SerializeAllConfigs()
        {
            return true;
            //if (!SerializeAddressableCfg())
            //    return false;
            //if (!SerializeAssetbundleCfg())
            //    return false;
            //if (!SerializeAssetReferenceCfg())
            //    return false;
            //return SerializeSceneInfoCfg();
        }


        //public static bool SerializeAddressableCfg()
        //{
        //    var buildInSo = AddressableHelper.LoadAssetManifest(AddressableClassify.BuiltIn);
        //    var infosBinary = new AddressableInfosBinary();
        //    buildInSo.Copy(ref infosBinary);
        //    var productSo = AddressableHelper.LoadAssetManifest(AddressableClassify.Product);
        //    productSo.Copy(ref infosBinary);
        //    var path = FileDefine.AddressableCfg.StreamingPath;
        //    return Write(infosBinary, path);
        //}

        //public static bool SerializeAssetbundleCfg()
        //{
        //    var infosSo = AssetBundleUtils.LoadManifest();
        //    if (infosSo == null || infosSo.bundleInfos == null)
        //    {
        //        Debug.Log($"没有找到配置文件或者内容为空。Path:{PathDefine.BundleManifestPath}");
        //        return false;
        //    }
        //    var infosBinary = new AssetBundleInfosBinary()
        //    {
        //        bundleInfoMap = new System.Collections.Generic.Dictionary<string, AssetBundleInfo>()
        //    };
        //    foreach (var info in infosSo.bundleInfos)
        //    {
        //        infosBinary.bundleInfoMap.Add(info.FullName, info);
        //    }
        //    var path = FileDefine.AssetbundleCfg.StreamingPath;
        //    return Write(infosBinary, path);
        //}
        //public static bool SerializeAssetReferenceCfg()
        //{
        //    var refManifestPath = PathDefine.ReferenceManifestPath;
        //    var infosSo = AssetDatabase.LoadAssetAtPath<AssetReferenceInfosSo>(refManifestPath);
        //    if (infosSo == null || infosSo.references == null)
        //    {
        //        Debug.Log($"没有找到配置文件或者内容为空。Path:{refManifestPath}");
        //        return false;
        //    }
        //    var infosBinary = new AssetReferenceInfosBinary()
        //    {
        //        referencesMap = new System.Collections.Generic.Dictionary<string, string[]>()
        //    };
        //    foreach(var reference in infosSo.references)
        //    {
        //        infosBinary.referencesMap.Add(reference.assetName, reference.references);
        //    }
        //    var path = FileDefine.AssetReferenceCfg.StreamingPath;
        //    return Write(infosBinary, path);
        //}
        //public static bool SerializeSceneInfoCfg()
        //{
        //    var infosSoPath = PathDefine.SceneAssetBundleManifestPath;
        //    var infosSo = AssetDatabase.LoadAssetAtPath<SceneInfosSo>(infosSoPath);
        //    if (infosSo == null || infosSo.sceneInfos == null)
        //    {
        //        Debug.Log($"没有找到配置文件或者内容为空。Path:{infosSoPath}");
        //        return false;
        //    }
        //    var infosBinary = new SceneInfosBinary()
        //    {
        //        sceneInfoMap = new System.Collections.Generic.Dictionary<string, string>()
        //    };
        //    foreach(var info in infosSo.sceneInfos)
        //    {
        //        infosBinary.sceneInfoMap.Add(info.sceneName, info.assetName);
        //    }
        //    var path = FileDefine.SceneInfoCfg.StreamingPath;
        //    return Write(infosBinary, path);
        //}
        public static bool Write(ISerializable serializable, string filePath)
        {
            try
            {
                var write = new Writer();
                serializable.Serialize(write);
                var bytes = write.Finish();
                SerializationUtils.EncryptXor(ref bytes, 1023);
                var directoryPath = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                File.WriteAllBytes(filePath, bytes);
            }
            catch(Exception ex)
            {
                Debug.LogException(ex);
                return false;
            }
            Debug.Log($"序列化二进制文件成功. Path:{filePath}");
            return true;
        }
    }
}

