from google.appengine.ext import ndb

from helpers.notification_helper import NotificationHelper
from models.account import Account
from models.favorite import Favorite
from models.subscription import Subscription


class MyTBAHelper(object):
    @classmethod
    def add_favorite(cls, fav, device_key=""):
        if Favorite.query(Favorite.model_key == fav.model_key, ancestor=ndb.Key(Account, fav.user_id)).count() == 0:
            # Favorite doesn't exist, add it
            fav.put()
            if device_key:
                # Send updates to user's other devices
                NotificationHelper.send_favorite_update(fav.user_id, device_key)
            return 200
        else:
            # Favorite already exists. Don't add it again
            return 304

    @classmethod
    def remove_favorite(cls, userId, modelKey, device_key=""):
        to_delete = Favorite.query(Favorite.model_key == modelKey, ancestor=ndb.Key(Account, userId)).fetch(keys_only=True)
        if len(to_delete) > 0:
            ndb.delete_multi(to_delete)
            if device_key:
                # Send updates to user's other devices
                NotificationHelper.send_favorite_update(userId, device_key)
            return 200
        else:
            # Favorite doesn't exist. Can't delete it
            return 404

    @classmethod
    def add_subscription(cls, sub, device_key=""):
        current = Subscription.query(Subscription.model_key == sub.model_key, ancestor=ndb.Key(Account, sub.user_id)).get()
        if current is None:
            # Subscription doesn't exist, add it
            sub.put()
            if device_key:
                # Send updates to user's other devices
                NotificationHelper.send_subscription_update(sub.user_id, device_key)
            return 200
        else:
            if current.notification_types == sub.notification_types:
                # Subscription already exists. Don't add it again
                return 304
            else:
                # We're updating the settings
                current.notification_types = sub.notification_types
                current.put()
                if device_key:
                    # Send updates to user's other devices
                    NotificationHelper.send_subscription_update(sub.user_id, device_key)
                return 200

    @classmethod
    def remove_subscription(cls, userId, modelKey, device_key=""):
        to_delete = Subscription.query(Subscription.model_key == modelKey, ancestor=ndb.Key(Account, userId)).fetch(keys_only=True)
        if len(to_delete) > 0:
            ndb.delete_multi(to_delete)
            if device_key:
                # Send updates to user's other devices
                NotificationHelper.send_subscription_update(userId, device_key)
            return 200
        else:
            # Subscription doesn't exist. Can't delete it
            return 404



@@ -488,39 +488,8 @@
 t()%0A
-            if device_key:%0A    
     
@@ -527,36 +527,32 @@
 s other devices%0A
-    
             Noti
@@ -1010,39 +1010,8 @@
 te)%0A
-            if device_key:%0A    
     
@@ -1049,36 +1049,32 @@
 s other devices%0A
-    
             Noti
@@ -1539,39 +1539,8 @@
 t()%0A
-            if device_key:%0A    
     
@@ -1578,36 +1578,32 @@
 s other devices%0A
-    
             Noti
@@ -2032,43 +2032,8 @@
 t()%0A
-                if device_key:%0A    
     
@@ -2075,36 +2075,32 @@
 s other devices%0A
-    
                 
@@ -2487,39 +2487,8 @@
 te)%0A
-            if device_key:%0A    
     
@@ -2526,36 +2526,32 @@
 s other devices%0A
-    
             Noti
@@ -2697,32 +2697,32 @@
 Can't delete it%0A
+
             retu
@@ -2728,10 +2728,8 @@
 urn 404%0A
-%0A%0A

Remove unnecessary device_key check