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

namespace FRVR
{
    public class FeaturesModule : MonoBehaviour
    {
        private FRVRSDK _frvrSDK;
        
        // Callback storage for IL2CPP compatibility
        private Action<string> _getFeatureCallback;

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

    public void GetFeature(string featureId, Action<string> callback)
    {
        if (!Application.isEditor && Application.platform != RuntimePlatform.WebGLPlayer)
        {
            return;
        }

        _frvrSDK.DebugLog($"Getting feature: {featureId}");

        _getFeatureCallback = callback;

        _frvrSDK.WrapSDKAction(
            () =>
            {
                GetFeatureSDK(featureId);
            },
            () =>
            {
                // Editor simulation
                _frvrSDK.DebugLog($"Editor: Simulating GetFeature - {featureId}");
                callback("{\"enabled\":true}");
            }
        );
    }

        // JSLib callback methods
        internal void HandleGetFeatureComplete(string result)
        {
            _getFeatureCallback?.Invoke(result);
        }

#if UNITY_WEBGL
        [DllImport("__Internal")]
        private static extern void GetFeatureSDK(string featureId);
#else
        private void GetFeatureSDK(string featureId) { }
#endif
    }
}
