'use strict';

/* Services */

angular.module('flickrDupFinderServices', ['ngResource'])
  .factory('Flickr', ['$window', '$log', '$resource', '$http', function($window, $log, $resource, $http) {
    var key = 'cF4gOblEUpueTtsL44-gVjZeeXM';
    $window.OAuth.initialize(key, {cache: true});
    var resource;
    $window.OAuth.popup('flickr', {state: 'state'}, function(error, result) {
      if(error){ $log.log('OAuth.popup error: ', error); };
      var oauthio_endpoint = 'https://oauth.io/request/flickr/services/rest/';
      var oauthio = 'k=' + key;
      oauthio += '&oauthv=1';
      function kv_result(key) {
        return '&'+key+'='+encodeURIComponent(result[key])
      }
      oauthio += kv_result('oauth_token');
      oauthio += kv_result('oauth_token_secret');
      oauthio += kv_result('code');
      $http.defaults.headers.common = {oauthio: oauthio};
      resource = $resource('https://oauth.io/request/flickr/services/rest/',
                           {
                             method: "flickr.photos.search",
                             format: "json",
                             user_id: "me",
                             per_page: 10,
                             //text: "vision:outdoor",
                             //tags: "vision:outdoor,vision:outdoor=099",
                             //machine_tags: "outdoor",
                             extras: "date_upload,date_taken,tags",
                             nojsoncallback: 1
                           });
    });
    return resource;
  }]);

@@ -197,16 +197,127 @@
     var 
+oauthd_url = 'http://nisse.lefant.net:6284';%0A    var key = 'foRRKXfQipy7kziBuWDhh1Ibs_k';   // nisse%0A    //var 
 key = 'c
@@ -344,16 +344,28 @@
 jZeeXM';
+ // oauth.io
 %0A    $wi
@@ -403,24 +403,68 @@
 he: true%7D);%0A
+    $window.OAuth.setOAuthdURL(oauthd_url);%0A
     var reso
@@ -519,18 +519,23 @@
 'state'%7D
-, 
+).done(
 function
@@ -539,163 +539,603 @@
 ion(
-error, result) %7B%0A      if(error)%7B $log.log('OAuth.popup error: ', error); %7D;%0A      var oauthio_endpoint = 'https://oauth.io/request/flickr/services/rest/';
+result) %7B%0A      // test request%0A      result.get('services/rest',%0A                 %7Bdata: %7B%0A                   method: %22flickr.photos.search%22,%0A                   format: %22json%22,%0A                   user_id: %22me%22,%0A                   per_page: 10,%0A                   extras: %22date_upload,date_taken,tags%22,%0A                   nojsoncallback: 1%0A                 %7D%7D).done(function(res) %7B%0A                   $log.log('initial OAuth result.get() successful: ', res);%0A                 %7D).fail(function(err) %7B%0A                   $log.log('initial OAuth result.get() failure: ', err);%0A                 %7D);%0A
 %0A   
@@ -1224,24 +1224,16 @@
 t(key) %7B
-%0A       
  return 
@@ -1275,22 +1275,17 @@
 lt%5Bkey%5D)
-%0A     
+;
  %7D%0A     
@@ -1497,25 +1497,22 @@
 rce(
-'https://oauth.io
+oauthd_url + '
 /req
@@ -2089,24 +2089,102 @@
         %7D);%0A
+%0A%0A    %7D).fail(function(error) %7B%0A      $log.log('OAuth.popup error: ', error);%0A
     %7D);%0A    

update oauth flickr wrapper for latest api, use nisse oauthd