using UnityEngine;

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

public class ReyunTracing: MonoBehaviour
{
	private static ReyunTracing _instance = null;

	public static ReyunTracing Instance {
		get {
			if (!_instance) {
				_instance = GameObject.FindObjectOfType (typeof(ReyunTracing)) as ReyunTracing;
				if (!_instance) {
					GameObject am = new GameObject ("ReyunTracing");
					_instance = am.AddComponent (typeof(ReyunTracing)) as ReyunTracing;
				}
			}
			return _instance;           
		}
	}

	void Awake ()
	{
		DontDestroyOnLoad (this);
	}
#if UNITY_IOS
	
	[DllImport ("__Internal")]
	private static extern void _internalInitWithAppKeyAndChannel_TrackingInternational(string appKey, string channelId);

	[DllImport ("__Internal")]
	private static extern void _internalSetRegisterWithAccountID_TrackingInternational(string userName,string userId,string referralFrom);

    [DllImport ("__Internal")]
	private static extern void _internalSetLoginWithAccountID_TrackingInternational(string userName,string userId,string referralFrom);

    [DllImport ("__Internal")]
	private static extern void _internalSetRyzf_TrackingInternational(string name,string userId,string checkoutAsGuest,string contentId,string ryTID,string hbType,float hbAmount);

    [DllImport ("__Internal")]
	private static extern void _internalSetDD_TrackingInternational(string name,string userId,string checkoutAsGuest,string contentId,string ryTID,string hbType,float hbAmount);

    [DllImport ("__Internal")]
	private static extern void _internalSetSubscribe_TrackingInternational(string name,string userId,string checkoutAsGuest,string contentId,string ryTID,string hbType,float hbAmount);

    [DllImport ("__Internal")]
	private static extern void _internalSetSearch_TrackingInternational(string uri,string results);

    [DllImport ("__Internal")]
	private static extern void _internalSetView_TrackingInernational(string name,string user_id,string referral_from,string content_id);

    [DllImport("__Internal")]
    private static extern void _internalSetAddToCart_TrackingInernational(string name,string user_id,string referral_from,string content_id);

    [DllImport("__Internal")]
    private static extern void _internalSetAddToWishlist_TrackingInernational(string name,string user_id,string referral_from,string content_id);

    [DllImport("__Internal")]
    private static extern void _internalSetTutorialComplete_TrackingInernational(string name,string user_id,string duration);

    [DllImport("__Internal")]
    private static extern void _internalSetLevelComplete_TrackingInernational(string name,string user_id,string duration);

    [DllImport("__Internal")]
    private static extern void _internalSetAchievement_TrackingInernational(string name,string user_id,string duration);

    [DllImport("__Internal")]
    private static extern void _internalSetAdClick_TrackingInernational(string ad_type,string ad_network_name,string placement);

    [DllImport("__Internal")]
    private static extern void _internalSetAdView_TrackingInernational(string ad_type,string ad_network_name,string placement);
   
   [DllImport("__Internal")]
    private static extern void _internalSetRating_TrackingInernational(float rating_value, float max_rating_value);

    [DllImport ("__Internal")]
    private static extern void _internalSetEvent_TrackingInternational(string name,string param);

    [DllImport ("__Internal")]
	private static extern void _internalSetPrintLog_TrackingInternational(bool b);
#endif

#if UNITY_ANDROID
    public static AndroidJavaObject getApplicationContext ()
	{
		
		using (AndroidJavaClass jc = new AndroidJavaClass ("com.unity3d.player.UnityPlayer")) {
			using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject> ("currentActivity")) {
				return jo.Call<AndroidJavaObject> ("getApplicationContext");
			}
		}
		
		return null;
	}
#endif

    /// <summary>
    /// 初始化方法   
    /// </summary>
    /// <param name="appKey">appKey</param>
    /// <param name="channelId">标识推广渠道的字符</param>
    public void Init(string appKey, string channelId)
	{
    #if UNITY_IOS
        _internalInitWithAppKeyAndChannel_TrackingInternational(appKey, channelId);
#endif

#if UNITY_ANDROID
		using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
			TrackingIO.CallStatic ("initWithKeyAndChannelId", getApplicationContext (), appKey, channelId);
		}
#endif

    }

    /// <summary>
    /// 玩家服务器注册
    /// </summary>
    /// <param name="userName">账号名称</param>
    /// <param name="userId">账号id</param>
    /// <param name="referralFrom">referralFrom</param>
    /// 
    public void Register(string userName, string userId, string referralFrom)
    {
#if UNITY_IOS
        _internalSetRegisterWithAccountID_TrackingInternational(userName,userId,referralFrom);
#endif

#if UNITY_ANDROID
		using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
			TrackingIO.CallStatic ("setRegisterWithAccountID", userId,userName,referralFrom);
		}
#endif

    }

    /// <summary>
    /// 玩家的账号登陆服务器
    /// </summary>
    /// <param name="userName">账号名称</param>
    /// <param name="userId">账号id</param>
    /// <param name="referralFrom">referralFrom</param>
    /// 

    public void Login(string userName, string userId, string referralFrom)
    {
#if UNITY_IOS
        _internalSetLoginWithAccountID_TrackingInternational(userName, userId, referralFrom);
#endif

#if UNITY_ANDROID
		using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
			TrackingIO.CallStatic ("setLoginSuccessBusiness", userId,userName,referralFrom);
		}
#endif
    }

    /// <summary>
    /// 玩家订阅数据
    /// </summary>
    public void SetSubscribe(string name, string userId, string checkoutAsGuest, string contentId, 
    string ryTID, string hbType, float hbAmount)
	{
#if UNITY_IOS
		_internalSetSubscribe_TrackingInternational (name, userId, checkoutAsGuest, contentId, ryTID, hbType, hbAmount);
#endif
#if UNITY_ANDROID
        //public static void setSubscribe(String name, String userId, String checkoutAsGuest, 
        //String contentId, String transactionId, String currencyType, float currencyAmount)
        using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
		TrackingIO.CallStatic ("setSubscribe", name, userId, checkoutAsGuest, contentId, ryTID, hbType, hbAmount);
		}
		#endif

	}

	/// <summary>
	/// 玩家的充值数据
	/// </summary>
	public void Setryzf(string name, string userId, string checkoutAsGuest, string contentId,
    string ryTID, string hbType, float hbAmount)
    {
#if UNITY_IOS
        _internalSetRyzf_TrackingInternational(name, userId, checkoutAsGuest, contentId, ryTID, hbType, hbAmount);
#endif

#if UNITY_ANDROID
        //public static void setPayment(String name, String userId, String checkoutAsGuest,
         //String contentId, String transactionId, String currencyType, float currencyAmount)

        using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
		TrackingIO.CallStatic ("setPayment", name, userId, checkoutAsGuest, contentId, ryTID, hbType, hbAmount);
		}
#endif

    }

	public void SetDD(string name, string userId, string checkoutAsGuest, string contentId,
    string ryTID, string hbType, float hbAmount)
    {
#if UNITY_IOS
        _internalSetDD_TrackingInternational(name, userId, checkoutAsGuest, contentId, ryTID, hbType, hbAmount);
#endif

#if UNITY_ANDROID
        //public static void setOrder(String name, String userId, String checkoutAsGuest, 
        //String contentId, String transactionId, String currencyType, float currencyAmount)

        using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
		TrackingIO.CallStatic ("setOrder", name, userId, checkoutAsGuest, contentId, ryTID, hbType, hbAmount);
        }
#endif
    }
	
	public void SetSearch(string uri, string results)
	{
#if UNITY_IOS
        _internalSetSearch_TrackingInternational(uri,results);
#endif

#if UNITY_ANDROID
        //public static void setSearch(String uri, String results)
        using (AndroidJavaClass reyun = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
			reyun.CallStatic ("setSearch", uri, results);
		}
		#endif
	}

    public void SetView(string name, string user_id, string referral_from, string content_id) {
#if UNITY_IOS
        _internalSetView_TrackingInernational(name, user_id, referral_from, content_id);
#endif

#if UNITY_ANDROID
        //public static void setView(String name, String userId, String referralFrom, String contentId)
        using (AndroidJavaClass reyun = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
            reyun.CallStatic ("setView", name, user_id, referral_from, content_id);
        }
#endif
    }

    public void SetAddToCart(string name, string user_id, string referral_from, string content_id)
    {
#if UNITY_IOS
        _internalSetAddToCart_TrackingInernational(name, user_id, referral_from, content_id);
#endif

#if UNITY_ANDROID
        //public static void setAddToCart(String name, String userId, String referralFrom, String contentId)
        using (AndroidJavaClass reyun = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
            reyun.CallStatic ("setAddToCart", name, user_id, referral_from, content_id);
        }
#endif
    }

    public void SetAddToWishlist(string name, string user_id, string referral_from, string content_id)
    {
#if UNITY_IOS
        _internalSetAddToWishlist_TrackingInernational(name, user_id, referral_from, content_id);
#endif

#if UNITY_ANDROID
        //public static void setAddToWishList(String name, String userId, String referralFrom, String contentId)
        using (AndroidJavaClass reyun = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
            reyun.CallStatic ("setAddToWishList", name, user_id, referral_from, content_id);
        }
#endif
    }

    public void SetTutorialComplete(string name, string user_id, string duration)
    {
#if UNITY_IOS
        _internalSetTutorialComplete_TrackingInernational(name,user_id,duration);
#endif

#if UNITY_ANDROID
        //public static void setTutorialcomplete(String name, String userId, String duration)
        using (AndroidJavaClass reyun = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
            reyun.CallStatic ("setTutorialcomplete", name, user_id,duration);
        }
#endif
    }

    public void SetLevelComplete(string name, string user_id, string duration)
    {
#if UNITY_IOS
        _internalSetLevelComplete_TrackingInernational(name, user_id, duration);
#endif

#if UNITY_ANDROID
        //public static void setLevelcomplete(String name, String userId, String duration)
        using (AndroidJavaClass reyun = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
            reyun.CallStatic ("setLevelcomplete", name, user_id, duration);
        }
#endif
    }

    public void SetAchievement(string name, string user_id, string duration)
    {
#if UNITY_IOS
        _internalSetAchievement_TrackingInernational(name, user_id, duration);
#endif

#if UNITY_ANDROID
        //public static void setAchievement(String name, String userId, String duration)
        using (AndroidJavaClass reyun = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
            reyun.CallStatic ("setAchievement", name, user_id, duration);
        }
#endif
    }

    public void SetAdClick(string ad_type, string ad_network_name, string placement)
    {
#if UNITY_IOS
        _internalSetAdClick_TrackingInernational(ad_type,ad_network_name,placement);
#endif

#if UNITY_ANDROID
        //public static void setAdClick(String adType, String adNetworkName, String placement)
        using (AndroidJavaClass reyun = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
            reyun.CallStatic ("setAdClick", ad_type, ad_network_name, placement);
        }
#endif
    }

    public void SetAdView(string ad_type, string ad_network_name, string placement)
    {
#if UNITY_IOS
        _internalSetAdView_TrackingInernational(ad_type, ad_network_name, placement);
#endif

#if UNITY_ANDROID
        //public static void setAdView(String adType, String adNetworkName, String placement)
        using (AndroidJavaClass reyun = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
            reyun.CallStatic ("setAdView", ad_type, ad_network_name, placement);
        }
#endif
    }

    public void SetRating(float rating_value, float max_rating_value)
    {
#if UNITY_IOS
        _internalSetRating_TrackingInernational(rating_value, max_rating_value);
#endif

#if UNITY_ANDROID
        //public static void setRating(String ratingValue, String maxRatingValue)
        using (AndroidJavaClass reyun = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
            reyun.CallStatic ("setRating", ""+ rating_value, ""+ max_rating_value);
        }
#endif
    }

    public void SetEvent(string eventName,Dictionary<string, string> param)
    {
        if(eventName == null || eventName.Length == 0) {
            return;
        }
        string jsonString = "";
        if (param == null || param.Count == 0)
        {
            jsonString = "{}";
        }
        else
        {
            int nLength = param.Count;
            List<string> dicKey = new List<string>(param.Keys);
            List<string> dicValue = new List<string>(param.Values);
            string json = "{";
            for (int i = 0; i < nLength; i++)
            {
                string subKeyValue = "\"" + dicKey[i] + "\"" + ":" + "\"" + dicValue[i] + "\"";
                json += subKeyValue;
                if (i != nLength - 1)
                {
                    json += ",";
                }
            }
            json += "}";
            jsonString = json;
        }
#if UNITY_IOS
        _internalSetEvent_TrackingInternational(eventName,jsonString);
#endif

#if UNITY_ANDROID
        //public static void setCustomEventJsonString(String eventName, String jsonString)
        using (AndroidJavaClass reyun = new AndroidJavaClass("com.reyun.inter.tracking.sdk.Tracking"))
        {
            reyun.CallStatic("setCustomEventJsonString", eventName,jsonString);
        }
#endif
    }

    /// 开启日志打印
    public void SetPrintLog (bool print)
	{
#if UNITY_IOS
        _internalSetPrintLog_TrackingInternational(print);
#endif
#if UNITY_ANDROID
        //public static void setDebugMode(boolean mode)
      using (AndroidJavaClass TrackingIO = new AndroidJavaClass ("com.reyun.inter.tracking.sdk.Tracking")) {
	   TrackingIO.CallStatic("setDebugMode", print);
	  }
#endif
	}
}

