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
{

    private static string _IP = "172.16.1.254";
    public static string IP
    {
        get { return _IP; }
        set
        {
            if (_IP != value)
                _IP = value;
        }
    }
    private static string _Port = "48888";
    public static string Port
    {
        get { return _Port; }
        set
        {
            if (_Port != value)
                _Port = value;
        }
    }
    public static string IpAddress { get => string.Format("http://{0}:{1}", IP, Port); }




    #region 接口
    /// <summary>
    /// 登出接口 Handler/User.ashx?action=exit&login_name=test0001
    /// </summary>
    public static string Url_LoginCheck { get => IpAddress + "/Handler/User.ashx?action=exit&"; }

    /// <summary>
    /// 修改设备单个属性
    /// </summary>
    public static string Url_UpdatePracticeDevicedetail { get => IpAddress + "/Handler/Thinkingfile.ashx?action=updatepracticedevicedetail"; }

    /// <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&"; }
    /// <summary>
    /// 添加设备
    /// </summary>
    public static string Url_Adddevice { get => IpAddress + "/Handler/Thinkingfile.ashx?action=adddevice"; }
    /// <summary>
    /// 添加设备参数
    /// </summary>
    public static string Url_Adddevicepara { get => IpAddress + "/Handler/Thinkingfile.ashx?action=adddevicepara"; }
    /// <summary>
    /// 修改想定接口
    /// </summary>
    public static string Url_updatedevice { get => IpAddress + "/Handler/Thinkingfile.ashx?action=updatedevice"; }
    /// <summary>
    /// 添加模型路径参数接口
    /// </summary>
    public static string Url_updatecontent { get => IpAddress + "/Handler/Thinkingfile.ashx?action=updatecontent"; }
    /// <summary>
    /// 修改环境
    /// </summary>
    public static string Url_update_env { get => IpAddress + "/Handler/Thinkingfile.ashx?action=update_env"; }
    /// <summary>
    /// 删除房间想定设备详情
    /// </summary>
    public static string Url_Deletepracticedevicedetail { get => IpAddress + "/Handler/Thinkingfile.ashx?action=deletepracticedevicedetail"; }
    /// <summary>
    /// 获取所有得房间信息
    /// </summary>
    public static string Url_RoomList { get => IpAddress + "/Handler/Practice.ashx?action=query&state=0,1"; }
    /// <summary>
    /// 学员信息
    /// </summary>
    public static string Url_StudentList { get => IpAddress + "/Handler/User.ashx?action=all&role_code=04"; }
    /// <summary>
    /// 查询房间科目
    /// </summary>
    public static string Url_QueryPracticeSubject { get => IpAddress + "/Handler/Practice.ashx?action=querypracticesubject&PracticeId="; }
    /// <summary>
    /// 查询科目席位
    /// </summary>
    public static string Url_QueryPracticeSeat { get => IpAddress + "/Handler/Practice.ashx?action=querypracticeseat&PracticeId="; }
    /// <summary>
    /// 编辑席位信息
    /// </summary>
    public static string Url_EnditorSeatInfo { get => IpAddress + "/Handler/PracticeSeat.ashx?action=UpdatePracticeSeatOne&practiceseatId="; }

    /// <summary>
    /// 获取房间ID并添加房间
    /// </summary>
    public static string Url_GetRoomID { get => IpAddress + "/Handler/Practice.ashx?action=add&Name="; }
    /// <summary>
    /// 更新房间状态
    /// </summary>
    public static string Url_UpdateRoomState { get => IpAddress + "/Handler/Practice.ashx?action=updatestate&Id="; }
    /// <summary>
    /// 获取房间设备
    /// </summary>
    public static string Url_QueryPracticeDevice { get => IpAddress + "/Handler/Thinkingfile.ashx?action=querypracticedevicedetail&PracticeId="; }

    /// <summary>
    /// 获取想定编辑中的设备
    /// </summary>
    public static string Url_GetAllPracticeThinkDevice { get => IpAddress + "/Handler/Thinkingfile.ashx?action=practicethinkdevice&Id="; }
    /// <summary>
    /// 获取所有想定
    /// </summary>
    public static string Url_GetAllThinkData { get => IpAddress + "/Handler/Thinkingfile.ashx?action=all"; }
    /// <summary>
    /// 获取房间信息
    /// </summary>
    public static string Url_GetSceneInfo { get => IpAddress + "/Handler/Thinkingfile.ashx?action=practicethink&Id="; }
    /// <summary>
    /// 添加设备并返回设备ID
    /// </summary>
    public static string Url_AddDeviceAndGetDeviceId { get => IpAddress + "/Handler/Thinkingfile.ashx?action=addpracticedevicedetail"; }
    /// <summary>
    /// 添加房间作战日志
    /// </summary>
    public static string Url_Addpracticelog { get => IpAddress + "/Handler/Practice.ashx?action=addpracticelog"; }
    /// <summary>
    /// 查询房间作战日志
    /// </summary>
    public static string Url_Querypracticelog { get => IpAddress + "/Handler/Practice.ashx?action=querypracticelog&PracticeId="; }
    /// <summary>
    /// 删除设备
    /// </summary>
    public static string Url_delete { get => IpAddress + "/Handler/Thinkingfile.ashx?action=deletedevice"; }
    /// <summary>
    /// 删除房间
    /// </summary>
    public static string Url_Practice { get => IpAddress + "/Handler/Practice.ashx?action=updatestate&state=2&Id="; }
    /// <summary>
    /// 用户退出
    /// </summary>
    //public static string Url_Exit { get => IpAddress + "/Handler/User.ashx?action=exit&login_name="; }

    #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 PostString(string url, WWWForm formdata, Action<string> callback = null)
    {
        using (UnityWebRequest www = UnityWebRequest.Post(url, formdata))
        {
            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);
                }
            }
        }
    }
}