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

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

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

    public void LogEvent(string eventName, string jsonDataString)
    {
        if (!Application.isEditor && Application.platform != RuntimePlatform.WebGLPlayer)
        {
            return;
        }

        _frvrSDK.DebugLog($"Logging event: {eventName} with data: {jsonDataString}");

        _frvrSDK.WrapSDKAction(
            () =>
            {
                LogEventSDK(eventName, jsonDataString);
            },
            () =>
            {
                // Editor simulation
                _frvrSDK.DebugLog($"Editor: Simulating LogEvent - {eventName}");
            }
        );
    }

    public void LogFTUE(int stepNumber, string stepName)
    {
        if (!Application.isEditor && Application.platform != RuntimePlatform.WebGLPlayer)
        {
            return;
        }

        _frvrSDK.DebugLog($"Logging FTUE: Step {stepNumber} - {stepName}");

        _frvrSDK.WrapSDKAction(
            () =>
            {
                LogFTUESDK(stepNumber, stepName);
            },
            () =>
            {
                // Editor simulation
                _frvrSDK.DebugLog($"Editor: Simulating LogFTUE - Step {stepNumber}: {stepName}");
            }
        );
    }

    public void LogLevelStart(string levelId)
    {
        if (!Application.isEditor && Application.platform != RuntimePlatform.WebGLPlayer)
        {
            return;
        }

        _frvrSDK.DebugLog($"Logging level start: {levelId}");

        _frvrSDK.WrapSDKAction(
            () =>
            {
                LogLevelStartSDK(levelId);
            },
            () =>
            {
                // Editor simulation
                _frvrSDK.DebugLog($"Editor: Simulating LogLevelStart - {levelId}");
            }
        );
    }

    public void LogLevelEnd(string levelId)
    {
        if (!Application.isEditor && Application.platform != RuntimePlatform.WebGLPlayer)
        {
            return;
        }

        _frvrSDK.DebugLog($"Logging level end: {levelId}");

        _frvrSDK.WrapSDKAction(
            () =>
            {
                LogLevelEndSDK(levelId);
            },
            () =>
            {
                // Editor simulation
                _frvrSDK.DebugLog($"Editor: Simulating LogLevelEnd - {levelId}");
            }
        );
    }

#if UNITY_WEBGL
        [DllImport("__Internal")]
        private static extern void LogEventSDK(string eventName, string jsonDataString);
        
        [DllImport("__Internal")]
        private static extern void LogFTUESDK(int stepNumber, string stepName);
        
        [DllImport("__Internal")]
        private static extern void LogLevelStartSDK(string levelId);
        
        [DllImport("__Internal")]
        private static extern void LogLevelEndSDK(string levelId);
#else
        private void LogEventSDK(string eventName, string jsonDataString) { }
        private void LogFTUESDK(int stepNumber, string stepName) { }
        private void LogLevelStartSDK(string levelId) { }
        private void LogLevelEndSDK(string levelId) { }
#endif
    }
}
