'use strict';

angular.module('loom.api',[])
  .factory('loomApi', ['$resource', function ($resource) {
    
    var service = {};

    //config
    var loomApiServer = null;
    service.Config = {};
    service.Config.init = function(configObj) {
      service.Config.protocol = configObj.protocol ? configObj.protocol : "http";
      service.Config.hostname = configObj.hostname ? configObj.hostname : "localhost";
      service.Config.port = configObj.port ? configObj.port : "9000";

      loomApiServer = service.Config.protocol 
                    + "://" 
                    + service.Config.hostname 
                    + ":" 
                    + service.Config.port;
    }
    service.Config.init({});

    //Article Service
    service.Article = {};
    service.Article.getArticle = function(id) {
      var r=$resource(loomApiServer + '/api/articles/getarticle/:id', {},
                      {
                          getArticle: { method: 'GET', params: { id: id }}
                      });

      return r.getArticle({id: id}).$promise.then(function(data) {
        return data;
      });
    };

    service.Article.saveArticle = function(model) {
      console.log(model);
      var r=$resource(loomApiServer + '/api/articles/saveArticle', {},
                      {
                          saveArticle: { method: 'POST', params: {}}
                      });

      return r.saveArticle(model).$promise.then(function(data) {
        return data; 
      });
    };

    service.Article.listAllMyArticles = function() {
      var r=$resource(loomApiServer + '/api/articles/listMyArticles', {}, 
                      {
                          listAllMyArticles: {method: 'GET', isArray: true, params: {getAllData: false}}
                      });
      return r.listAllMyArticles().$promise.then(function(data){
        return data;
      });
    };

    service.Article.delete = function(id, rev) {
      console.log("delete, id:" + id + ", rev:" + rev);
      var r=$resource(loomApiServer + '/api/articles/deleteArticle', {}, 
                      {
                          deleteArticle: {method: 'POST', params: { id: id, rev: rev}}
                      });
      return r.deleteArticle().$promise.then(function(data){
        return data;
      });
    };

    service.Article.updateArticle = function(docData) {
      var r=$resource(loomApiServer + '/api/articles/updateArticle', {},
                      {
                          updateArticle: { method: 'Post', params: { updateData: docData }}
                      });

      return r.updateArticle({updateData: docData}).$promise.then(function(data) {
        return data;
      });
    };

    //User Service
    service.User = {};
    service.User.signInUser = function(username, password){
      var r=$resource(loomApiServer + '/api/users/signin', {},
                      {
                          signInUser: { method: 'Post', params: {username: username, password: password }}
                      });

      return r.signInUser({username: username, password: password }).$promise.then(
        function(success) {
          return success;
        },
        function(error){
          return error;
        });
    };

    service.User.getUser = function(userid) {
      var r=$resource(loomApiServer + '/api/users/getuser/:username', {},
                      {
                          getUser: { method: 'GET', params: {username: '' }}
                      });

      return r.getUser({username: userid}).$promise.then(function(data) {
        return new User(data);
      });
    };

    return service;

  }]);

@@ -121,18 +121,197 @@
 vice = %7B
-%7D;
+%0A      protocol : %22http%22,%0A      hostname : %22localhost%22,%0A      port : %229000%22%0A    %7D;%0A%0A    //TODO create constructor that initialises default config, removing need for config function.
 %0A%0A    //
@@ -380,31 +380,24 @@
     service.
-Config.
 init = funct
@@ -414,24 +414,27 @@
 Obj) %7B%0A     
+ //
  service.Con
@@ -471,30 +471,10 @@
 col 
-? configObj.protocol :
+%7C%7C
  %22ht
@@ -475,32 +475,35 @@
 %7C%7C %22http%22;%0A     
+ //
  service.Config.
@@ -536,30 +536,10 @@
 ame 
-? configObj.hostname :
+%7C%7C
  %22lo
@@ -553,16 +553,19 @@
 %22;%0A     
+ //
  service
@@ -598,34 +598,229 @@
 ort 
-? configObj.port : %229000%22;
+%7C%7C %229000%22;%0A      if(Object.keys(configObj).length !== 0)%7B%0A        for(var prop in arguments%5B0%5D)%7B%0A          if(service.hasOwnProperty(prop))%7B%0A              service%5Bprop%5D=arguments%5B0%5D%5Bprop%5D;   %0A          %7D%0A        %7D%0A      %7D
 %0A%0A  
@@ -839,39 +839,32 @@
 erver = service.
-Config.
 protocol %0A      
@@ -908,39 +908,32 @@
       + service.
-Config.
 hostname %0A      
@@ -979,31 +979,24 @@
   + service.
-Config.
 port;%0A    %7D%0A
@@ -1007,23 +1007,16 @@
 service.
-Config.
 init(%7B%7D)
@@ -3093,16 +3093,50 @@
 sword)%7B%0A
+      console.log(loomApiServer);%0A
       va

refactor of config intialisation to be cleaner