using System;
using System.Threading.Tasks;
using UnityEngine;
using Arkadium.Interop;

namespace Arkadium
{
    public interface IArkadiumWalletV2
    {
        Task<bool> IsGemsSupportedAsync();
        Task<int> GetGemsAsync();
        Task<bool> ConsumeGemsAsync(int value);

        // Legacy callback-based methods for backward compatibility
        void IsGemsSupported(Action<bool> cb);
        void GetGems(Action<int> cb);
        void ConsumeGems(int value, Action<bool> cb);
    }

    public class ArkadiumWalletV2 : IArkadiumWalletV2
    {
        /// <summary>
        /// Check if gems are supported using async/await
        /// </summary>
        public async Task<bool> IsGemsSupportedAsync()
        {
            try
            {
                var result = await WebGLInterop.InvokeAsync<bool>("wallet.isGemsSupported");
                return result;
            }
            catch (Exception ex)
            {
                Debug.LogError($"Error checking if gems are supported: {ex.Message}");
                return false;
            }
        }

        /// <summary>
        /// Get current gems count using async/await
        /// </summary>
        public async Task<int> GetGemsAsync()
        {
            try
            {
                var result = await WebGLInterop.InvokeAsync<int>("wallet.getGems");
                return result;
            }
            catch (Exception ex)
            {
                Debug.LogError($"Error getting gems: {ex.Message}");
                return -1;
            }
        }

        /// <summary>
        /// Consume gems using async/await
        /// </summary>
        public async Task<bool> ConsumeGemsAsync(int value)
        {
            try
            {
                var result = await WebGLInterop.InvokeAsync<bool>("wallet.consumeGems", new { value });
                return result;
            }
            catch (Exception ex)
            {
                Debug.LogError($"Error consuming gems: {ex.Message}");
                return false;
            }
        }

        // Legacy callback-based methods for backward compatibility
        public void IsGemsSupported(Action<bool> cb)
        {
            _ = InvokeAsyncCallback(async () =>
            {
                var result = await IsGemsSupportedAsync();
                cb?.Invoke(result);
            });
        }

        public void GetGems(Action<int> cb)
        {
            _ = InvokeAsyncCallback(async () =>
            {
                var result = await GetGemsAsync();
                cb?.Invoke(result);
            });
        }

        public void ConsumeGems(int value, Action<bool> cb)
        {
            _ = InvokeAsyncCallback(async () =>
            {
                var result = await ConsumeGemsAsync(value);
                cb?.Invoke(result);
            });
        }

        /// <summary>
        /// Helper method to properly handle async callbacks in Unity WebGL
        /// </summary>
        private async Task InvokeAsyncCallback(Func<Task> asyncOperation)
        {
            try
            {
                await asyncOperation();
            }
            catch (Exception ex)
            {
                Debug.LogError($"Error in async callback: {ex.Message}");
            }
        }
    }
}