using System.Collections;
using System;
using UnityEngine;
using UnityEngine.Networking;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ThinkingData.Analytics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

// 考虑写成工厂模式，但由于只接了一个渠道，后面很多渠道实际上都是一样用的官方的，所以暂时不考虑
namespace NeoriginPCSDK
{
    /* 平台工厂类
     * 用于根据平台名称创建对应的操作实例
     * 目前支持 mocopc、google、dmm 等平台
     */
    public static class NeoriginPlatformFactory
    {
        // 当前平台的操作委托
        public delegate void InitializeDelegate(NeoriginSDKConfig.Platform config, NeoriginSDKDevice device,NeoriginSDKParamsInit initData, NeoriginSDK.InitCallBack callback);
        public delegate void LoginDelegate(string userAccount,int touristFlag, NeoriginSDK.LoginCallBack callback);
        public delegate void LoginAccountDelegate(NeoriginSDK.LoginAccountCallBack callback,NeoriginSDK.LoginAccountProcessCallBack processCallBack = null);
        public delegate void OpenUserCenterDelegate(NeoriginSDKCallbackDataUserInfo u8User,NeoriginSDKU8UserExtraData u8Role,NeoriginSDK.OpenUserCenterCallBack callback,NeoriginSDK.OpenUserCenterProcessCallBack processCallBack = null);
        public delegate void OpenCustomerServiceDelegate(NeoriginSDKCallbackDataUserInfo u8User,NeoriginSDKU8UserExtraData u8Role,NeoriginSDK.OpenCustomerServiceCallBack callback);
        public delegate void OpenPolicyDelegate(NeoriginSDK.OpenPolicyCallBack callback);
        public delegate void PayDelegate(NeoriginSDKPlatformPayParameter payParams,NeoriginSDK.PayCallBack callback, NeoriginSDK.PayProcessCallBack payProcessCallBack = null);
        public delegate void GetUserInfoDelegate(NeoriginSDK.GetUserInfoCallBack callback);
        public delegate NeoriginSDKPlatformUserInfo GetPlatformUserInfoDelegate();
        public delegate void GetPayItemsDelegate(NeoriginSDK.GetPayItemsCallBack getPayItemsCallBack = null);
        public delegate void LogoutDelegate(NeoriginSDK.LogoutCallBack logoutCallBack = null);

        public delegate void ShowInsufficientVirtualCoinDelegate(InterPayParams payParams);

        // 当前平台的操作委托
        public static InitializeDelegate Initialize;
        public static LoginDelegate Login;
        public static LoginAccountDelegate LoginAccount;
        public static OpenUserCenterDelegate OpenUserCenter;
        public static OpenCustomerServiceDelegate OpenCustomerService;
        public static OpenPolicyDelegate OpenPolicy;
        public static PayDelegate Pay;
        public static GetUserInfoDelegate GetUserInfo;
        public static GetPlatformUserInfoDelegate GetPlatformUserInfo;

        public static GetPayItemsDelegate GetPayItems;
        public static LogoutDelegate Logout;

        public static ShowInsufficientVirtualCoinDelegate ShowInsufficientVirtualCoin;

        public static NeoriginPlatformBaseServer platfrom_server;

        public static void SetPlatform(string platformName)
        {
            Initialize = null;
            Login = null;
            LoginAccount = null;
            OpenUserCenter = null;
            OpenCustomerService = null;
            OpenPolicy = null;
            Pay = null;
            GetUserInfo = null;
            GetPlatformUserInfo = null;
            GetPayItems = null;
            Logout = null;
            switch (platformName.ToLower())
            {
                case "mocopc":
                    platfrom_server = new NeoriginPlatformPcServer();
                    break;
                case "mocogoogle":
                    platfrom_server = new NeoriginPlatformGoogleServer();
                    break;
                case "mocosteam":
                    platfrom_server = new NeoriginPlatformSteamServer();
                    break;
                case "mocodmm":
                    break;

                default:
                    throw new Exception($"NeoriginPlatformFactory Unsupported platform: {platformName}");
            }
            Init();
        }

        public static void Init()
        {
            if (platfrom_server != null)
            {
                Initialize = platfrom_server.Initialize;
                Login = platfrom_server.Login;
                LoginAccount = platfrom_server.LoginAccount;
                OpenUserCenter = platfrom_server.OpenUserCenter;
                OpenCustomerService = platfrom_server.OpenCustomerService;
                OpenPolicy = platfrom_server.OpenPolicy;
                Pay = platfrom_server.Pay;
                GetUserInfo = platfrom_server.GetUserInfo;
                GetPlatformUserInfo = platfrom_server.GetPlatformUserInfo;
                GetPayItems = platfrom_server.GetPayItems;
                Logout = platfrom_server.Logout;
                ShowInsufficientVirtualCoin=platfrom_server.ShowInsufficientVirtualCoin;
            }
        }
    }



    public static class NeoriginPlatformManager
    {
        private static string platformName; // 用于区分不同平台的名称
        // 初始化时设置基础域名
        public static void Initialize(NeoriginSDKConfig sdkConfig, NeoriginSDKDevice device,NeoriginSDKParamsInit initData, NeoriginSDK.InitCallBack callback)
        {
            if (string.IsNullOrEmpty(sdkConfig.platform.platformName))
            {
                Debug.LogError("NeoriginPlatformManager config error.");
                return;
            }

            platformName = sdkConfig.platform.platformName;

            try
            {
                NeoriginPlatformFactory.SetPlatform(sdkConfig.platform.platformName);
                NeoriginPlatformFactory.Initialize(sdkConfig.platform, device,initData, callback);
            }
            catch (Exception ex)
            {
                Debug.Log($"Platform init error: {ex.Message}");
                callback(NeoriginSDKStatusCode.InternalError, ex.Message);
            }

        }

        // 游客登录
        public static void Login(string userAccount, int touristFlag, NeoriginSDK.LoginCallBack callback)
        {
            try
            {
                NeoriginPlatformFactory.Login(userAccount, touristFlag, callback);
            }
            catch (Exception ex)
            {
                Debug.Log($"Platform Login error: {ex.Message}");
                callback(NeoriginSDKStatusCode.InternalError, ex.Message);
            }
        }

        //账号登录
        public static void LoginAccount(NeoriginSDK.LoginAccountCallBack callback, NeoriginSDK.LoginAccountProcessCallBack processCallBack = null)
        {
            try
            {
                NeoriginPlatformFactory.LoginAccount(callback, processCallBack);
            }
            catch (Exception ex)
            {
                Debug.Log($"Platform LoginAccount error: {ex.Message}");
                callback(NeoriginSDKStatusCode.InternalError, ex.Message);
            }
        }

        //用户中心
        public static void OpenUserCenter(NeoriginSDKCallbackDataUserInfo u8User,NeoriginSDKU8UserExtraData u8Role,NeoriginSDK.OpenUserCenterCallBack callback, NeoriginSDK.OpenUserCenterProcessCallBack processCallBack = null)
        {
            try
            {
                NeoriginPlatformFactory.OpenUserCenter(u8User,u8Role,callback, processCallBack);
            }
            catch (Exception ex)
            {
                Debug.Log($"Platform OpenUserCenter error: {ex.Message}");
                callback(NeoriginSDKStatusCode.InternalError, ex.Message);
            }
        }

        //客服
        public static void OpenCustomerService(NeoriginSDKCallbackDataUserInfo u8User,NeoriginSDKU8UserExtraData u8Role,NeoriginSDK.OpenCustomerServiceCallBack callback)
        {
            try
            {
                NeoriginPlatformFactory.OpenCustomerService(u8User,u8Role,callback);
            }
            catch (Exception ex)
            {
                Debug.Log($"Platform OpenCustomerService error: {ex.Message}");
                callback(NeoriginSDKStatusCode.InternalError, ex.Message);
            }
        }

        //隐私政策
        public static void OpenPolicy(NeoriginSDK.OpenPolicyCallBack callback)
        {
            try
            {
                NeoriginPlatformFactory.OpenPolicy(callback);
            }
            catch (Exception ex)
            {
                Debug.Log($"Platform OpenPolicy error: {ex.Message}");
                callback(NeoriginSDKStatusCode.InternalError, ex.Message);
            }
        }

        //支付
        public static void Pay(NeoriginSDKPlatformPayParameter payParams, NeoriginSDK.PayCallBack callback, NeoriginSDK.PayProcessCallBack payProcessCallBack = null)
        {
            try
            {
                NeoriginPlatformFactory.Pay(payParams, callback, payProcessCallBack);
            }
            catch (Exception ex)
            {
                Debug.Log($"Platform Pay error: {ex.Message}");
                callback(NeoriginSDKStatusCode.InternalError, ex.Message);
            }
        }

        //获取用户信息
        public static void GetUserInfo(NeoriginSDK.GetUserInfoCallBack callback)
        {
            try
            {
                NeoriginPlatformFactory.GetUserInfo(callback);
            }
            catch (Exception ex)
            {
                Debug.Log($"Platform GetUserInfo error: {ex.Message}");
                callback(NeoriginSDKStatusCode.InternalError, ex.Message);
            }
        }

        //获取用户信息
        public static NeoriginSDKPlatformUserInfo GetPlatformUserInfo()
        {
            try
            {
                return NeoriginPlatformFactory.GetPlatformUserInfo();
            }
            catch (Exception ex)
            {
                Debug.Log($"Platform GetPlatformUserInfo error: {ex.Message}");
                return new NeoriginSDKPlatformUserInfo { };
            }
        }

        public static void GetPayItems(NeoriginSDK.GetPayItemsCallBack getPayItemsCallBack = null)
        {
            try
            {
                NeoriginPlatformFactory.GetPayItems(getPayItemsCallBack);
            }
            catch (Exception ex)
            {
                Debug.Log($"Platform GetPayItems error: {ex.Message}");
                getPayItemsCallBack?.Invoke(NeoriginSDKStatusCode.InternalError, ex.Message, null);
            }
        }
        
        public static void Logout(NeoriginSDK.LogoutCallBack logoutCallBack = null)
        {
            try
            {
                NeoriginPlatformFactory.Logout(logoutCallBack);
            }
            catch (Exception ex)
            {
                Debug.Log($"Platform Logout error: {ex.Message}");
                logoutCallBack?.Invoke(NeoriginSDKStatusCode.InternalError, ex.Message, null);
            }
        }

        /*
        private static void HandleGoogleInitResult(InitializeResult result)
        {
            Debug.Log("NeoriginSDK-HandleGoogleInitResult: " + result.status + " " + InitializeStatus.kStatusOk);
            if (result.status == InitializeStatus.kStatusOk)
            {
                Debug.Log("Play初始化成功!");
                // 初始化成功后
                isInitialized = true;
                // 开始游戏逻辑...
                initCallBack?.Invoke(NeoriginSDKStatusCode.Success);
            }
            else
            {
                Debug.Log($"初始化失败! 状态码: {result.status}");

                // 立即退出应用（根据SDK要求）
#if UNITY_EDITOR
                UnityEditor.EditorApplication.isPlaying = false;
#else
                Application.Quit();
#endif
            }
        }
        */
    }
}
