import Ember from 'ember';
import DS from 'ember-data';

const remoteUrl = 'http://192.168.1.31:3480/data_request';
const database  = {};

const remoteMap = {
  'gladys@model:schedule-list:': {
    type: 'schedule-list',
  },
  'gladys@model:schedule:': {
    type: 'schedule',
    flattenDown: {
      events: 'gladys@model:event:',
    },
  },
  'gladys@model:event:': {
    type:       'event',
    sendParent: 'gladys@model:schedule:',
  },
};

const sendRequest = function(data) {
  return new Ember.RSVP.Promise(function(resolve, reject) {
    Ember.$.ajax({
      type: 'GET',
      url:  remoteUrl,
      data: data,
    }).then(function(data) {
      Ember.run(null, resolve, data);
    }, function(jqXHR) {
      jqXHR.then = null;
      Ember.run(null, reject, jqXHR);
    });
  });
};

const clearRemote = function(localType) {
  const data = {
    id:   'lr_scheduler',
    op:   'clear',
    name: localType,
  };

  return sendRequest(data);
};

const saveRemote = function(localType) {
  const data = {
    id:   'lr_scheduler',
    op:   'save',
    name: localType,
  };

  return sendRequest(data);
};

const updateRemote = function(localType) {
  const remoteConfig = remoteMap[localType];

  if (remoteConfig.sendParent) {
    return updateRemote(remoteConfig.sendParent);
  }

  const rawData = _.clone(database[localType]);

  if (remoteConfig.flattenDown) {
    _.each(rawData, function(datum, id) {
      datum = _.clone(datum);
      rawData[id] = datum;

      _.each(remoteConfig.flattenDown, function(type, key) {
        if (!datum[key]) {
          return;
        }

        datum[key] = _.clone(datum[key]);

        _.each(datum[key], function(id, j) {
          const newValue = database[type][id];

          datum[key][j] = newValue;
        });
      });
    });
  }

  const remoteType = remoteMap[localType].type;

  const data = {
    id:   'lr_scheduler',
    op:   'append',

    // TODO: Split into 5000 character chunks.
    data: _.values(rawData),
  };

  // FIXME: This doesn't return the right promise.
  return clearRemote(remoteType).then(function() {
    sendRequest(data).then(function() {
      saveRemote(remoteType);
    });
  });
};

const updateLocal = function() {
  // TODO: Get the remote data and overwrite the existing data with it.
};

export default DS.Adapter.extend({
  generateIdForRecord() {
    return cuid();
  },

  createRecord(store, type, snapshot) {
    const data = {};
    const serializer = store.serializerFor(type.modelName);

    serializer.serializeIntoHash(data, type, snapshot, { includeId: true });

    const localType = type.toString();

    if (!database[localType]) {
      database[localType] = {};
    }

    database[localType][snapshot.id] = data;

    return updateRemote(localType);
  },

  updateRecord(store, type, snapshot) {
    return this.createRecord(store, type, snapshot);
  },

  deleteRecord(store, type, snapshot) {
    const localType = type.toString();

    if (database[localType]) {
      delete database[localType][snapshot.id];
    }

    // TODO: Don't update if the data hasn't changed?
    return updateRemote(localType);
  },

  // TODO
  findRecord() {

  },

  // TODO
  findAll() {

  },

  // TODO
  query() {

  },
});

@@ -1811,49 +1811,199 @@
 nst 
-remoteType = remoteMap%5BlocalType%5D.type
+sendChunks = function(dataString, totalSent, doneCallback) %7B%0A    const chunk     = dataString.slice(0, 4000);%0A    const remainder = dataString.slice(4000);%0A%0A    totalSent += chunk.length
 ;%0A%0A
+  
   co
@@ -2011,32 +2011,34 @@
 st data = %7B%0A    
+  
 id:   'lr_schedu
@@ -2043,24 +2043,26 @@
 duler',%0A    
+  
 op:   'appen
@@ -2069,66 +2069,427 @@
 d',%0A
-%0A
     
-// TODO: Split into 5000 character chunks.%0A    data: 
+  data: chunk,%0A    %7D;%0A%0A    sendRequest(data).then(function(resp) %7B%0A      if (parseInt(resp) !== totalSent) %7B%0A        // FIXME: An error has occurred. Retry.%0A        return;%0A      %7D%0A%0A      if (remainder) %7B%0A        sendChunks(remainder, totalSent, doneCallback);%0A      %7D else %7B%0A        doneCallback(totalSent);%0A      %7D%0A    %7D);%0A  %7D;%0A%0A  const remoteType = remoteMap%5BlocalType%5D.type;%0A  const dataString = JSON.stringify(
 _.va
@@ -2501,21 +2501,17 @@
 rawData)
-,%0A  %7D
+)
 ;%0A%0A  // 
@@ -2556,16 +2556,72 @@
 romise.%0A
+  // FIXME: Check this is returning the right response.%0A
   return
@@ -2675,39 +2675,102 @@
 send
-Request(data).then(function() %7B
+Chunks(dataString, 0, function() %7B%0A      // FIXME: Check this is returning the right response.
 %0A   

Send the data in 4000 character chunks.