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

namespace Arkadium.Examples
{
    /// <summary>
    /// Example demonstrating the new promise-based interop approach
    /// </summary>
    public class WalletExample : MonoBehaviour
    {
        private ArkadiumWalletV2 _wallet;

        void Start()
        {
            _wallet = new ArkadiumWalletV2();

            // Example 1: Using async/await (recommended)
            _ = DemonstrateAsyncAwait();

            // Example 2: Using callbacks (legacy approach)
            DemonstrateCallbacks();
        }

        /// <summary>
        /// Example using the new async/await approach
        /// </summary>
        private async Task DemonstrateAsyncAwait()
        {
            Debug.Log("=== Async/Await Example ===");

            try
            {
                // Check if gems are supported
                var isSupported = await _wallet.IsGemsSupportedAsync();
                Debug.Log($"Gems supported: {isSupported}");

                if (isSupported)
                {
                    // Get current gems
                    var gems = await _wallet.GetGemsAsync();
                    Debug.Log($"Current gems: {gems}");

                    // Try to consume some gems
                    var consumed = await _wallet.ConsumeGemsAsync(100);
                    Debug.Log($"Consumed 100 gems: {consumed}");

                    // Get updated gems count
                    var newGems = await _wallet.GetGemsAsync();
                    Debug.Log($"Gems after consumption: {newGems}");
                }
            }
            catch (Exception ex)
            {
                Debug.LogError($"Error in async example: {ex.Message}");
            }
        }

        /// <summary>
        /// Example using the legacy callback approach
        /// </summary>
        private void DemonstrateCallbacks()
        {
            Debug.Log("=== Callback Example ===");

            _wallet.IsGemsSupported((isSupported) =>
            {
                Debug.Log($"Gems supported: {isSupported}");

                if (isSupported)
                {
                    _wallet.GetGems((gems) =>
                    {
                        Debug.Log($"Current gems: {gems}");

                        _wallet.ConsumeGems(50, (consumed) =>
                        {
                            Debug.Log($"Consumed 50 gems: {consumed}");

                            _wallet.GetGems((newGems) =>
                            {
                                Debug.Log($"Gems after consumption: {newGems}");
                            });
                        });
                    });
                }
            });
        }

        /// <summary>
        /// Example showing error handling with async/await
        /// </summary>
        private async Task DemonstrateErrorHandling()
        {
            Debug.Log("=== Error Handling Example ===");

            try
            {
                // This might fail if the API is not available
                var result = await _wallet.GetGemsAsync();
                Debug.Log($"Successfully got gems: {result}");
            }
            catch (Exception ex)
            {
                Debug.LogError($"Failed to get gems: {ex.Message}");
                // Handle the error gracefully
            }
        }

        /// <summary>
        /// Example showing parallel operations
        /// </summary>
        private async Task DemonstrateParallelOperations()
        {
            Debug.Log("=== Parallel Operations Example ===");

            try
            {
                // Run multiple operations in parallel
                var isSupportedTask = _wallet.IsGemsSupportedAsync();
                var gemsTask = _wallet.GetGemsAsync();

                // Wait for both to complete
                await Task.WhenAll(isSupportedTask, gemsTask);

                var isSupported = isSupportedTask.Result;
                var gems = gemsTask.Result;

                Debug.Log($"Parallel results - Supported: {isSupported}, Gems: {gems}");
            }
            catch (Exception ex)
            {
                Debug.LogError($"Error in parallel operations: {ex.Message}");
            }
        }

        // UI Button handlers for testing
        public void OnTestAsyncAwait()
        {
            _ = DemonstrateAsyncAwait();
        }

        public void OnTestCallbacks()
        {
            DemonstrateCallbacks();
        }

        public void OnTestErrorHandling()
        {
            _ = DemonstrateErrorHandling();
        }

        public void OnTestParallel()
        {
            _ = DemonstrateParallelOperations();
        }
    }
}