using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;

using Touka.GameLogic;

public class TGTools : ToukaSingletonMonoBehaviour<TGTools>
{

    public Action onRemoteConfigHandler;
    public Action onAgreeHander;
    private bool isInit = false;
#if UNITY_ANDROID && !UNITY_EDITOR
    protected static AndroidJavaObject Context = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");

#endif
    void Awake()
    {

        if (this != Instance)
        {
            DontDestroyOnLoad(gameObject);
            return;
        }
        DontDestroyOnLoad(gameObject);
    }



#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
    [DllImport("__Internal")]
    private static extern void initUMOlineConfig(string appKey, string channel);
    [DllImport("__Internal")]
    private static extern void setDefaults(string defaults);
    [DllImport("__Internal")]
    private static extern void setDefaultsFromPlistFileName(string fileName);
    [DllImport("__Internal")]
    private static extern string configValueForKey(string key);
    [DllImport("__Internal")]
    public static extern void remoteConfigActivatedCallback(
        IntPtr remoteConfig
    );
    
    [DllImport("__Internal")]
    private static extern void registerAPNS();
     [DllImport("__Internal")]
    private static extern void registerNotification(string name, string text, string fireDate);
     [DllImport("__Internal")]
    private static extern void removeNotification(string nofiName);
     [DllImport("__Internal")]
    private static extern void removeAllNotification();

    //应用内评价
    [DllImport("__Internal")]
    private static extern void showAppStoreReView();

#if IOS_CN
    //用户协议弹框回调
    [DllImport("__Internal")]
    public static extern void userAgreementAgreeCallback(
        IntPtr userAgree
    );
    //用户协议弹框
    [DllImport("__Internal")]
    private static extern void showUserAgreementView(string contentType,string ageType);
#endif
#endif

    // 友盟在线参数
    // 初始化，appkey 同友盟初始化key ，channel 一版填写 " app store "
    public void InitSdk(string appKey, string channel)
    {


#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        //设置回调托管函数指针
        remoteConfigActivated handler = new remoteConfigActivated(remoteConfigActivatedHandle);
        IntPtr remoteConfig = Marshal.GetFunctionPointerForDelegate(handler);
        //调用OC的方法，将C#的回调方法函数指针传给OC
        remoteConfigActivatedCallback(remoteConfig);

        initUMOlineConfig(appKey, channel);

#elif UNITY_ANDROID

#endif
}

// 设置默认值，传入json 字符串  (可不设置，不设置默认值时，取不到远端值时，返回空字符串)
public void SetDefaults(string defaults)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK

        setDefaults(defaults);

#elif UNITY_ANDROID

#endif
    }

    // 通过 plist 文件设置默认值，需 plist 文件导出至xcode工程里，这个参数是 plist 文件名称  (可不设置，不设置默认值时，取不到远端值时，返回空字符串)
    public void SetDefaultWithFileName(string fileName)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK

        setDefaultsFromPlistFileName(fileName);

#elif UNITY_ANDROID

#endif
    }

    // 通过 key 去取 在线参数
    public string ConfigValueForKey(string key)
    {
#if UNITY_EDITOR

        return "";

#elif UNITY_ANDROID

        return configValueForKeyAndroid(key);

#elif UNITY_IPHONE && !NO_SDK
        return configValueForKey(key);
   
#endif
        return "";

    }

    public string configValueForKeyAndroid(string key)
    {
        string result = "";
        //try

        //{
        //    AndroidJavaClass jc = new AndroidJavaClass("com.toukagames.toukaumengsdk.UmengRemoteConfig");
        //    result = jc.CallStatic<string>("getValueByKey", key);
        //}
        //catch (Exception e)

        //{
        //    Debug.LogException(e);

        //}
        return result;
    }

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void remoteConfigActivated();
    // 接收在线参数回调
    [AOT.MonoPInvokeCallback(typeof(remoteConfigActivated))]
    static void remoteConfigActivatedHandle()
    {
        Instance.onRemoteConfigHandler?.Invoke();
    }


    /*
    *      
    *          ┌─┐       ┌─┐
    *       ┌──┘ ┴───────┘ ┴──┐
    *       │                 │
    *       │       ───       │
    *       │  ─┬┘       └┬─  │
    *       │                 │
    *       │       ─┴─       │
    *       │                 │
    *       └───┐         ┌───┘
    *           │         │
    *           │         │
    *           │         │
    *           │         └──────────────┐
    *           │                        │
    *           │                        ├─┐
    *           │                        ┌─┘    
    *           │                        │
    *           └─┐  ┐  ┌───────┬──┐  ┌──┘         
    *             │ ─┤ ─┤       │ ─┤ ─┤         
    *             └──┴──┘       └──┴──┘ 
    *             
    *                 以下是通知相关的方法
    *                 
    */

    //获取通知权限
    public void RegisterAPNS()
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        registerAPNS();

#elif UNITY_ANDROID

#endif
    }

    // 注册通知
    public void RegisterNotification(string name, string text, string fireDate)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK

        registerNotification(name,text,fireDate);

#elif UNITY_ANDROID

#endif
    }

    //移除单个通知
    public void RemoveNotification(string notiName)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK

        removeNotification(notiName);

#elif UNITY_ANDROID

#endif
    }

    //移除所有通知
    public void RemoveAllNotification()
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK

        removeAllNotification();

#elif UNITY_ANDROID

#endif
    }

    #region 评价

    #region 应用内弹框

    public void ShowReview()
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK

    showAppStoreReView();
#endif

    }
    #endregion

    #endregion

    #region 用户协议相关
    /**
     * age : 8、12、16、18
     */
    public void ShowUserAgreementView(string contentType, string ageType)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK && IOS_CN
        userAgreementDelegate handler = new userAgreementDelegate(userAgreementAgreeHandle);
        IntPtr userAgree = Marshal.GetFunctionPointerForDelegate(handler);
        //调用OC的方法，将C#的回调方法函数指针传给OC
        userAgreementAgreeCallback(userAgree);
        showUserAgreementView(contentType,ageType);
#endif
    }

    // 用户授权回调
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void userAgreementDelegate();
    [AOT.MonoPInvokeCallback(typeof(userAgreementDelegate))]
    static void userAgreementAgreeHandle()
    {
        Instance.onAgreeHander?.Invoke();
    }

    #endregion

    #region android 恢复游戏相关
    public void Ini()
    {
        InitSuccess();
    }


    public void enterGame()
    {

#if !UNITY_EDITOR
        if (!isInit)
        {
            
            ToukaSDKManager.Instance.initAdsSDK();
            ToukaSDKManager.Instance.initAnalytics();
            isInit = true;
            
        }
#endif

    }
    public void InitSuccess()
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        try
        {
            AndroidJavaClass jc = new AndroidJavaClass("com.toukagames.ToukaActivity");
            jc.CallStatic("initSuccess");

        }
        catch (Exception e)
        {
            Debug.LogException(e);

        }
#endif
    }
    #endregion
}
