using NBC;
using NBC.Asset;
using NBF.Utils;
using UnityEngine;

namespace NBF
{
    public static class IConfigTableExtensions
    {
        
        public static string GetModelPath(this cfg.Item config)
        {
            return $"Assets/ResRaw/gfx/{config.Model}.prefab";
        }

        public static GameObject GetModelPrefab(this cfg.Item config)
        {
            return Assets.Load<GameObject>(config.GetModelPath());
        }

        public static GameObject Instantiate(this cfg.Item config, Transform parent, Vector3 position,
            Quaternion rotation)
        {
            return Object.Instantiate(config.GetModelPrefab(), position, rotation, parent);
        }


        public static T InstantiateAndComponent<T>(this cfg.Item config, Transform parent, Vector3 position,
            Quaternion rotation) where T : MonoBehaviour
        {
            var obj = config.Instantiate(parent, position, rotation);
            var com = obj.GetComponent<T>();
            if (com == null)
            {
                com = obj.AddComponent<T>();
            }

            return com;
        }

        public static T InstantiateAndComponent<T>(this cfg.Item config) where T : MonoBehaviour
        {
            var obj = Object.Instantiate(config.GetModelPrefab());
            var com = obj.GetComponent<T>();
            if (com == null)
            {
                com = obj.AddComponent<T>();
            }

            return com;
        }
    }
}