package oasis.jongo.applications.v2;

import javax.inject.Inject;

import org.jongo.Jongo;
import org.jongo.MongoCollection;
import org.jongo.MongoCursor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.mongodb.DuplicateKeyException;

import oasis.jongo.JongoBootstrapper;
import oasis.model.InvalidVersionException;
import oasis.model.applications.v2.UserSubscription;
import oasis.model.applications.v2.UserSubscriptionRepository;

public class JongoUserSubscriptionRepository implements UserSubscriptionRepository, JongoBootstrapper {
  private static final Logger logger = LoggerFactory.getLogger(JongoUserSubscriptionRepository.class);

  private final Jongo jongo;

  @Inject JongoUserSubscriptionRepository(Jongo jongo) {
    this.jongo = jongo;
  }

  private MongoCollection getUserSubscriptionsCollection() {
    return jongo.getCollection("user_subscriptions");
  }

  @Override
  public UserSubscription createUserSubscription(UserSubscription subscription) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(subscription.getService_id()));

    subscription = new JongoUserSubscription(subscription);
    try {
      getUserSubscriptionsCollection()
          .insert(subscription);
    } catch (DuplicateKeyException dke) {
      return null;
    }
    return subscription;
  }

  @Override
  public UserSubscription getUserSubscription(String id) {
    return getUserSubscriptionsCollection()
        .findOne("{ id: # }", id)
        .as(JongoUserSubscription.class);
  }

  @Override
  public boolean deleteUserSubscription(String id, long[] versions) throws InvalidVersionException {
    int n = getUserSubscriptionsCollection()
        .remove("{ id: #, modified: { $in: # } }", id, versions)
        .getN();

    if (n == 0) {
      if (getUserSubscriptionsCollection().count("{ id: # }", id) != 0) {
        throw new InvalidVersionException("userSubscription", id);
      }
      return false;
    }

    if (n > 1) {
      logger.error("Deleted {} user subscriptions with ID {}, that shouldn't have happened", n, id);
    }
    return true;
  }

  @Override
  @SuppressWarnings("unchecked")
  public Iterable<UserSubscription> getUserSubscriptions(String userId) {
    return (Iterable<UserSubscription>) (Iterable<?>) getUserSubscriptionsCollection()
        .find("{ user_id: # }", userId)
        .as(JongoUserSubscription.class);
  }

  @Override
  @SuppressWarnings("unchecked")
  public Iterable<UserSubscription> getSubscriptionsForService(String serviceId) {
    return (Iterable<UserSubscription>) (Iterable<?>) getUserSubscriptionsCollection()
        .find("{ service_id: 1 }")
        .as(JongoUserSubscription.class);
  }

  @Override
  public void bootstrap() {
    getUserSubscriptionsCollection().ensureIndex("{ id: 1 }", "{ unique: 1 }");
    getUserSubscriptionsCollection().ensureIndex("{ service_id: 1, user_id: 1 }", "{ unique: 1 }");
    getUserSubscriptionsCollection().ensureIndex("{ user_id: 1 }");
    getUserSubscriptionsCollection().ensureIndex("{ service_id: 1 }");
  }
}

@@ -2711,20 +2711,31 @@
 ice_id: 
-1
+#
  %7D%22
+, serviceId
 )%0A      

Fix empty response of Service subscription getter