/*
 *  Copyright (c) 2016 Magnet Systems, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.magnet.samples.android.quickstart.activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import com.magnet.max.android.config.MaxAndroidConfig;
import com.magnet.max.android.config.MaxAndroidPropertiesConfig;
import com.magnet.max.android.util.StringUtil;
import com.magnet.samples.android.quickstart.R;
import com.magnet.samples.android.quickstart.helpers.Utils;

/**
 * For complete feature list, @see <a href="https://developer.magnet.com/docs/message/overview/features/index.html">Features</a>
 */
public class FeaturesActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_features);
        findViewById(R.id.featureChatBtn).setOnClickListener(this);
        findViewById(R.id.featurePublishBtn).setOnClickListener(this);
        findViewById(R.id.featureManagementBtn).setOnClickListener(this);
        findViewById(R.id.featurePushBtn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.featureChatBtn:
                startActivity(new Intent(this, ChatActivity.class));
                break;
            case R.id.featurePublishBtn:
                startActivity(new Intent(this, PubSubActivity.class));
                break;
            case R.id.featureManagementBtn:
                startActivity(new Intent(this, UserManagementActivity.class));
                break;
            case R.id.featurePushBtn:
                if(isGCMConfigured()) {
                    startActivity(new Intent(this, PushActivity.class));
                }
                break;
            default:
                break;
        }
    }

    private boolean isGCMConfigured() {
        boolean isSenderIdValid = false;

        if(Build.FINGERPRINT.contains("generic")) {
            Utils.showWarning(this, "Push is not configured",  "Push can't be test on emulator, please use a device");
        } else {
            MaxAndroidConfig config = new MaxAndroidPropertiesConfig(this, R.raw.magnetmax);
            String senderId = config.getAllConfigs().get("mmx-gcmSenderId");
            if (StringUtil.isNotEmpty(senderId)) {
                try {
                    long sendIdLong = Long.parseLong(senderId);
                    isSenderIdValid = true;
                } catch (NumberFormatException e) {
                }
                if (!isSenderIdValid) {
                    Utils.showWarning(this, "Push is not configured",  "Please follow the instruction in https://developer.magnet.com/docs/message/v2.1/android/set-up-gcm/index.html and set mmx-gcmSenderId in res/raw/magnetmax.properties");
                }
            }
        }

        return isSenderIdValid;
    }

}

@@ -854,16 +854,132 @@
 w.View;%0A
+import com.google.android.gms.common.ConnectionResult;%0Aimport com.google.android.gms.common.GooglePlayServicesUtil;%0A
 import c
@@ -1017,32 +1017,32 @@
 xAndroidConfig;%0A
-
 import com.magne
@@ -1133,24 +1133,65 @@
 StringUtil;%0A
+import com.magnet.mmx.client.common.Log;%0A
 import com.m
@@ -2771,44 +2771,38 @@
  if(
-Build.FINGERPRINT.contains(%22gen
+!isGooglePlayS
 er
+v
 ic
-%22
+eInstalled(
 )) %7B
@@ -2831,36 +2831,51 @@
 wWarning(this, %22
-Push
+Google Play Service
  is not configur
@@ -2886,58 +2886,68 @@
   %22P
-ush can't be test on emulator, please use a device
+lease install Google Play Service on your device or emulator
 %22);%0A
@@ -3704,24 +3704,24 @@
 %0A        %7D%0A%0A
-
         retu
@@ -3747,10 +3747,511 @@
 %0A    %7D%0A%0A
+    private boolean isGooglePlayServiceInstalled() %7B%0A        //try %7B%0A        //    Class.forName(%22com.google.android.gms.gcm.GoogleCloudMessaging%22);%0A        //    return true;%0A        //%7D catch (ClassNotFoundException e) %7B%0A        //%0A        //%7D%0A        //%0A        //return false;%0A        final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());%0A        if (status == ConnectionResult.SUCCESS) %7B%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A
 %7D%0A

allow test push on simulator