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 LinkAccount(EAccoutType accoutType){}
        public static void UnlinkAccount(EAccoutType accoutType){}
        public static void LinkCheck(EAccoutType accoutType){}

        public static void UnityGetIdentityFun(string type){}

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

        //支付

        public static void UnityKnow(String productId, String productName, String productCount,
                String prodorderId, String appInfo) {}
        
        //获取商品列表
        public static void GetSKUList() { }

        //打点
        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;} 

#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)
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("UnityLogin", accoutType.ToString());
            }
        }

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

        public static void UnityLogOutByChannel(EAccoutType accoutType)
        {

            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("UnityLogOutByChannel", accoutType.ToString());
            }
        }

        public static void LinkAccount(EAccoutType accoutType)
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                string userId = TYSdkFacade.TYAccountInfo.strUserId;
                sdkManager.CallStatic("LinkAccount", accoutType.ToString(), userId);
            }
        }

        public static void UnlinkAccount(EAccoutType accoutType)
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("UnlinkAccount", accoutType.ToString());
            }
        }

        public static void LinkCheck(EAccoutType accoutType)
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("LinkCheck", accoutType.ToString());
            }
        }

        //支付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 productId, String productName, String productCount, String prodorderId, String appInfo) 
        {
            using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
            {
                sdkManager.CallStatic("UnityKnow", productId,  productName, productCount, prodorderId, appInfo);
            }
        }

        //获取商品列表
        public static void GetSKUList()
        {
            UnityEngine.Debug.Log("UnityBridgeFunc.GetSKUList()");
            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;} 


#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;
                case EAccoutType.Apple:
                    UnityLoginByApple();
                    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 UnityLoginByApple();

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

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

        public static void UnityLogOutByChannel(EAccoutType accoutType)
        {
            switch (accoutType)
            {
                case EAccoutType.hwGoogle:
                    UnityLogoutGoogle();
                    break;
                case EAccoutType.hwFacebook:
                    UnityLogoutFacebook();
                    break;
                case EAccoutType.hwGuest:
                    UnityLogoutGuest();
                    break;
                case EAccoutType.Apple:
                    UnityLogoutApple();
                    break;
            }
        }

        [DllImport("__Internal")]
        private static extern void UnityLogoutGoogle();

        [DllImport("__Internal")]
        private static extern void UnityLogoutFacebook();

        [DllImport("__Internal")]
        private static extern void UnityLogoutGuest();

        [DllImport("__Internal")]
        private static extern void UnityLogoutApple();

        public static void LinkAccount(EAccoutType accoutType)
        {
            switch (accoutType)
            {
                case EAccoutType.hwGoogle:
                    LinkGoogle();
                    break;
                case EAccoutType.hwFacebook:
                    LinkFacebook();
                    break;
                case EAccoutType.Apple:
                    LinkApple();
                    break;
            }
        }

        public static void UnlinkAccount(EAccoutType accoutType)
        {
            switch (accoutType)
            {
                case EAccoutType.hwGoogle:
                    UnlinkGoogle();
                    break;
                case EAccoutType.hwFacebook:
                    UnlinkFacebook();
                    break;
            }
        }

        [DllImport("__Internal")] private static extern void LinkGoogle();
        [DllImport("__Internal")] private static extern void UnlinkGoogle();
        [DllImport("__Internal")] private static extern void LinkFacebook();
        [DllImport("__Internal")] private static extern void UnlinkFacebook();
        [DllImport("__Internal")] private static extern void LinkApple();
        [DllImport("__Internal")] private static extern void UnlinkApple();

        public static void LinkCheck(EAccoutType accoutType)
        {
            switch (accoutType)
            {
                case EAccoutType.hwGoogle:
                    IsLinkedGoogle();
                    break;
                case EAccoutType.hwFacebook:
                    IsLinkedFacebook();
                    break;

                case EAccoutType.Apple:
                    IsLinkedApple();
                    break;
            }
        }

        [DllImport("__Internal")] private static extern void IsLinkedGoogle();
        [DllImport("__Internal")] private static extern void IsLinkedFacebook();
        [DllImport("__Internal")] private static extern void IsLinkedApple();
        

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

        //获取商品列表
        [DllImport("__Internal")]
        public static extern void GetSKUList();

        //打点
        [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);

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

        [DllImport("__Internal")]
        public static extern int GetATT();
#endif
    }
}
