using Giant.Common;
using Giant.Res;
using System;
using System.IO;
using UnityEngine.SceneManagement;

#if UNITY_EDITOR
namespace Giant.Resource {
    /// <summary>
    /// 编辑器下 离散资源加载
    /// </summary>
    public class EditorResourceLoader : IResourceLoader
    {
        public override void LoadAssetAsync(string strAssetPath, string strAssetName, string strSuffix, LoadCallback callback = null, bool bShader = false, Type t = null)
        {
            string tmpPath = strAssetPath.Replace(AssetPath, "");
            if (tmpPath.IndexOf("/") == 0)
                tmpPath = tmpPath.Substring(1);


            VString abPath = VString.Empty;
            if (ResourceloadConfig.useAssetDataBase)
            {
                abPath = VString.Concat(AssetPath, tmpPath, strAssetName, strSuffix, AllocType.Persistent);
            }
            else
            {
                abPath = VString.Concat(IResourceLoader.strAsset, tmpPath, strAssetName, AllocType.Persistent);
                //abPath = VString.Concat(IResourceLoader.strAsset, tmpPath, strName, AllocType.Tmp);
            }
            AssetBundleManager.Instance.LoadResourceAsync(abPath, (UnityEngine.Object o) =>
            {
                if (null != o)
                {
                    if (null != callback)
                        callback(o);
                    return;
                }
                else
                {
                    LogHelper.LogWarning(string.Format("resource load fail {0}", strAssetName));
                }
            }, t);
            abPath.Dispose();
        }

        public override ResourceLoadAsync LoadAssetAsync(string strAssetPath, string strAssetName, string strSuffix, bool bShader = false, Type t = null)
        {
            ResourceLoadAsync resourceLoadAsync = new ResourceLoadAsync();

            string tmpPath = strAssetPath.Replace(AssetPath, "");
            if (tmpPath.IndexOf("/") == 0)
                tmpPath = tmpPath.Substring(1);


            VString abPath = VString.Empty;
            if (ResourceloadConfig.useAssetDataBase)
            {
                abPath = VString.Concat(AssetPath, tmpPath, strAssetName, strSuffix, AllocType.Persistent);
            }
            else
            {
                abPath = VString.Concat(IResourceLoader.strAsset, tmpPath, strAssetName, AllocType.Persistent);
                //abPath = VString.Concat(IResourceLoader.strAsset, tmpPath, strName, AllocType.Tmp);
            }
            AssetBundleManager.Instance.LoadResourceAsync(abPath, (UnityEngine.Object o) =>
            {
                if (null != o)
                {
                    resourceLoadAsync.asset = o;
                    resourceLoadAsync.isDone = true;
                    // return resourceLoadAsync;
                }
                else
                {
                    LogHelper.LogWarning(string.Format("resource load fail {0}", strAssetName));
                }
            }, t);
            abPath.Dispose();
            return resourceLoadAsync;
        }

        public override UnityEngine.Object LoadAssetSync(string strAssetPath, string strAssetName, string strSuffix, bool bShader = false, Type t = null)
        {
            UnityEngine.Object result = null;

            string tmpPath = strAssetPath.Replace(AssetPath, "");
            if (tmpPath.IndexOf("/") == 0)
                tmpPath = tmpPath.Substring(1);
            VString abPath = VString.Empty;
            if (ResourceloadConfig.useAssetDataBase)
            {
                abPath = VString.Concat(AssetPath, tmpPath, strAssetName, strSuffix, AllocType.Tmp);
            }
            else
            {
                abPath = VString.Concat(IResourceLoader.strAsset, tmpPath, strAssetName, AllocType.Tmp);
            }
            result = AssetBundleManager.Instance.LoadResourceSync(abPath, t);

            abPath.Dispose();


            return result;
        }

      
        public override ResourceLoadAsync LoadSceneAsync(string scenepath, LoadSceneMode mode)
        {
            string sceneName = System.IO.Path.GetFileName(scenepath);
            Giant.Common.VString folder = Giant.Common.VString.From(Giant.Resource.IResourceLoader.abpath, Giant.Common.AllocType.Tmp);
            Giant.Common.VString sn = Giant.Common.VString.From(scenepath, Giant.Common.AllocType.Tmp);
            sn.ToLower();
            ResourceLoadAsync request = new ResourceLoadAsync();
            // request.sceneName = sceneName;

            var aopt = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(Giant.Resource.IResourceLoader.strAssetFolder + scenepath + ".unity", new LoadSceneParameters(mode));
            aopt.completed += (sc) =>
            {
                //request.asset = sc.;
                request.isDone = true;
            };

            return request;
        }

        public override void UnLoadScene(string scenepath)
        {
            string sceneName = System.IO.Path.GetFileName(scenepath);
            //var opt = SceneManager.UnloadSceneAsync(sceneName);
        }

        public override SceneLoadAsync LoadOutSceneAsync(string sceneName, LoadSceneMode mode)
        {
            string scenepath = "outscenes/" + sceneName;
            Giant.Common.VString folder = Giant.Common.VString.From(Giant.Resource.IResourceLoader.abpath, Giant.Common.AllocType.Tmp);
            Giant.Common.VString sn = Giant.Common.VString.From(scenepath, Giant.Common.AllocType.Tmp);
            sn.ToLower();
            SceneLoadAsync request = new SceneLoadAsync();
            // request.sceneName = sceneName;
            var aopt = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(GetOutScenePath(sceneName), new LoadSceneParameters(mode));
            aopt.completed += (sc) =>
            {
                request.scene = SceneManager.GetSceneByName(sceneName);
                //request.asset = sc;
                request.isDone = true;
            };

            return request;
        }

        public override void UnOutLoadScene(string sceneName)
        {
            var opt = SceneManager.UnloadSceneAsync(sceneName);
        }

        private string GetOutScenePath(string scenename)
        {
            var path = Path.GetFileNameWithoutExtension(scenename);
#if UNITY_EDITOR
            var guids = UnityEditor.AssetDatabase.FindAssets(string.Format("{0} t:Scene", path));
            foreach (var guid in guids)
            {
                string realPath = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
                string realName = Path.GetFileNameWithoutExtension(realPath);
                if (realName == path)
                {
                    path = realPath;
                    break;
                }
            }
#endif
            return path;
        }
    }
}

#endif