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

namespace tysdk
{

    public static class UnityBridgeFunc
    {
        
#if UNITY_EDITOR 
        //初始化sdk
        public static void InitSDK(){}
        
        //更新登录支付域名
        public static void UnityResetServerUrl(string url){}

        //登录
        public static void UnityLoginByTokenFun(string token){}
        public static void UnityLogin(EAccoutType accoutType){}

        public static void UnityGetIdentityFun(string type){}

        public static void UnityLoginOut(){}
        //支付new
        public static void UnityKnowNew(string productId, string productPrice, string productName,
            string productCount, string prodorderId, string appInfo, string pType) { }

        //支付
        public static void UnityKnow(string userId, string productId, string productPrice, string productName,
            string productCount, string prodorderId, string appInfo){}
        
        //获取商品列表
        public static void ThirdExtend() { }

        //打点
        public static void SetGaUserInfo(string userId){}
        

        public static void SetGaCommonInfo(string SetGaCommonInfo){}

        public static void GAReportParams(int type, string eventstr, string paramstr){}

        //ATT
        public static void RequestATT(){}

        public static int GetATT() {return 1;} 

        //广告相关
        public static void UnitySetAdBoxUserInfo(string userId){}
        
        public static void UnityInitADSConfig(){}
        
        public static void UnityLoadRvADS(){}

        public static void UnityShowRVAD() { }

#elif UNITY_ANDROID

        private static string SDK_CLASS = "com.unity3d.player.SDKManager";

        //初始化sdk
        public static void InitSDK(){
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                using(var activityCls = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                {
                    var activity = activityCls.GetStatic<AndroidJavaObject>("currentActivity");
                    sdkManager.CallStatic("InitSDK", activity);
                }
            }
        }
        //更新登录支付域名
        public static void UnityResetServerUrl(string url)
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("UnityResetServerUrl", url);
            }
        }

        //登录
        public static void UnityLoginByTokenFun(string token)
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("UnityLoginByTokenFun", token);
            }
        }

        public static void UnityLogin(EAccoutType accoutType)
        {
            var typeName = GetLoginTypeName(accoutType);
            Debug.LogError("typeName:::" + typeName);
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("UnityLogin", typeName);
            }
        }

        public static void UnityGetIdentityFun(string type)
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("UnityGetIdentityFun", type);
            }
        }

        public static void UnityLoginOut()
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("UnityLoginOut");
            }
        }

        public static void UnityLogOutByChannel()
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                if(PlayerPrefs.HasKey("last_login_channel"))
                {
                    sdkManager.CallStatic("UnityLogOutByChannel", PlayerPrefs.GetString("last_login_channel"));
                }
            }
        }

        //支付new
        public static void UnityKnowNew(string productId, string productPrice, string productName,
            string productCount, string prodorderId, string appInfo,string pType)
        {
            Debug.LogError("[UnityKnowNew] pType:" + pType);
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("UnityKnowNew", productId, productPrice, productName,
                        productCount, prodorderId, appInfo,pType);
            }
        }
        //支付
        public static void UnityKnow(string userId, string productId, string productPrice, string productName,
            string productCount, string prodorderId, string appInfo)
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("UnityKnow",userId, productId, productPrice, productName,
                        productCount, prodorderId, appInfo);
            }
        }

        //获取商品列表
        public static void ThirdExtend()
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("thirdExtend");
            }
        }

        //打点
        public static void SetGaUserInfo(string userId)
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("SetGaUserInfo", userId);
            }
        }

        public static void SetGaCommonInfo(string SetGaCommonInfo)
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("SetGaCommonInfo", SetGaCommonInfo);
            }
        }

        public static void GAReportParams(int type, string eventstr, string paramstr)
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("GAReportParams", type, eventstr, paramstr);
            }
        }


        //ATT
        public static void RequestATT(){}

        public static int GetATT() {return 1;} 

        //广告相关
        public static void UnitySetAdBoxUserInfo(string userId){}
        
        public static void UnityInitADSConfig(){}
        
        public static void UnityLoadRvADS(){}

        public static void UnityShowRVAD(){}
        

#elif UNITY_IOS

        //更新登录支付域名
        [DllImport("__Internal")]
        public static extern void UnityResetServerUrl(string url);

        //登录
        [DllImport("__Internal")]
        public static extern void UnityLoginByTokenFun(string token);

        public static void UnityLogin(EAccoutType accoutType)
        {
            switch (accoutType)
            {
                case EAccoutType.hwGoogle:
                    UnityLoginByGoogle();
                    break;
                case EAccoutType.hwFacebook:
                    UnityLoginByFacebook();
                    break;
                case EAccoutType.hwGuest:
                    UnityLoginByGuest();
                    break;
            }
        }

        [DllImport("__Internal")]
        public static extern void UnityLoginByGuest();

        [DllImport("__Internal")]
        public static extern void UnityLoginByGoogle();

        [DllImport("__Internal")]
        public static extern void UnityLoginByFacebook();

        [DllImport("__Internal")]
        public static extern void UnityGetIdentityFun(string type);

        [DllImport("__Internal")]
        public static extern void UnityLoginOut();

        //支付
        [DllImport("__Internal")]
        public static extern void UnityKnow(string userId, string productId, string productPrice, string productName,
            string productCount, string prodorderId, string appInfo);

        //支付new
        [DllImport("__Internal")]
        public static extern void UnityKnowNew(string productId, string productPrice, string productName,
            string productCount, string prodorderId, string appInfo, string pType);
   //获取商品列表
        [DllImport("__Internal")]
        public static extern void ThirdExtend();
        //打点
        [DllImport("__Internal")]
        public static extern void SetGaUserInfo(string userId);

        [DllImport("__Internal")]
        public static extern void SetGaCommonInfo(string SetGaCommonInfo);

        [DllImport("__Internal")]
        public static extern void GAReportParams(int type, string eventstr, string paramstr);

        //广告相关
        [DllImport("__Internal")]
        public static extern void UnitySetAdBoxUserInfo(string userId);
        
        [DllImport("__Internal")]
        public static extern void UnityInitADSConfig();
        
        [DllImport("__Internal")]
        public static extern void UnityLoadRvADS();

        [DllImport("__Internal")]
        public static extern void UnityShowRVAD();
        
        //ATT
        [DllImport("__Internal")]
        public static extern void RequestATT();

        [DllImport("__Internal")]
        public static extern int GetATT();
#endif
        
        public static string GetLoginTypeName(EAccoutType accoutType)
        {
            string str = String.Empty;
            switch (accoutType)
            {
                case EAccoutType.hwGoogle:
                    str = EAccoutTypeName.hwGoogle;
                    break;
                case EAccoutType.hwFacebook:
                    str = EAccoutTypeName.hwFacebook;
                    break;
                case EAccoutType.hwGuest:
                    str = EAccoutTypeName.hwGuest;
                    break;
            }

            return str;
        }
    }
}
