using UnityEngine;
using HeathenEngineering;

#if STEAMWORKS_NET && !UNITY_SERVER
using HeathenEngineering.SteamworksIntegration;
using Friends = HeathenEngineering.SteamworksIntegration.API.Friends;
using API = HeathenEngineering.SteamworksIntegration.API;
#endif

public class SteamUserNameManager : MonoBehaviour
{
    [SerializeField]
    private StringReference userName;

    // Called on the frame when a script is enabled just before any of the Update methods is called the first time
    void Awake() 
    {
#if STEAMWORKS_NET && !UNITY_SERVER
        GetUserName();
#else
        userName.Value = "";
#endif
    }

#if STEAMWORKS_NET && !UNITY_SERVER
    void GetUserName()
    {
        bool initialized = Steamworks.SteamAPI.Init();
        if (initialized == false) {
            Debug.Log("Unable to init steam api");
            return;
        }

        API.Friends.Client.RequestUserInformation(UserData.Me, false);

        API.Authentication.GetAuthSessionTicket(UserData.Me, (result, IOError) =>
        {
            if(!IOError)
            {
                Debug.Log("GetAuthSessionTicket success");
                //result.Data is your ticket data, use it well
            } else {
                Debug.Log("GetAuthSessionTicket failed");
            }
        });

        userName.Value = UserData.Me.Name;
        Debug.Log("Found steam username: " + userName.Value);
    }
#endif
}