using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestSharp;

namespace Common
{
    public class ReturnInfo
    {
        public bool isok { set; get; } // 是否成功，true成功
        public string? message { set; get; } // 传递接口信息，or报错信息
        public int status { set; get; } // 服务器报错信息，正确为200，错误404/500等
        public object? response { set; get; } // 获取到的信息本体，若报错则为null
    }
    public class Http
    {
        public static string BaseUrl = "https://www.boonlive-rcu.com";
        static public async Task<ReturnInfo> HttpPostSendData(Object data, string url)
        {
            ReturnInfo r = new ReturnInfo();
            try
            {
                var options = new RestClientOptions(BaseUrl);
                var client = new RestClient(options);

                string str = System.Text.Json.JsonSerializer.Serialize(data);

                var request = new RestRequest(url);
                request.AddJsonBody(str);
                RestResponse response = await client.PostAsync(request);
                string? str1 = response.Content;
                r.isok = true;
                r.response = str1;
            }
            catch (Exception ex)
            {
                r.isok = false;
                r.response = ex.Message;
            }
            return r;
        }


        static public async Task<ReturnInfo> HttpPostFormSendData(Dictionary<string, string> data, string url)
        {
            ReturnInfo r = new ReturnInfo();
            try
            {
                var options = new RestClientOptions(BaseUrl);
                var client = new RestClient(options);

                string str = System.Text.Json.JsonSerializer.Serialize(data);
                var request = new RestRequest(url);
                foreach (var item in data)
                {
                    request.AddParameter(item.Key, item.Value);
                }

                RestResponse response = await client.PostAsync(request);
                string? str1 = response.Content;
                r.isok = true;
                r.response = str1;
            }
            catch (Exception ex)
            {
                r.isok = false;
                r.response = ex.Message;
            }
            return r;
        }

        static public async Task<ReturnInfo> HttpGetSendData(Object data, string url)
        {
            ReturnInfo r = new ReturnInfo();
            try
            {
                var options = new RestClientOptions(BaseUrl);
                var client = new RestClient(options);

                string str = System.Text.Json.JsonSerializer.Serialize(data);

                var request = new RestRequest(url);
                request.AddParameter("jsonData", str);
                RestResponse response = await client.GetAsync(request);
                string? str1 = response.Content;
                r.isok = true;
                r.response = str1;
            }
            catch (Exception ex)
            {
                r.isok = false;
                r.response = ex.Message;
            }
            return r;
        }

    }
}
