using System;
using UnityEngine;
using Object = UnityEngine.Object;

namespace XGame.Framework.Asset
{
    internal static class Extensions
    {
        /// <summary>
        /// object转泛型对象的扩展
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="value"></param>
        /// <param name="addressableName">用于转换错误时将名字打印出来</param>
        /// <returns></returns>
        public static TResult Convert<TResult>(this object value, string addressableName)
        {
            if (value != null)
            {
                Type type = typeof(TResult);
                if (type.IsInstanceOfType(value))
                {
                    return (TResult)value;
                }
                if (type.IsClass)
                {
                    if (type.IsSubclassOf(typeof(Component)))
                    {
                        var go = value as GameObject;
                        if (go != null)
                        {
                            return go.GetComponent<TResult>();
                        }
                        var component = value as Component;
                        if (component != null)
                        {
                            return component.gameObject.GetComponent<TResult>();
                        }
                    }
                    else if (type == typeof(Sprite) && value is Texture2D texture)
                    {
                        object sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
                        return (TResult) sprite;
                    }
                }
                try
                {
                    TResult result = (TResult)value;
                    return result;
                }
                catch (Exception ex)
                {
                    AssetsLog.Error($"object conver type Error. source type:{value.GetType()} name:{value} target:{type} addressable:{addressableName}");
                    AssetsLog.Exception(ex);
                }
            }
            return default;
        }
        /// <summary>
        /// 返回object的long格式的hashCode
        /// UnityEngine.Object返回的是instanceId << 32
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static long GetLongHashCode(this object obj)
        {
            if (obj == null) return 0;
            if (obj is Object uniObj)
            {
                long instanceId = uniObj.GetInstanceID();
                instanceId <<= 32;
                return instanceId;
            }
            return obj.GetHashCode();
        }
        /// <summary>
        /// 释放Object对象
        /// 独立资源才能使用Resources.UnloadAsset
        /// </summary>
        /// <param name="asset"></param>
        /// <returns></returns>
        public static bool Unload(this Object asset)
        {
            if (asset == null || asset is Component || asset is GameObject || asset is AssetBundle)
            {
                //非独立资源
            }
            else
            {
                //独立资源才能使用Resources.UnloadAsset
                Resources.UnloadAsset(asset);
                return true;
            }
            return false;
        }

        //public static string SaveToLower(this string msg)
        //{
        //    if (string.IsNullOrEmpty(msg))
        //    {
        //        return msg;
        //    }
        //    return msg.ToLower();
        //}
    }
}
