//  Created by ZhuCong on 1/1/14.
//  Copyright 2014 Umeng.com . All rights reserved.
#undef UNITY_EDITOR
using System;
using UnityEngine;
using System.Runtime.InteropServices;
namespace Umeng
{
    
    /// 
    /// 友盟游戏统计
    /// 
	public class GA :Analytics
    {
        public enum Gender
        {
            Unknown = 0,
            Male = 1,
            Female = 2
        }
		/// 
		/// 设置玩家等级
		/// 
		/// 玩家等级
		public static void SetUserLevel(int level)
		{
			#if UNITY_EDITOR
			//Debug.Log("SetUserLevel");
			#elif UNITY_IPHONE
			_SetUserLevel(level);
			#elif UNITY_ANDROID
			Agent.CallStatic("setPlayerLevel", level);
			#endif
			
		}
        /// 
        /// 设置玩家属性
        /// 
        /// 玩家Id
        /// 性别
        /// 年龄
        /// 来源
		[System.Obsolete("SetUserInfo已弃用, 请使用ProfileSignIn")]
        public static void SetUserInfo(string userId, Gender gender, int age, string platform)
        {
#if UNITY_EDITOR
            //Debug.Log("SetUserInfo");
#elif UNITY_IPHONE
            _SetUserInfo(userId, (int)gender, age, platform);
#elif UNITY_ANDROID
            Agent.CallStatic("setPlayerInfo",userId, age,  (int)gender, platform);
#endif
        }
        
        /// 
        /// 玩家进入关卡
        /// 
        /// 关卡
        public static void StartLevel(string level)
        {
#if UNITY_EDITOR
            //Debug.Log("StartLevel");
#elif UNITY_IPHONE
            _StartLevel(level);
#elif UNITY_ANDROID
            Agent.CallStatic("startLevel",level);
#endif
        }
        /// 
        /// 玩家通过关卡
        /// 
        /// 如果level设置为null 则为当前关卡
        public static void FinishLevel(string level)
        {
#if UNITY_EDITOR
            //Debug.Log("FinishLevel");
#elif UNITY_IPHONE
            _FinishLevel(level);
#elif UNITY_ANDROID
            Agent.CallStatic("finishLevel",level);
#endif
        }
        /// 
        /// 玩家未通过关卡
        /// 
        /// 如果level设置为null 则为当前关卡
        public static void FailLevel(string level)
        {
#if UNITY_EDITOR
            //Debug.Log("FailLevel");
#elif UNITY_IPHONE
            _FailLevel(level);
#elif UNITY_ANDROID
            Agent.CallStatic("failLevel",level);
#endif
        }
        /// 
        /// Source9 到Source 20 请在友盟后台网站设置 对应的定义
        /// 
        public enum PaySource
        {
            AppStore = 1,
            支付宝 = 2,
            网银 = 3,
            财付通 = 4,
            移动 = 5,
            联通 = 6,
            电信 = 7,
            Paypal = 8,
            Source9,
            Source10,
            Source11,
            Source12,
            Source13,
            Source14,
            Source15,
            Source16,
            Source17,
            Source18,
            Source19,
            Source20,
			Source21,
			Source22,
			Source23,
			Source24,
			Source25,
			Source26,
			Source27,
			Source28,
			Source29,
			Source30
        }
        /// 
        /// 游戏中真实消费(充值)的时候调用此方法
        /// 
        /// 本次消费金额
		/// 来源
        /// 本次消费等值的虚拟币
        public static void Pay(double cash, PaySource source, double coin)
        {
#if UNITY_EDITOR
            //Debug.Log("Pay");
#elif UNITY_IPHONE
            _PayCashForCoin(cash,(int)source,coin);
#elif UNITY_ANDROID
            Agent.CallStatic("pay",cash , coin, (int)source);
#endif
        }
		/// 
		/// 游戏中真实消费(充值)的时候调用此方法
		/// 
		/// 本次消费金额
		/// 来源:AppStore = 1,支付宝 = 2,网银 = 3,财付通 = 4,移动 = 5,联通 = 6,电信 = 7,Paypal = 8,
		/// 9~100对应渠道请到友盟后台设置本次消费的途径,网银,支付宝 等
		/// 本次消费等值的虚拟币
		public static void Pay(double cash, int source, double coin)
		{
				if (source < 1 || source > 100) {
						throw new System.ArgumentException ();
				}
				#if UNITY_EDITOR
				//Debug.Log("Pay");
				#elif UNITY_IPHONE
				_PayCashForCoin(cash,source,coin);
				#elif UNITY_ANDROID
				Agent.CallStatic("pay",cash , coin, source);
				#endif
		}
        /// 
        /// 玩家支付货币购买道具
        /// 
        /// 真实货币数量
        /// 支付渠道
        /// 道具名称
        /// 道具数量
        /// 道具单价
        public static void Pay(double cash, PaySource source, string item, int amount, double price)
        {
#if UNITY_EDITOR
            //Debug.Log("Pay");
#elif UNITY_IPHONE
            _PayCashForItem(cash,(int)source,item,amount,price);
#elif UNITY_ANDROID
            Agent.CallStatic("pay",cash, item, amount, price, (int)source);
#endif
        }
        /// 
        /// 玩家使用虚拟币购买道具
        /// 
        /// 道具名称
        /// 道具数量
        /// 道具单价
        public static void Buy(string item, int amount, double price)
        {
#if UNITY_EDITOR
            //Debug.Log("Buy");
#elif UNITY_IPHONE
            _Buy(item,amount,price);
#elif UNITY_ANDROID
            Agent.CallStatic("buy", item, amount, price);
#endif
        }
        /// 
        /// 玩家使用虚拟币购买道具
        /// 
        /// 道具名称
        /// 道具数量
        /// 道具单价
        public static void Use(string item, int amount, double price)
        {
#if UNITY_EDITOR
            //Debug.Log("Use");
#elif UNITY_IPHONE
            _Use(item, amount, price);
#elif UNITY_ANDROID
            Agent.CallStatic("use", item, amount, price);
#endif
        }
        /// 
        /// Source4 到Source 10 请在友盟后台网站设置 对应的定义
        /// 
        public enum BonusSource
        {
            
            玩家赠送 = 1,
			Source2 =2,
			Source3 =3,
			Source4,
            Source5,
            Source6,
            Source7,
            Source8,
            Source9,
            Source10,
        }
        /// 
        /// 玩家获虚拟币奖励
        /// 
        /// 虚拟币数量
        /// 奖励方式
        public static void Bonus(double coin, BonusSource source)
        {
#if UNITY_EDITOR
            //Debug.Log("Bonus");
#elif UNITY_IPHONE
            _BonusCoin(coin, (int)source);
#elif UNITY_ANDROID
            Agent.CallStatic("bonus", coin, (int)source);
#endif
        }
        /// 
        /// 玩家获道具奖励
        /// 
        /// 道具名称
        /// 道具数量
        /// 道具单价
        /// 奖励方式
        ///         
        public static void Bonus(string item, int amount, double price, BonusSource source)
        {
#if UNITY_EDITOR
            //Debug.Log("Bonus");
#elif UNITY_IPHONE
			_BonusItem(item, amount, price, (int)source);
#elif UNITY_ANDROID
            Agent.CallStatic("bonus", item, amount, price, (int)source);
#endif
        }
		//使用sign-In函数后,如果结束该userId的统计,需要调用ProfileSignOff函数
		public static  void ProfileSignIn(string userId)
		{
			#if UNITY_EDITOR
			//Debug.Log("ProfileSignIn");
			#elif UNITY_IPHONE
			_ProfileSignInWithPUID(userId);
			#elif UNITY_ANDROID
			Agent.CallStatic("onProfileSignIn", userId);
			#endif
		}
		//使用sign-In函数后,如果结束该userId的统计,需要调用ProfileSignOfff函数
 		//provider : 不能以下划线"_"开头,使用大写字母和数字标识; 如果是上市公司,建议使用股票代码。
		public static  void ProfileSignIn(string userId,string provider)
		{
			#if UNITY_EDITOR
			//Debug.Log("ProfileSignIn");
			#elif UNITY_IPHONE
			_ProfileSignInWithPUIDAndProvider(userId,provider);
			#elif UNITY_ANDROID
			Agent.CallStatic("onProfileSignIn", provider,userId);
			#endif
		}
		
		//该结束该userId的统计
		public static  void ProfileSignOff()
		{
			#if UNITY_EDITOR
			//Debug.Log("ProfileSignOff");
			#elif UNITY_IPHONE
			_ProfileSignOff();
			#elif UNITY_ANDROID
			Agent.CallStatic("onProfileSignOff");
			#endif
		
		}
#if	UNITY_IPHONE
        [DllImport("__Internal")]
        private static extern void _SetUserLevel(int level);
        [DllImport("__Internal")]
        private static extern void _SetUserInfo(string userId, int gender, int age, string platform);
        [DllImport("__Internal")]
        private static extern void _StartLevel(string level);
        [DllImport("__Internal")]
        private static extern void _FinishLevel(string level);
        [DllImport("__Internal")]
        private static extern void _FailLevel(string level);
        [DllImport("__Internal")]
        private static extern void _PayCashForCoin(double cash, int source, double coin);
        [DllImport("__Internal")]
        private static extern void _PayCashForItem(double cash, int source, string item, int amount, double price);
        [DllImport("__Internal")]
        private static extern void _Buy(string item, int amount, double price);
        [DllImport("__Internal")]
        private static extern void _Use(string item, int amount, double price);
        [DllImport("__Internal")]
        private static extern void _BonusCoin(double coin, int source);
		[DllImport("__Internal")]
        private static extern void _BonusItem(string item, int amount, double price, int source);
		[DllImport("__Internal")]
		private static extern void _ProfileSignInWithPUID (string puid);
		[DllImport("__Internal")]
		private static extern void _ProfileSignInWithPUIDAndProvider(string puid,string provider);
		[DllImport("__Internal")]
		private static extern void _ProfileSignOff();
#endif
    }
}