import Ember from 'ember';

const {
  computed,
  inject,
  isEmpty,
  isPresent,
  merge,
  on,
  Service
} = Ember;

export default Service.extend({
  activeState: computed(() => Ember.Object.create()),
  statePoints: computed(() => Ember.A()),
  store: inject.service(),

  initializeState: on('init', async function() {
    const autosave = await this.get('autosave');

    if (isPresent(autosave)) {
      this.loadRecord(autosave);
    }
  }),

  autosave: computed({
    get: async function() {
      let autosave = await this.get('store').queryRecord('ember-theater-local-save', {
        name: 'autosave'
      });

      if (isEmpty(autosave)) {
        autosave = await this.createRecord('autosave');
      }

      return autosave;
    }
  }).readOnly(),

  saves: computed({
    get: async function() {
      return await this.get('store').findAll('ember-theater-local-save');
    }
  }).readOnly(),

  resetAutosave: async function() {
    const autosave = await this.get('autosave');
    
    this.get('statePoints').clear();
    this.set('activeState', Ember.Object.create());

    this.updateRecord(autosave);
  },

  // RECORD MANAGEMENT //
  createRecord: async function(name) {
    const statePoints = Ember.$.extend([], this.get('statePoints'));
    const record = this.get('store').createRecord('ember-theater-local-save', {
      name,
      statePoints
    });

    return await record.save();
  },

  deleteRecord: async function(record) {
    return await record.destroyRecord();
  },

  loadRecord(record) {
    const { activeState, statePoints } = record.getProperties('activeState', 'statePoints');

    this.setProperties({ activeState, statePoints });
  },

  updateRecord: async function(record) {
    const statePoints = Ember.$.extend([], this.get('statePoints'));

    record.set('statePoints', statePoints);

    return await record.save();
  },

  // STATE MANAGEMENT // 
  appendActiveState(optionalValues) {
    const activeState = this.get('activeState');
    const mergedState = merge(optionalValues, activeState);

    this.get('statePoints').pushObject(mergedState);
  },

  deleteStateValue(key) {
    return this.setStateValue(key, undefined);
  },

  getStateValue(key) {
    return this.get(`activeState.${key}`);
  },

  setStateValue(key, value) {
    return this.set(`activeState.${key}`, value);
  }
});

@@ -1963,16 +1963,35 @@
 eState =
+ Ember.$.extend(%7B%7D,
  this.ge
@@ -2006,16 +2006,17 @@
 eState')
+)
 ;%0A    co
@@ -2039,16 +2039,29 @@
 = merge(
+activeState, 
 optional
@@ -2070,21 +2070,8 @@
 lues
-, activeState
 );%0A%0A

fix autosave