text: ﻿'use strict';
  app.factory('authService', ['$http', '$q', 'localStorageService', 'ngAuthSettings', function ($http, $q, localStorageService, ngAuthSettings) {
  
      var serviceBase = ngAuthSettings.apiServiceBaseUri;
      var authorizeUri = ngAuthSettings.authorizeUri;
      var returnUri = ngAuthSettings.returnUri;
      var authServiceFactory = {};
  
      var _authentication = {
          isAuth: false,
          userName: "",
          useRefreshTokens: false
      };
  
      var _externalAuthData = {
          provider: "",
          userName: "",
          externalAccessToken: ""
      };
  
      var _saveRegistration = function (registration) {
  
          _logOut();
  
          return $http.post(serviceBase + 'api/account/register', registration).then(function (response) {
              return response;
          });
  
      };
  
      var _getAcessTokenFromCode = function (code) {
          var deferred = $q.defer();
  
          var data = "grant_type=authorization_code&code=" + code + "&client_id=6890ab&client_secret=7890ab&redirect_uri=" + returnUri;
  
  
          $http.post(ngAuthSettings.authorizeUri + '/OAuth/Token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) {
              console.log('authorizationData', response);
          }).error(function (data, status, headers, config) {
              console.log('authorizationData', data);
          });
  
          //$http({
          //    method: 'POST',
          //    url: ngAuthSettings.authorizeUri +'/OAuth/Token',
          //    headers: {
          //        'Content-Type': 'application/x-www-form-urlencoded',
          //    },
          //    //transformRequest: function (obj) {
          //    //    var str = [];
          //    //    for (var p in obj)
          //    //        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
          //    //    return str.join("&");
          //    //},
          //    data: {
          //        code: code,
          //        client_id: "6890ab",
          //        redirect_uri: returnUri,
          //        grant_type: 'authorization_code',
          //       // state: 'my-nonce',
          //       // scope: 'bio notes',
          //        client_secret:'7890ab'
          //    }
          //}).success(function (data, status, headers, config) {
          //    console.log('authorizationData', data);
          //}).error(function (data, status, headers, config) {
          //});
          return deferred.promise;
      }
  
      var _login = function (loginData) {
  
          var nonce = 'my-nonce';
  
          var uri = addQueryString(authorizeUri +'/OAuth/Authorize', {
              'client_id': '6890ab',
              'redirect_uri': returnUri,
              'state': nonce,
              'scope': 'bio notes',
              'response_type': 'code',
          });
  
          //redirect
           window.location.replace(uri);
      };
  
      function addQueryString(uri, parameters) {
          var delimiter = (uri.indexOf('?') == -1) ? '?' : '&';
          for (var parameterName in parameters) {
              var parameterValue = parameters[parameterName];
              uri += delimiter + encodeURIComponent(parameterName) + '=' + encodeURIComponent(parameterValue);
              delimiter = '&';
          }
          return uri;
      }
  
      var _logOut = function () {
  
          localStorageService.remove('authorizationData');
  
          _authentication.isAuth = false;
          _authentication.userName = "";
          _authentication.useRefreshTokens = false;
  
      };
  
      var _fillAuthData = function () {
  
          var authData = localStorageService.get('authorizationData');
          if (authData) {
              _authentication.isAuth = true;
              _authentication.userName = authData.userName;
              _authentication.useRefreshTokens = authData.useRefreshTokens;
          }
  
      };
  
      var _refreshToken = function () {
          var deferred = $q.defer();
  
          var authData = localStorageService.get('authorizationData');
  
          if (authData) {
  
              if (authData.useRefreshTokens) {
  
                  var data = "grant_type=refresh_token&refresh_token=" + authData.refreshToken + "&client_id=" + ngAuthSettings.clientId;
  
                  localStorageService.remove('authorizationData');
  
                  $http.post(serviceBase + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) {
  
                      localStorageService.set('authorizationData', { token: response.access_token, userName: response.userName, refreshToken: response.refresh_token, useRefreshTokens: true });
  
                      deferred.resolve(response);
  
                  }).error(function (err, status) {
                      _logOut();
                      deferred.reject(err);
                  });
              }
          }
  
          return deferred.promise;
      };
  
      var _obtainAccessToken = function (externalData) {
  
          var deferred = $q.defer();
  
          $http.get(serviceBase + 'api/account/ObtainLocalAccessToken', { params: { provider: externalData.provider, externalAccessToken: externalData.externalAccessToken } }).success(function (response) {
  
              localStorageService.set('authorizationData', { token: response.access_token, userName: response.userName, refreshToken: "", useRefreshTokens: false });
  
              _authentication.isAuth = true;
              _authentication.userName = response.userName;
              _authentication.useRefreshTokens = false;
  
              deferred.resolve(response);
  
          }).error(function (err, status) {
              _logOut();
              deferred.reject(err);
          });
  
          return deferred.promise;
  
      };
  
      var _registerExternal = function (registerExternalData) {
  
          var deferred = $q.defer();
  
          $http.post(serviceBase + 'api/account/registerexternal', registerExternalData).success(function (response) {
  
              localStorageService.set('authorizationData', { token: response.access_token, userName: response.userName, refreshToken: "", useRefreshTokens: false });
  
              _authentication.isAuth = true;
              _authentication.userName = response.userName;
              _authentication.useRefreshTokens = false;
  
              deferred.resolve(response);
  
          }).error(function (err, status) {
              _logOut();
              deferred.reject(err);
          });
  
          return deferred.promise;
  
      };
  
      authServiceFactory.saveRegistration = _saveRegistration;
      authServiceFactory.login = _login;
      authServiceFactory.logOut = _logOut;
      authServiceFactory.fillAuthData = _fillAuthData;
      authServiceFactory.authentication = _authentication;
      authServiceFactory.refreshToken = _refreshToken;
  
      authServiceFactory.obtainAccessToken = _obtainAccessToken;
      authServiceFactory.externalAuthData = _externalAuthData;
          authServiceFactory.registerExternal = _registerExternal;
  
          authServiceFactory.GetAcessTokenFromCode = _getAcessTokenFromCode;
  
      return authServiceFactory;
  }]);