package com.tapglue.android.sims;

import android.content.Context;

import com.tapglue.android.Configuration;
import com.tapglue.android.entities.User;
import com.tapglue.android.internal.NotificationServiceIdStore;
import com.tapglue.android.internal.SessionStore;
import com.tapglue.android.internal.UUIDStore;

import java.util.Locale;

import rx.Observable;
import rx.Observer;
import rx.functions.Func3;
import rx.schedulers.Schedulers;

public class TapglueSims implements NotificationServiceIdListener {

    NotificationServiceIdStore notificationIdStore;
    SessionStore sessionStore;
    UUIDStore uuidStore;
    final Configuration configuration;
    final Locale locale;

    public TapglueSims(Configuration configuration, Context context) {
        this.configuration = configuration;
        notificationIdStore = new NotificationServiceIdStore(context);
        sessionStore = new SessionStore(context);
        uuidStore = new UUIDStore(context);
        locale = context.getResources().getConfiguration().locale;
        SimsIdListenerService.setListener(this);
    }

    @Override
    public void idChanged(String id) {
        notificationIdStore.store(id);
        registerDeviceForSims();
    }

    public void sessionTokenChanged() {
       registerDeviceForSims();
    }

    private void registerDeviceForSims() {
        Observable.combineLatest(notificationIdStore.get(), sessionStore.get(), uuidStore.get(), new Func3<String, User, String, Void>() {
            @Override
            public Void call(String notificationId, User session, String uuid) {
                SimsServiceFactory serviceFactory = new SimsServiceFactory(configuration);
                serviceFactory.setSessionToken(session.getSessionToken());
                serviceFactory.setUserUUID(uuid);
                SimsService service = serviceFactory.createService();
                DevicePayload payload = new DevicePayload();
                payload.token = notificationId;
                payload.language = locale.toString();
                service.registerDevice(uuid, payload).subscribe();
                return null;
            }
        }).subscribeOn(Schedulers.io()).subscribe(new Observer<Void>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Void aVoid) {

            }
        });
    }
}

@@ -331,16 +331,66 @@
 .Locale;
+%0Aimport java.util.concurrent.atomic.AtomicBoolean;
 %0A%0Aimport
@@ -551,24 +551,99 @@
 Listener %7B%0A%0A
+    private static AtomicBoolean isRegistered = new AtomicBoolean(false);%0A%0A
     Notifica
@@ -1456,24 +1456,87 @@
 ForSims() %7B%0A
+        if(isRegistered.get()) %7B%0A            return;%0A        %7D%0A
         Obse
@@ -2623,24 +2623,63 @@
 id aVoid) %7B%0A
+                isRegistered.set(true);
 %0A           

Fix device registering on every instance