// Use this for initialization
	void Awake() {
		DontDestroyOnLoad(this);
		#if UNITY_IPHONE
		plugin = GameObject.Find("KiiPush").GetComponent<KiiPushPlugin>();
		Debug.Log("Plugin: "+plugin.ToString());
		plugin.OnPushMessageReceived += (ReceivedMessage message) => {
			// This event handler is called when received the push message.
			switch (message.PushMessageType) 
			{
			case ReceivedMessage.MessageType.PUSH_TO_APP:
				// Get the "push_to_app" specific fields.
				PushToAppMessage appMsg = (PushToAppMessage)message;
				Debug.Log("Bucket=" + appMsg.KiiBucket.Uri.ToString());
				Debug.Log("Object=" + appMsg.KiiObject.Uri.ToString());
				break;
			case ReceivedMessage.MessageType.PUSH_TO_USER:
				// "Push to User" message handling...
				Debug.Log("Push to User="******"Direct push" message handling...
				Debug.Log("Direct Push=" + message.ToString());
				break;
			}
			// Dammy Message
 			notif_message = "New HighScore";
		};
		#endif
	}
    // Use this for initialization
    void Start()
    {
        Debug.Log ("#####Main.Start");
        this.kiiPushPlugin = GameObject.Find ("KiiPushPlugin").GetComponent<KiiPushPlugin> ();

        this.receivedCallback = (ReceivedMessage message) => {
            switch (message.PushMessageType)
            {
            case ReceivedMessage.MessageType.PUSH_TO_APP:
                Debug.Log ("#####PUSH_TO_APP Message");
                this.OnPushNotificationsReceived (message);
                break;
            case ReceivedMessage.MessageType.PUSH_TO_USER:
                Debug.Log ("#####PUSH_TO_USER Message");
                this.OnPushNotificationsReceived (message);
                break;
            case ReceivedMessage.MessageType.DIRECT_PUSH:
                Debug.Log ("#####DIRECT_PUSH Message");
                this.OnPushNotificationsReceived (message);
                break;
            }
        };
        this.kiiPushPlugin.OnPushMessageReceived += this.receivedCallback;

        pushSetting = new PushSetting ();
        apnsSetting = new APNSSetting ();
        gcmSetting = new GCMSetting ();

        if (KiiUser.CurrentUser != null)
        {
            Invoke ("registerPush", 0);
            return;
        }

        KiiUser.LogIn (USER_NAME, PASSWORD, (KiiUser u1, Exception e1) => {
            if (e1 != null)
            {
                KiiUser newUser = KiiUser.BuilderWithName (USER_NAME).Build ();
                Debug.Log ("#####Register");
                newUser.Register (PASSWORD, (KiiUser u2, Exception e2) => {
                    Debug.Log ("#####callback Register");
                    if (e2 != null)
                    {
                        Debug.Log ("#####failed to Register");
                        this.ShowException ("Failed to register user.", e2);
                        return;
                    }
                    else
                    {
                        Invoke ("registerPush", 0);
                    }
                });
            }
            else
            {
                Invoke ("registerPush", 0);
            }
        });
    }
	private void register () {
		user = null;
		OnCallback = true;
		KiiUser built_user = KiiUser.BuilderWithName(username).Build();
		built_user.Register(password, (KiiUser user2, Exception e) => {
			if (e == null)
			{
				#if UNITY_IPHONE
				bool development = true;      // choose development/production for iOS
				string USER = username;
				string PASS = password;
				KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.IOS;
				plugin = GameObject.Find("KiiPush").GetComponent<KiiPushPlugin>();
				plugin.RegisterPush((string pushToken, Exception e0) => {
					Debug.Log("Token :"+pushToken);
					if( e0 == null ){
						KiiUser.LogIn(USER, PASS, (KiiUser kiiuser, Exception e1) => {
							if( e1 == null ){
								KiiUser.PushInstallation(development).Install(pushToken, deviceType, (Exception e2) => {
									Debug.Log ("Push registration completed");
									KiiUser user3 = KiiUser.CurrentUser;
									KiiBucket bucket = Kii.Bucket("FlappyDogHighScore");
									KiiPushSubscription ps = user3.PushSubscription;
		 							ps.Subscribe(bucket, (KiiSubscribable target, Exception e3) => {
										if (e3 != null)
										{
											// check fail cause
											Debug.Log("Subscription Failed");
											Debug.Log(e3.ToString());
										}
									}); 
								});
							}else{
								Debug.Log("e1 error: "+e1.ToString());
							}; 
						});
					}else{
						Debug.Log("e0 error: "+e0.ToString());
					};
				});
				#endif

				user = user2;
				Debug.Log ("Register completed");
			} else {
				user = null;
				OnCallback = false;
				Debug.Log ("Register failed : " + e.ToString());
			}
		});
	}