using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class exportToServer : MonoBehaviour
{
    private void Awake()
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

    }

    private string apiUrl = "https://upload.kotiki.fun";

    // The API key (password) required to access the API
    public string token = "YOUR-TOKEN-HERE";


    // Method to start the upload process
    public void StartUpload(string fileName, string content)
    {
        StartCoroutine(UploadJson(fileName, content));
    }

    // Coroutine to handle the JSON upload process
    private IEnumerator UploadJson(string fileName, string content)
    {
        // Create a new UnityWebRequest for a POST request
        using (UnityWebRequest request = new UnityWebRequest(apiUrl + "/upload-" + fileName, "POST"))
        {

            // Set the request body content
            byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(content);
            request.uploadHandler = new UploadHandlerRaw(jsonToSend);
            request.downloadHandler = new DownloadHandlerBuffer();

            // Set the content-type and API key headers
            request.SetRequestHeader("Content-Type", "text/plain");
            request.SetRequestHeader("x-api-key", token);


            // Send the request and wait for a response
            yield return request.SendWebRequest();

            if (request.result == UnityWebRequest.Result.ConnectionError ||
            request.result == UnityWebRequest.Result.ProtocolError)
            {

                if (request.responseCode == 403)
                {
                    // Parse the JSON error
                    string json = request.downloadHandler.text;
                    Debug.Log($"Server Response: {json}");

                }
                else
                {
                    Debug.Log($"Error: {request.responseCode}");
                }
            }
            else
            {
                // Success!
                //Debug.Log("Request Successful!");
                Debug.Log(request.downloadHandler.text);
            }
        }
    }

    public string ConvertSpriteToBase64(Sprite sprite)
    {
        if (sprite == null) return null;

        // Get the texture from the sprite
        Texture2D texture = sprite.texture;

        // Create a readable Texture2D in a compatible format (RGBA32)
        Texture2D readableTexture = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height, TextureFormat.RGBA32, false);

        // Copy the pixels from the original texture to the new readable texture
        Color[] pixels = texture.GetPixels((int)sprite.rect.x, (int)sprite.rect.y, (int)sprite.rect.width, (int)sprite.rect.height);
        readableTexture.SetPixels(pixels);
        readableTexture.Apply();

        // Convert the texture to PNG format (or JPG if preferred)
        byte[] imageData = readableTexture.EncodeToPNG(); // Or EncodeToJPG for JPG format

        // Convert byte array to base64 string
        string base64String = System.Convert.ToBase64String(imageData);

        return base64String;
    }


    void Start()
    {
        
    }

}
