import Ember from 'ember';
import onlineJob from 'ember-data-offline/jobs/online';
// import DS from 'ember-data';
const { Mixin, $, on, assert, computed, get, isPresent } = Ember;

export default Mixin.create({
  isOffline: computed.not('isOnline'),

  store: computed({
    get() {
      return this.container.lookup('store:main');
    }
  }),
  _workingQueue: computed('queue', {
    get() {
      if (isPresent(get(this, 'queue'))) {
        return get(this, 'queue');
      } else {
        return get(this, 'store.queue');
      }
    }
  }),

  populatedLog: Ember.Object.create({}),
  persistData(typeClass, records){
    this.get('offlineAdapter').persistData(typeClass, records);
  },

  createOnlineJob(method, params){
    onlineJob.create({
      adapter: this,
      method: method,
      params: params
    });
  },

  createOfflineJob(){
    //for create, update...or maybe this we don't need
  },

  /*
   * `find()`
   * `createRecord()`
   * `updateRecord()`
   * `deleteRecord()`
   * `findAll()` -- done some
   * `findQuery()`
   */

  findAll: function(store, typeClass, sinceToken) {
    let isPopulated = this.get(`populatedLog.${typeClass}`);
    if (this.get('isOffline')) {
      this.createOnlineJob('findAll', [store, typeClass, sinceToken]);
      return this.get('offlineAdapter').findAll(store, typeClass, sinceToken);
    }

    let adapterResp = this._super.apply(this, arguments);

    if (isPopulated){
      return adapterResp;
    }
    adapterResp.then(records => {
      this.persistData(typeClass, records);
      this.set(`populatedLog.${typeClass}`, true);
    });
    return adapterResp;
  },

  find: function(store, type, id, snapshot) {
    if (this.get('isOnline')) {
      let onlineResp = this._super.apply(this, arguments);
      let adapter = this;
      //check offline storage
      Ember.RSVP.resolve().then(() => {
        return this.get('offlineAdapter').find(store, type, id, snapshot);
      }).then(offineRecord => {
        if (Ember.isEmpty(offineRecord)) {
          return adapterResp;
        }
      }).then(onlineRecord => {
        if (!Ember.isEmpty(onlineRecord)) {
          this.get('offlineAdapter').persistData(typeClass, onlineRecord);
        }
      }).catch(() => {
          onlineResp.then(onlineRecord => {
            if (!Ember.isEmpty(onlineRecord)) {
              adapter.get('offlineAdapter').persistData(typeClass, onlineRecord);
            }
          });
      });

      return onlineResp;
    } else {
      return this.get('offlineAdapter').find(store, type, id, snapshot);
    }
  },

  assertRunner: on('init', function() {
    assert('[ember-data-offline] You should set offline adapter', get(this, 'offlineAdapter'));
  }),

  setup: on('init', function() {
    $(window).on('online', () => {
      this.set('isOnline', true);
    });
    $(window).on('offline', () => {
      this.set('isOnline', false);
    });
    this.set('isOnline', window.navigator.onLine);
  }),

  teardown: on('willDestroy', function() {
    $(window).off('online');
    $(window).off('offline');
  }),
});

@@ -1652,32 +1652,37 @@
 tion(store, type
+Class
 , id, snapshot) 
@@ -1700,25 +1700,26 @@
 his.get('isO
-n
+ff
 line')) %7B%0A  
@@ -1714,24 +1714,106 @@
 line')) %7B%0A  
+    return this.get('offlineAdapter').find(store, typeClass, id, snapshot);%0A    %7D%0A
     let onli
@@ -1861,18 +1861,16 @@
 s);%0A    
-  
 let adap
@@ -1881,18 +1881,16 @@
 = this;%0A
-  
     //ch
@@ -1913,18 +1913,16 @@
 age%0A    
-  
 Ember.RS
@@ -1943,26 +1943,24 @@
 hen(() =%3E %7B%0A
-  
       return
@@ -1995,32 +1995,37 @@
 find(store, type
+Class
 , id, snapshot);
@@ -2025,18 +2025,16 @@
 pshot);%0A
-  
     %7D).t
@@ -2057,26 +2057,24 @@
  =%3E %7B%0A      
-  
 if (Ember.is
@@ -2104,32 +2104,29 @@
         
-  
 return 
-adapter
+online
 Resp;%0A  
@@ -2125,30 +2125,26 @@
 Resp;%0A      
-  %7D%0A  
+%7D%0A
     %7D).then(
@@ -2157,26 +2157,24 @@
 Record =%3E %7B%0A
-  
       if (!E
@@ -2195,34 +2195,32 @@
 nlineRecord)) %7B%0A
-  
         this.get
@@ -2278,30 +2278,26 @@
 ord);%0A      
-  %7D%0A  
+%7D%0A
     %7D).catch
@@ -2301,26 +2301,24 @@
 tch(() =%3E %7B%0A
-  
         onli
@@ -2353,26 +2353,24 @@
 %7B%0A          
- 
- 
 if (!Ember.i
@@ -2389,26 +2389,24 @@
 eRecord)) %7B%0A
-  
             
@@ -2483,22 +2483,18 @@
         
-  %7D%0A  
+%7D%0A
         
@@ -2505,17 +2505,12 @@
     
- 
- 
 %7D);%0A
-%0A  
     
@@ -2532,100 +2532,8 @@
 sp;%0A
-    %7D else %7B%0A      return this.get('offlineAdapter').find(store, type, id, snapshot);%0A    %7D%0A
   %7D,

fix tests