using System.Collections;
using UnityEngine;
using System;
using UnityEngine.Networking;
using System.Text;
using GFGGame.Launcher;
using System.Reflection;
using System.Collections.Generic;
namespace GFGGame
{
    /// 
    /// Http Request SDK 
    /// 
    public class HttpTool : SingletonMonoBase
    {
        //最大尝试次数
        const int TryTimes = 3;
        //存储尝试次数
        public Dictionary TryTimesDic = new Dictionary();
        public void Get(string url, Action callback, bool showWrong = true)
        {
            StartCoroutine(GetRequest(url, callback, showWrong));
        }
        public void Get(string prefixUrl, string methodName, Action callback, bool showWrong = true)
        {
            string url = prefixUrl + methodName;
            StartCoroutine(GetRequest(url, callback, showWrong));
        }
        public IEnumerator GetRequest(string url, Action callback, bool showWrong)
        {
            var key = url;
            //Debug.Log("get url : " + key);
            using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
            {
                webRequest.timeout = LauncherConfig.HTTP_GET_TIME_OUT;
                TryTimesDic.TryGetValue(key, out var times);
                times++;
                TryTimesDic[key] = times;
                yield return webRequest.SendWebRequest();
                ResultHandler(webRequest, callback, url, () => 
                {
                    if(times < HttpTool.TryTimes)
                    {
                        Get(url, callback, showWrong);
                    }
                    else
                    {
                        if (showWrong)
                        {
                            TryTimesDic.Remove(key);
                            Debug.Log("get url : " + key);
                            Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
                            .SetRightButton(true, "重试", (object data) =>
                            {
                                Get(url, callback, showWrong);
                            });
                        }
                    }
                });
            }
        }
        public void Post(string url, string body, Action callback = null, string contentType = "application/text", bool showWrong = false)
        {
            StartCoroutine(PostRequest(url, body, callback, contentType, showWrong));
        }
        public void Post(string prefixUrl, string methodName, string body, Action callback = null, string contentType = "application/text", bool showWrong = false)
        {
            string url = prefixUrl + methodName;
            Post(url, body, callback, contentType);
        }
        private IEnumerator PostRequest(string url, string body, Action callback, string contentType, bool showWrong)
        {
            var key = url + ":" + body;
            //ET.Log.Debug(string.Format($"post {key}", url, body));
            using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
            {
                byte[] bodyRaw = Encoding.UTF8.GetBytes(body);
                webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
                webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
                webRequest.timeout = LauncherConfig.HTTP_POST_TIME_OUT;
                //http header 的内容
                webRequest.SetRequestHeader("Content-Type", contentType);
                TryTimesDic.TryGetValue(key, out var times);
                times++;
                TryTimesDic[key] = times;
                yield return webRequest.SendWebRequest();
                ResultHandler(webRequest, callback, key, () =>
                {
                    if (times < HttpTool.TryTimes)
                    {
                        Post(url, body, callback,contentType, showWrong);
                    }
                    else
                    {
                        if (showWrong)
                        {
                            TryTimesDic.Remove(key);
                            Debug.Log(string.Format($"post {key}", url, body));
                            Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
                            .SetRightButton(true, "重试", (object data) =>
                            {
                                Post(url, body, callback, contentType, showWrong);
                            });
                        }
                    }
                });
            }
        }
        public void ResultHandler(UnityWebRequest webRequest, Action callback, string tag, Action retryFunc)
        {
            string paramCallback = null;
            if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
            {
                Debug.LogWarning(webRequest.error + "\n" + webRequest.downloadHandler.text + "\nby " + tag);
                retryFunc?.Invoke();
            }
            else
            {
                //ET.Log.Debug("from server " + webRequest.downloadHandler.text + "\nby " + tag);
                paramCallback = webRequest.downloadHandler.text;
                //paramCallback = System.Text.Encoding.UTF8.GetString(webRequest.downloadHandler.data, 3, webRequest.downloadHandler.data.Length - 3);
                callback?.Invoke(paramCallback);
            }
        }
    }
}