var get = Ember.get, set = Ember.set;

var adapter, store, rsClient;
var Todo, todo, todos;

module("DS.RSAdapter", {
  setup: function() {
    Ember.run(function() {
      adapter = DS.RSAdapter.create();
      store = DS.Store.create({adapter: adapter});
    });

    Todo = DS.Model.extend({
      title: DS.attr('string'),
      completed: DS.attr('boolean')
    });

    Todo.toString = function() {
      return "App.Todo";
    };

    Todo.reopenClass({
      rs_type: 'todo',
      rs_module: 'todos'
    });

    rsClient = remoteStorage.todos.client;

    stop();
    remoteStorage.claimAccess('todos', 'rw').then(function() {
      start();
    });

    remoteStorage.util.silenceAllLoggers();
  },

  teardown: function() {
    Ember.run(function() {
      adapter.destroy();
      store.destroy();
    });
    remoteStorage.flushLocal();
  }
});

var expectState = function(state, value, t) {
  t = t || todo;

  if (value === undefined) { value = true; }

  var flag = "is" + state.charAt(0).toUpperCase() + state.substr(1);
  equal(get(t, flag), value, "the todo is " + (value === false ? "not " : "") + state);
};

var expectStates = function(state, value) {
  todos.forEach(function(todo) {
    expectState(state, value, todo);
  });
};

test("existence", function() {
  ok(DS.RSAdapter, "RSAdapter added to DS namespace");
  ok(remoteStorage, "remoteStorage exists");
});

test("find", function() {
  var list = [];
  equal(list.length, 0, "list is empty");
});

asyncTest("create a todo", function() {
  expect(4);

  adapter.didCreateRecord = function(store, type, record, newObject) {
    equal(newObject["title"], "Homework");
    equal(type, "App.Todo");

    start();
  };

  Ember.run(function() {
    todo = store.createRecord(Todo, { title: "Homework" });
  });

  expectState("new");

  Ember.run(function() {
    store.commit();
    expectState("saving");
  });
});

asyncTest("find all todos", function() {
  expect(3);

  var newObject = rsClient.buildObject("todo", { title: "Homework", completed: true });
  rsClient.saveObject(newObject).then(function() {
    Ember.run(function() {
      todos = store.find(Todo);

      expectState('loaded');
    });
  });

  adapter.didFindAll = function(store, type, results) {
    var result = results[0];
    equal(result['title'], "Homework");
    equal(result['completed'], true);

    start();
  };
});

asyncTest("find a single todo", function() {
  expect(4);

  var newObject = rsClient.buildObject("todo", { title: "Homework", completed: true });
  rsClient.saveObject(newObject).then(function() {
    Ember.run(function() {
      todos = store.find(Todo, newObject['id']);

      expectState('loaded');
    });
  });

  adapter.didFindRecord = function(store, type, result, id) {
    equal(result['title'], "Homework");
    equal(result['completed'], true);
    equal(id, newObject['id']);

    start();
  };
});

asyncTest("delete a record", function() {
  expect(2);

  var id;

  adapter.didDeleteRecord = function(store, type, record) {
    equal(type, "App.Todo");
    equal(id, record.get('id'));

    start();
  };

  Ember.run(function() {
    todo = Todo.createRecord({ title: "Homework", completed: false });

    todo.addObserver('id', function() {
      todo.addObserver('stateManager.currentPath', function() {
        if (todo.get('stateManager.currentPath') === 'rootState.loaded.saved') {
          id = todo.get('id');
          todo.deleteRecord();
          store.commit();
        }
      });
    });

    store.commit();
  });
});

@@ -2881,32 +2881,759 @@
 rt();%0A  %7D;%0A%7D);%0A%0A
+asyncTest(%22update a todo%22, function() %7B%0A  expect(3);%0A%0A  var id;%0A%0A  adapter.didSaveRecord = function(store, type, record, data) %7B%0A    equal(data%5B'id'%5D, id);%0A    equal(data%5B'title'%5D, 'Laundry');%0A    equal(data%5B'completed'%5D, true);%0A%0A    start();%0A  %7D;%0A%0A  Ember.run(function() %7B%0A    todo = Todo.createRecord(%7B title: %22Homework%22, completed: false %7D);%0A%0A    todo.addObserver('id', function() %7B%0A      todo.addObserver('stateManager.currentPath', function() %7B%0A        if (todo.get('stateManager.currentPath') === 'rootState.loaded.saved') %7B%0A          id = todo.get('id');%0A          todo.set('title', 'Laundry');%0A          todo.set('completed', true);%0A          store.commit();%0A        %7D%0A      %7D);%0A    %7D);%0A%0A    store.commit();%0A  %7D);%0A%7D);%0A%0A
 asyncTest(%22delet

Test updateRecord