using System.Runtime.InteropServices;
using UnityEngine;

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

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

        /// <summary>
        /// Set the player's score via FRVR.score.setScore().
        /// This is a fire-and-forget call: the score is reported to the FRVR
        /// platform and there is no success/error callback.
        /// </summary>
        /// <param name="score">The score to report.</param>
        public void SetScore(int score)
        {
            if (!Application.isEditor && Application.platform != RuntimePlatform.WebGLPlayer)
            {
                return;
            }

            _frvrSDK.DebugLog($"Setting score {score}");

            _frvrSDK.WrapSDKAction(
                () =>
                {
                    SetScoreSDK(score);
                },
                () =>
                {
                    _frvrSDK.DebugLog($"Editor: Simulating SetScore({score})");
                }
            );
        }

#if UNITY_WEBGL
        [DllImport("__Internal")]
        private static extern void SetScoreSDK(int score);
#else
        private static void SetScoreSDK(int score) { }
#endif
    }
}
