import Ember from 'ember';
import Adapter from 'ember-data/adapter';

export default Adapter.extend({
    _hzService: Ember.inject.service('horizon-connection'),
    _getConnectionPromise() {
        return this.get('_hzService').connect();
    },

    defaultSerializer: 'json',

    findRecord(store, type, id) {
        const connectionPromise = this._getConnectionPromise();

        return connectionPromise.then((hz) => {
            return new Ember.RSVP.Promise((resolve, reject) => {
                hz(type.modelName)
                    .find(id)
                    .fetch()
                    .subscribe(resolve, reject);
            });
        });
    },

    findAll(store, type) {
        const connectionPromise = this._getConnectionPromise();

        return connectionPromise.then((hz) => {
            return new Ember.RSVP.Promise((resolve, reject) => {
                hz(type.modelName)
                    .fetch()
                    .subscribe(resolve, reject);
            });
        });
    },

    findMany(store, type, ids) {
        const connectionPromise = this._getConnectionPromise();

        return connectionPromise.then((hz) => {
            return new Ember.RSVP.Promise((resolve, reject) => {
                hz(type.modelName)
                    .findAll(ids)
                    .fetch()
                    .subscribe(resolve, reject);
            });
        });
    },

    query(store, type, query) {
        const connectionPromise = this._getConnectionPromise();

        return connectionPromise.then((hz) => {
            return new Ember.RSVP.Promise((resolve, reject) => {
                hz(type.modelName)
                    .findAll(query)
                    .fetch()
                    .subscribe(resolve, reject);
            });
        });
    },

    queryRecord(store, type, query) {
        const connectionPromise = this._getConnectionPromise();

        return connectionPromise.then((hz) => {
            return new Ember.RSVP.Promise((resolve, reject) => {
                hz(type.modelName)
                    .find(query)
                    .fetch()
                    .subscribe(resolve, reject);
            });
        });
    },

    createRecord(store, type, snapshot) {
        const connectionPromise = this._getConnectionPromise();

        return connectionPromise.then((hz) => {
            const payload = this.serialize(snapshot);
            return new Ember.RSVP.Promise((resolve, reject) => {
                hz(type.modelName)
                    .store(payload)
                    .subscribe(resolve, reject);
            });
        });
    },

    updateRecord(store, type, snapshot) {
        const connectionPromise = this._getConnectionPromise();

        return connectionPromise.then((hz) => {
            const payload = this.serialize(snapshot, { includeId: true });
            return new Ember.RSVP.Promise((resolve, reject) => {
                hz(type.modelName)
                    .replace(payload)
                    .subscribe(resolve, reject);
            });
        });
    },

    deleteRecord(store, type, snapshot) {
        const connectionPromise = this._getConnectionPromise();

        return connectionPromise.then((hz) => {
            return new Ember.RSVP.Promise((resolve, reject) => {
                hz(type.modelName)
                    .remove(snapshot.id)
                    .subscribe(resolve, reject);
            });
        });
    },
});

@@ -603,39 +603,82 @@
      .subscribe(
-resolve
+Ember.run.bind(this, resolve), Ember.run.bind(this
 , reject);%0A     
@@ -662,32 +662,33 @@
 nd(this, reject)
+)
 ;%0A            %7D)
@@ -1001,39 +1001,82 @@
      .subscribe(
-resolve
+Ember.run.bind(this, resolve), Ember.run.bind(this
 , reject);%0A     
@@ -1060,32 +1060,33 @@
 nd(this, reject)
+)
 ;%0A            %7D)
@@ -1439,39 +1439,82 @@
      .subscribe(
-resolve
+Ember.run.bind(this, resolve), Ember.run.bind(this
 , reject);%0A     
@@ -1498,32 +1498,33 @@
 nd(this, reject)
+)
 ;%0A            %7D)
@@ -1878,39 +1878,82 @@
      .subscribe(
-resolve
+Ember.run.bind(this, resolve), Ember.run.bind(this
 , reject);%0A     
@@ -1937,32 +1937,33 @@
 nd(this, reject)
+)
 ;%0A            %7D)
@@ -2320,39 +2320,82 @@
      .subscribe(
-resolve
+Ember.run.bind(this, resolve), Ember.run.bind(this
 , reject);%0A     
@@ -2379,32 +2379,33 @@
 nd(this, reject)
+)
 ;%0A            %7D)
@@ -2794,39 +2794,82 @@
      .subscribe(
-resolve
+Ember.run.bind(this, resolve), Ember.run.bind(this
 , reject);%0A     
@@ -2853,32 +2853,33 @@
 nd(this, reject)
+)
 ;%0A            %7D)
@@ -3291,39 +3291,82 @@
      .subscribe(
-resolve
+Ember.run.bind(this, resolve), Ember.run.bind(this
 , reject);%0A     
@@ -3350,32 +3350,33 @@
 nd(this, reject)
+)
 ;%0A            %7D)
@@ -3724,23 +3724,66 @@
 bscribe(
-resolve
+Ember.run.bind(this, resolve), Ember.run.bind(this
 , reject
@@ -3783,16 +3783,17 @@
  reject)
+)
 ;%0A      

add callbacks to the run loop.