using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

namespace Assets.Cyp.Common
{
    public static class Utils
    {
        /*显示简略提示，需要拖Toast的预制件，且保证其他组件名字不是Toast,ToastContainer*/
        public static void showToast(GameObject game,string text,int duration = 1) 
        {
            var toast = Utils.FindUpParent(game.transform).Find("ToastContainer");
            if (toast!=null) 
            {
                toast.GetComponent<Toast>().showToast(JsonConvert.SerializeObject(new
                {
                    text,
                    duration
                }));
                //toast.SendMessage("showToast", );
            }
        }
        /*显示简略提示，需要拖Toast的预制件，且保证其他组件名字不是Toast,ToastContainer*/

        /*显示网络图片*/
        public delegate Coroutine StartCoroutine(IEnumerator routine);
        public static void DisplayImage(StartCoroutine startCoroutine,RawImage img, string url) 
        {
            startCoroutine(DownloadImage(img, url));
        }
        static IEnumerator DownloadImage(RawImage img,string MediaUrl)
        {
            UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
            yield return request.SendWebRequest();
            if (request.isNetworkError || request.isHttpError)
                Debug.Log(request.error);
            else
                img.texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
        }
        /*显示网络图片*/

        /*获取最顶层对象*/
        public static Transform FindUpParent(Transform zi)
        {
            if (zi.parent == null)
                return zi;
            else
                return FindUpParent(zi.parent);
        }
        /*获取最顶层对象*/
    }
}
