using System.Collections;
using System.Collections.Generic;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayFabInit : MonoBehaviour
{
    public string playFabTitleId = "7D3B9"; // Replace with your PlayFab Title ID

    void Start()
    {
        if (string.IsNullOrEmpty(PlayFabSettings.TitleId))
        {
            PlayFabSettings.TitleId = playFabTitleId;
        }
    }

    // 🚀 Called from GoogleSignInManager when Google Sign-In is successful
    public void LoginToPlayFabWithGoogle(string idToken)
    {
        Debug.Log("Logging into PlayFab with Google...");

        var request = new LoginWithGoogleAccountRequest
        {
            TitleId = PlayFabSettings.TitleId,
            ServerAuthCode = idToken, // Use ServerAuthCode instead of IdToken
            CreateAccount = true
        };

        PlayFabClientAPI.LoginWithGoogleAccount(request, OnLoginSuccess, OnLoginFailure);
    }

    private void OnLoginSuccess(LoginResult result)
    {
        Debug.Log("PlayFab Login successful!");
        GetPlayerProfile();
        GetCustomUserData();
    }

    private void GetCustomUserData()
    {
        GetPlayerProfileWithUserData();
    }

    public void GetPlayerProfileWithUserData()
    {
        var request = new GetPlayerCombinedInfoRequest
        {
            PlayFabId = PlayFabSettings.staticPlayer.PlayFabId,
            InfoRequestParameters = new GetPlayerCombinedInfoRequestParams
            {
                GetUserData = true
            }
        };

        PlayFabClientAPI.GetPlayerCombinedInfo(request, OnGetCombinedInfoSuccess, OnGetCombinedInfoFailure);
    }

    private void OnGetCombinedInfoSuccess(GetPlayerCombinedInfoResult result)
    {
        var userData = result.InfoResultPayload.UserData;
        PlayFabManager.Instance.playFabUserDataManager.myCustomData = userData;
    }

    private void OnGetCombinedInfoFailure(PlayFabError error)
    {
        Debug.LogError("Failed to get combined player info: " + error.GenerateErrorReport());
    }

    public void GetPlayerProfile()
    {
        var request = new GetPlayerProfileRequest
        {
            PlayFabId = PlayFabSettings.staticPlayer.PlayFabId,
            ProfileConstraints = new PlayerProfileViewConstraints
            {
                ShowDisplayName = true
            }
        };

        PlayFabClientAPI.GetPlayerProfile(request, OnGetProfileSuccess, OnGetProfileFailure);
    }

    private void OnGetProfileFailure(PlayFabError obj)
    {
        Debug.LogError("Couldn't Fetch Player Profile " + obj.GenerateErrorReport());
    }

    private void OnGetProfileSuccess(GetPlayerProfileResult obj)
    {
        Debug.Log("Player profile fetching successful");
        PlayFabManager.Instance.playFabUserDataManager.myProfile = obj.PlayerProfile;
    }

    private void OnLoginFailure(PlayFabError error)
    {
        Debug.LogError("PlayFab Login failed: " + error.GenerateErrorReport());
    }
}