using FairyGUI;
using System;
using XGame.Framework.Asset;
using XGame.Framework.UI;

namespace XGame.Framework.FGUI
{
    public static class FguiUtils
    {
        /// <summary>
        /// 包名转ui的描述文件名(Description file data)
        /// fgui的ui数据保存在pkgName_fui.bytes文件里
        /// </summary>
        /// <param name="pkgName"></param>
        /// <returns></returns>
        public static string ToDescFileName(string pkgName)
        {
            return $"{pkgName}_fui";
        }
        /// <summary>
        /// 包名+资源名转ui路径
        /// </summary>
        /// <param name="pkgName"></param>
        /// <param name="resName"></param>
        /// <returns></returns>
        public static string ToUrl(string pkgName, string resName)
        {
            return $"ui://{pkgName}/{resName}";
        }
        /// <summary>
        /// 加载Nested并设置父节点
        /// </summary>
        /// <param name="assetModule"></param>
        /// <param name="nestedKey"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static IAssetLoadAsync<INestedView> LoadFguiNested(this IUIAssetModule assetModule, UIKey nestedKey, GComponent parent)
        {
            if (assetModule is FguiAssetModule fguiImpl)
            {
                return fguiImpl.LoadNested(nestedKey, parent);
            }
            Log.Error($"IUIAssetModule 为空. NestedKey:{nestedKey}");
            return null;
        }

        public static IGListItemHandle CreateListItem(this IUIViewAdapter viewAdapter, Type viewType, GObject gObj)
        {
            if (viewAdapter == null)
                return null;
            var view = viewAdapter.CreateNested(viewType, new FguiNested(gObj.asCom), false);
            return view as IGListItemHandle;
        }
    }
}