using Firebase.Database;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class PropositionHandler : MonoBehaviour
{
    public GameManager gameManager;
    public StorageManager storageManager;
    public TextMeshProUGUI promptLabel;
    public PromptList prompts;
    public Sprite defaultImage;
    public Image[] btns;

    public void ShowPlayersProposition(Question currentQuestion)
    {
        List<Proposition> props = currentQuestion.propositions.Values.ToList();
        string prompt = GameManager.Instance.myRoom.promptsLanguage == "en" ? prompts.GetPromptById(currentQuestion.promptId).en : prompts.GetPromptById(currentQuestion.promptId).fr;
        promptLabel.text = prompt;
        Debug.Log($"Showing Question ({currentQuestion.index}) : {prompt}");
        for (int i = 0; i < 2; i++)
        {
            int index = i; // Capture la valeur de i pour cette itération
            if(string.IsNullOrEmpty(props[i].photoUrl))
            {
                Debug.Log("One player did not send a proposition !", this);
                btns[index].sprite = defaultImage; // Utilise l'index local au lieu de i
            }
            else
            {
                StorageManager.ConvertGoogleStorageURLToHttpsUrl(props[i].photoUrl, _httpURL =>
                {
                    StartCoroutine(StorageManager.DownloadImage_Coroutine(_httpURL, (Texture texture) =>
                    {
                        Sprite sprite = Sprite.Create(texture as Texture2D, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
                        btns[index].sprite = sprite; // Utilise l'index local au lieu de i
                    }));
                });
            }
        }

    }

}
