using UnityEngine;
using BestHTTP;
using System;
using Newtonsoft.Json;
using MEC;
using System.Collections.Generic;


public class NetworkManager : SingletonMonoBehaviour<NetworkManager>
{
    [SerializeField] private string baseUrl;

    /// <summary>
    /// �����͸� �ۼ����ؾ��� �� ����մϴ�. send()�� ������ ��ġ�� �ڵ����� �����ϴ�.
    /// </summary>
    /// <typeparam name="T">������ȭ�� Ÿ��</typeparam>
    /// <param name="API">api �ּ�</param>
    /// <param name="onRequestFinished">������ ���� �� ó���� ����</param>
    /// <param name="serializeData">������ ������, null�� ������ �Ⱥ����ϴ�.</param>
    /// <param name="methods">���۹��</param>
    /// <returns>������ȭ�� ������ or �⺻��(��ȯ ���� ����)</returns>
    public HTTPRequest CreateRequest<T>(string api, object serializeData, Action<T> onSuccess, Action<T> onFail = null, HTTPMethods methods = HTTPMethods.Post)
    {
        string url = $"{baseUrl}{api}".Replace("\\p{Z}", String.Empty);

        var httpRquest = new HTTPRequest(new Uri(url) //url �ֱ�
            , methods //���� ��� �ֱ�
            , (request, response) => {
                if (response == null || (response.StatusCode != 200))
                {
                    onFail?.Invoke(default(T));
                }
                else
                {
                    T deSerialized = JsonConvert.DeserializeObject<T>(response.DataAsText);
                    onSuccess?.Invoke(deSerialized);
                }
            });

        if (serializeData != null)
        {
            httpRquest.SetHeader("Content-Type", "application/json; charset=UTF-8");
            string json = JsonConvert.SerializeObject(serializeData);
            httpRquest.RawData = System.Text.Encoding.UTF8.GetBytes(json);
        }

        httpRquest.Send();
        return httpRquest;
    }
    public HTTPRequest CreateImageRequest(string api, object serializeData, Action<Texture2D> onSuccess, Action<Texture2D> onFail = null, HTTPMethods methods = HTTPMethods.Post)
    {
        string url = $"{baseUrl}{api}".Replace("\\p{Z}", String.Empty);

        var httpRquest = new HTTPRequest(new Uri(url) //url �ֱ�
            , methods //���� ��� �ֱ�
            , (request, response) => {
                if (response == null || (response.StatusCode != 200))
                {
                    onFail?.Invoke(null);
                }
                else
                {
                    Texture2D deSerialized = response.DataAsTexture2D;
                    onSuccess?.Invoke(deSerialized);
                }
            });

        if (serializeData != null)
        {
            httpRquest.SetHeader("Content-Type", "application/json; charset=UTF-8");
            string json = JsonConvert.SerializeObject(serializeData);
            httpRquest.RawData = System.Text.Encoding.UTF8.GetBytes(json);
        }

        return httpRquest;
    }

    private IEnumerator<float> Sendearch()
    {
        //Timing.RunCoroutine(BuildingSet());
        yield return Timing.WaitForSeconds(0.1f);
    }
}
