using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class ResMgr : BaseMgr<ResMgr> 
{
    public T Load<T>(string respath) where T:Object
    {
        T obj=Resources.Load<T>(respath);

        if (obj is GameObject)
        {
            return GameObject.Instantiate(obj);
        }
        else
        {
            return obj;
        }
    }

    public T Load<T>(string respath, Transform father, Vector3 localposition = default, Vector3 localscale = default) where T : UIBehaviour
    {
        T obj = Resources.Load<T>(respath);

        obj.transform.SetParent(father);

        if (localposition==default)
        {
            obj.transform.localPosition = Vector3.one;
        }
        else
        {
            obj.transform.localPosition = localposition;
        }

        if (localscale==default)
        {
            obj.transform.localScale = Vector3.one;
        }
        else
        {
            obj.transform.localScale = localscale;
        }

        return obj;
    }

    public void LoadAsync<T>(string respath,UnityAction<T> callback=null) where T : Object 
    {
        MonoMgr.Getinstance().StartCoroutine(InsideLoadresAsync<T>(respath,callback));
    }
    public IEnumerator InsideLoadresAsync<T>(string respath,UnityAction<T> callback=null) where T:Object
    {
        ResourceRequest r = Resources.LoadAsync<T>(respath);
        yield return r;

        if (r.asset is GameObject)
        {
            callback?.Invoke(GameObject.Instantiate(r.asset as T));
        }
        else
        {
            callback?.Invoke(r.asset as T);
        }
    }

}