using System;
using System.Runtime.InteropServices;
using UnityEngine;

namespace FRVR
{
    public class AuthModule : MonoBehaviour
    {
        private FRVRSDK _frvrSDK;

        public void Init(FRVRSDK frvrSDK)
        {
            _frvrSDK = frvrSDK;
        }

        /// <summary>
        /// Get the FRVR ID for the current user
        /// </summary>
        /// <returns>The FRVR ID as a UUID string, or empty string if not available</returns>
        public string GetFRVRID()
        {
            if (!Application.isEditor && Application.platform != RuntimePlatform.WebGLPlayer)
            {
                return string.Empty;
            }

            _frvrSDK.DebugLog("Getting FRVR ID");

            return _frvrSDK.WrapSDKFunc(
                () =>
                {
                    return GetFRVRIDSDK();
                },
                () =>
                {
                    // Editor simulation - return a fake UUID
                    _frvrSDK.DebugLog("Editor: Returning simulated FRVR ID");
                    return "00000000-0000-0000-0000-000000000000";
                }
            );
        }

        /// <summary>
        /// Get the access token (JWT) for the current user
        /// </summary>
        /// <returns>The access token as a JWT string, or empty string if not available</returns>
        public string GetAccessToken()
        {
            if (!Application.isEditor && Application.platform != RuntimePlatform.WebGLPlayer)
            {
                return string.Empty;
            }

            _frvrSDK.DebugLog("Getting Access Token");

            return _frvrSDK.WrapSDKFunc(
                () =>
                {
                    return GetAccessTokenSDK();
                },
                () =>
                {
                    // Editor simulation - return a fake token
                    _frvrSDK.DebugLog("Editor: Returning simulated Access Token");
                    return "editor_simulated_token";
                }
            );
        }

        /// <summary>
        /// Check if the user is currently logged in
        /// </summary>
        /// <returns>True if the user is logged in, false otherwise</returns>
        public bool IsLoggedIn()
        {
            if (!Application.isEditor && Application.platform != RuntimePlatform.WebGLPlayer)
            {
                return false;
            }

            _frvrSDK.DebugLog("Checking IsLoggedIn");

            return _frvrSDK.WrapSDKFunc(
                () =>
                {
                    return IsLoggedInSDK();
                },
                () =>
                {
                    // Editor simulation - return true for testing
                    _frvrSDK.DebugLog("Editor: Returning simulated IsLoggedIn = true");
                    return true;
                }
            );
        }

#if UNITY_WEBGL
        [DllImport("__Internal")]
        private static extern string GetFRVRIDSDK();

        [DllImport("__Internal")]
        private static extern string GetAccessTokenSDK();

        [DllImport("__Internal")]
        private static extern bool IsLoggedInSDK();
#else
        private static string GetFRVRIDSDK() { return string.Empty; }
        private static string GetAccessTokenSDK() { return string.Empty; }
        private static bool IsLoggedInSDK() { return false; }
#endif
    }
}
