(function() {
  'use strict';

  describe('drupal.service:', function() {
    var $window;
    var $resource;
    var $http;
    var $log;
    var $q;
    var AppService;
    var Drupal;
    var $rootScope;
    var domain = 'www.streamer.nl';
    var nid = 6;
    var deferred;

    beforeEach(module('app-base'));
    beforeEach(inject(function(_$rootScope_,_$window_,_$resource_,_$q_, _$http_, _$log_,_AppService_,_Drupal_) {
      $rootScope = _$rootScope_;
      $window = _$window_;
      $resource = _$resource_;
      $http = _$http_;
      $log = _$log_;
      $q = _$q_;
      AppService = _AppService_;
      Drupal = _Drupal_;

      Drupal.init({domain:domain})
      spyOn(Drupal,'getParams');

    }));

    it('should have initialized', function() {
      expect(Drupal.domain).toEqual(domain);
      expect(Drupal.Node).not.toEqual(null);
      expect(Drupal.getParams).not.toHaveBeenCalled();
    });

    it('should have connected', function() {
      //fixme: async
      var promise = Drupal.connect();
      promise.then(function(res){
        console.log(res);
        expect(res.user).toBeDefined();
      }, function(error){
        expect("connect error: " + error).toFail();
      }).always()
    });

    it('should get node', function() {
      var id;
      Drupal.Node.get({nid:nid}).then(function(node){
        id = node.nid
      });
      expect(id).toEqual(nid);
    });


    describe('init function', function(Drupal) {
      //Drupal.init({domain:domain});
      //it('Node resource should exist', function() {
      //  expect(Drupal.Node).not.toEqual(null);
      //});

      //it('should return data', function() {
      //  $http.when('GET',  $resource.apiHost + '/contributors?per_page=1').respond(200, [{pprt: 'value'}]);
      //  var data;
      //  $resource.getContributors(1).then(function(fetchedData) {
      //    data = fetchedData;
      //  });
      //  $http.flush();
      //  expect(data).toEqual(jasmine.any(Array));
      //  expect(data.length === 1).toBeTruthy();
      //  expect(data[0]).toEqual(jasmine.any(Object));
      //});

      //it('should define a limit per page as default value', function() {
      //  $http.when('GET',  $resource.apiHost + '/contributors?per_page=30').respond(200, new Array(30));
      //  var data;
      //  $resource.getContributors().then(function(fetchedData) {
      //    data = fetchedData;
      //  });
      //  $http.flush();
      //  expect(data).toEqual(jasmine.any(Array));
      //  expect(data.length === 30).toBeTruthy();
      //});

      //it('should log a error', function() {
      //  $http.when('GET',  $resource.apiHost + '/contributors?per_page=1').respond(500);
      //  $resource.getContributors(1);
      //  $http.flush();
      //  expect($log.error.logs).toEqual(jasmine.stringMatching('XHR Failed for'));
      //});
    });
  });
})();
