using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.U2D;

namespace XGame.Editor.Asset.Tools
{
    internal class AssetInfoItem : IReorderableItem, System.IDisposable, IReorderableItemData<AssetInfoData>
    {
        private AssetInfoData _data;
        private string _assetName;
        private Object _assetObj;
        private bool _isAddressableAsset;
        //private SpriteAtlas _spriteAtlas;
        public float Height { get; private set; }

        public AssetInfoData Data => _data;

        public AssetInfoItem(AssetInfoData data)
        {
            _data = data;
            var assetPath = data.assetPath;
            _assetName = Path.GetFileNameWithoutExtension(assetPath);
            _assetObj = AssetDatabase.LoadMainAssetAtPath(assetPath);
            _isAddressableAsset = assetPath.Contains(PathDefine.ResAddressableName);
            //FindSpriteAtlas();
        }
        //private void FindSpriteAtlas()
        //{
        //    var assetPath = _data.assetPath;
        //    if (FileUtil.IsTextureFile(assetPath))
        //    {
        //        var dirName = Path.GetDirectoryName(assetPath);
        //        while (!string.IsNullOrEmpty(dirName))
        //        {
        //            var files = Directory.GetFiles(dirName, "*.spriteatlas");
        //            if (files.Length > 0)
        //            {
        //                _spriteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(files[0]);
        //                //Debug.Log($"_spriteAtlas is {_spriteAtlas != null}  Path:{files[0]} asset:{assetPath}");
        //                break;
        //            }
        //            else
        //            {
        //                dirName = Path.GetDirectoryName(dirName);
        //            }
        //        }
        //    }
        //}

        void IReorderableItem.OnGUI(Rect rect)
        {
            Height = 0;
            var lineH = EditorGUIUtility.singleLineHeight;
            var tempRect = rect;
            tempRect.x += 14;
            tempRect.width -= 14;
            tempRect.height = lineH;
            //展示区域，禁止操作
            EditorGUI.BeginDisabledGroup(true);
            //var assetContent = new GUIContent("Asset", "Asset Object");
            var color = GUI.color;
            if (_isAddressableAsset)
            {
                GUI.color = Color.red;
            }
            EditorGUI.ObjectField(tempRect, _assetName, _assetObj, typeof(Object), false);
            if (_isAddressableAsset)
            {
                GUI.color = color;
            }
            tempRect.y += lineH;
            Height += lineH;
            //if (_spriteAtlas != null)
            //{
            //    EditorGUI.ObjectField(tempRect, "图集", _spriteAtlas, typeof(SpriteAtlas), false);
            //    tempRect.y += lineH;
            //    Height += lineH;
            //}
            

            EditorGUI.EndDisabledGroup();
            
        }

        void System.IDisposable.Dispose()
        {
            _data = null;
            _assetObj = null;

        }
    }
}
