package com.example.android.sunshine.app.gcm;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.example.android.sunshine.app.R;
import com.example.android.sunshine.app.Utility;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;

/**
 * Created by zwh on 7/18/16.
 */
public class RegistrationIntentService extends IntentService {
    private static final String TAG = "GCM";

    public RegistrationIntentService() {
        super(TAG);
    }

    public static Intent newIntent(Context context){
        return new Intent(context, RegistrationIntentService.class);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //Log.i(TAG, "RegistrationIntentService - onHandleIntent");
        try {
            // In the (unlikely) event that multiple refresh operations occur simultaneously,
            // ensure that they are processed sequentially.
            synchronized (TAG) {
                // Initially this call goes out to the network to retrieve the token,
                // subsequent calls are local.
                InstanceID instanceID = InstanceID.getInstance(this);

                //To perform the registration, we use the InstanceID class to call getToken.
                //This class needs a SenderID which is pulled from our google-services.json
                //config file. Then it also needs a scope for the permissions that will be
                //granted by the token and any parameters required by that scope. In our case,
                //there are none.
                String senderId = getString(R.string.gcm_defaultSenderId);
                //senderId = "project_number": "102191122573"

                if ( senderId.length() != 0 ) {
                    String token = instanceID.getToken(senderId,
                            GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                    sendRegistrationToServer(token);
                }

                // You should store a boolean that indicates whether the generated token has been
                // sent to your server. If the boolean is false, send the token to your server,
                // otherwise your server should have already received the token.
                Utility.setSentTokenToServer(this, true);
            }
        } catch (Exception e) {
            //Log.d(TAG, "Failed to complete token refresh", e);

            // If an exception happens while fetching the new token or updating our registration data
            // on a third-party server, this ensures that we'll attempt the update at a later time.
            Utility.setSentTokenToServer(this, false);
        }
    }

    /**
     * Normally, you would want to persist the registration to third-party servers. Because we do
     * not have a server, and are faking it with a website, you'll want to log the token instead.
     * That way you can see the value in logcat, and note it for future use in the website.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        Log.i(TAG, "GCM Registration Token: " + token);
    }
}
