using System.Runtime.InteropServices;
using ChainSafe.Gaming.UnityPackage;
using TMPro;
using UnityEngine;

public class FirebaseManager : MonoBehaviour
{
    [DllImport("__Internal")]
    private static extern void AddDataToFirestore(string wallet, string jsonData);

    [DllImport("__Internal")]
    private static extern void GetUserProfileData(string wallet);

    [SerializeField] private TMP_InputField nameInput;
    [SerializeField] private TMP_Text nameText;
    public void SaveUserData()
    {
        string wallet = Web3Unity.Web3.Signer.PublicAddress;
        string playerName = nameInput.text;
        string jsonData = $"{{\"Name\":\"{playerName}\",\"Age\":25,\"Country\":\"Pakistan\"}}";
        AddDataToFirestore(wallet, jsonData);
    }

    public void RetrieveUserData()
    {
        string wallet = Web3Unity.Web3.Signer.PublicAddress;
        GetUserProfileData(wallet);
    }

    // This method is called from JavaScript
    public void OnUserProfileDataReceived(string jsonData)
    {
        // Deserialize JSON data
        UserProfile profile = JsonUtility.FromJson<UserProfile>(jsonData);
        // Use the profile data as needed
        Debug.Log("User Name: " + profile.Name);
        Debug.Log("User Age: " + profile.Age);
        nameText.text = "Name: " + profile.Name;
    }
}

[System.Serializable]
public class UserProfile
{
    public string Name;
    public int Age;
    public string Country;
}
