using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public static class InterfaceManager
{
   
    public static string IP { get => "111.229.30.246:"; }
    public static string Port { get => "48888"; }
    public static string IpAddress { get => string.Format("http://{0}:{1}", IP, Port); }




    #region 接口

    /// <summary>
    /// 想定Wbe接口
    /// </summary>
    public static string Url_Action { get => IpAddress + "/Handler/Thinkingfile.ashx?action=all"; }
    /// <summary>
    /// 修改想定设备参数
    /// </summary>
    public static string Url_Updatedevicepara { get => IpAddress + "/Handler/Thinkingfile.ashx?action=updatedevicepara"; }
    /// <summary>
    /// 查询想定设备参数
    /// </summary>
    public static string Url_Querydevicepara { get => IpAddress + "/Handler/Thinkingfile.ashx?action=querydevicepara&"; }
    /// <summary>
    /// 查询想定设备
    /// </summary>
    public static string Url_Querydevice { get => IpAddress + "/Handler/Thinkingfile.ashx?action=querydevice&"; }
    
    #endregion

    public static string GetLocalTxt(string path)
    {
        using (System.IO.StreamReader reader = new System.IO.StreamReader(path)) { return reader.ReadToEnd(); }
    }

    

    public static IEnumerator GetBytes(string url, Action<string> callback)
    {
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {
            yield return www.SendWebRequest();
            if (www.isNetworkError || www.isHttpError)
            {
                callback(null);
                Debug.Log("网络请求失败了");
            }
            else
            {
                if (!string.IsNullOrEmpty(www.downloadHandler.text))
                {
                    callback(www.downloadHandler.text);
                }
                else
                {
                    callback(null);
                }
            }
        }
    }

    public static IEnumerator GetString(string url, Action<string> callback)
    {
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {
            yield return www.SendWebRequest();
            if (www.isNetworkError || www.isHttpError)
            {
                callback(null);
                Debug.Log("网络请求失败了");
            }
            else
            {
                if (!string.IsNullOrEmpty(www.downloadHandler.text))
                {
                    callback(www.downloadHandler.text);
                }
                else
                {
                    callback(null);
                }
            }
        }
    }
    public static IEnumerator GetString(string url, int time, Action<string> callback)
    {
        while (true)
        {
            using (UnityWebRequest www = UnityWebRequest.Get(url))
            {
                yield return new WaitForSeconds(time); // 每隔 time 秒调用一次接口

                yield return www.SendWebRequest();
                if (www.isNetworkError || www.isHttpError)
                {
                    callback(null);
                    Debug.Log("网络请求失败了");
                }
                else
                {
                    if (!string.IsNullOrEmpty(www.downloadHandler.text))
                    {
                        callback(www.downloadHandler.text);
                    }
                    else
                    {
                        callback(null);
                    }
                }
            }
        }
    }

  


    

    public static IEnumerator PostString(string url, WWWForm formdata, Dictionary<string, string> headers = null, Action<string> callback = null)
    {
        using (UnityWebRequest www = UnityWebRequest.Post(url, formdata))
        {
            foreach (var item in headers)
            {
                www.SetRequestHeader(item.Key, item.Value);
            }
            yield return www.SendWebRequest();
            if (www.isNetworkError || www.isHttpError)
            {
                callback(null);
                Debug.Log("网络请求失败了");
            }
            else
            {
                if (!string.IsNullOrEmpty(www.downloadHandler.text))
                {
                    callback(www.downloadHandler.text);
                }
                else
                {
                    callback(null);
                }
            }
        }
    }

    public static IEnumerator GetString(string url, Dictionary<string, string> headers = null,int time=50,Action<string> callback = null)
    {
        while (true)
        {
            using (UnityWebRequest www = UnityWebRequest.Get(url))
            {
                yield return new WaitForSeconds(time); // 每隔 time 秒调用一次接口
                foreach (var item in headers)
                {
                    www.SetRequestHeader(item.Key, item.Value);
                }
                yield return www.SendWebRequest();
                if (www.isNetworkError || www.isHttpError)
                {
                    callback(null);
                    Debug.Log("网络请求失败了");
                }
                else
                {
                    if (!string.IsNullOrEmpty(www.downloadHandler.text))
                    {
                        callback(www.downloadHandler.text);
                    }
                    else
                    {
                        callback(null);
                    }
                }
            }
        }
    }

    public static IEnumerator GetString(string url, Dictionary<string, string> headers = null, Action<string> callback = null)
    {
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {
            foreach (var item in headers)
            {
                www.SetRequestHeader(item.Key, item.Value);
            }
            yield return www.SendWebRequest();
            if (www.isNetworkError || www.isHttpError)
            {
                callback(null);
                Debug.Log("网络请求失败了");
            }
            else
            {
                if (!string.IsNullOrEmpty(www.downloadHandler.text))
                {
                    callback(www.downloadHandler.text);
                }
                else
                {
                    callback(null);
                }
            }
        }
    }
}