/**
 * Module dependencies.
 */
var util = require('util')
  , OAuth2Strategy = require('passport-oauth').OAuth2Strategy
  , InternalOAuthError = require('passport-oauth').InternalOAuthError;


/**
 * `Strategy` constructor.
 *
 * The Chalkable authentication strategy authenticates requests by delegating to
 * Chalkable using the OAuth 2.0 protocol.
 *
 * Applications must supply a `verify` callback which accepts an `accessToken`,
 * `refreshToken` and service-specific `profile`, and then calls the `done`
 * callback supplying a `user`, which should be set to `false` if the
 * credentials are not valid.  If an exception occured, `err` should be set.
 *
 * Options:
 *   - `clientID`      your Google application's client id
 *   - `clientSecret`  your Google application's client secret
 *   - `callbackURL`   URL to which Google will redirect the user after granting authorization
 *
 * Examples:
 *
 *     passport.use(new ChalkableStrategy({
 *         clientID: 'example.net',
 *         clientSecret: 'shhh-its-a-secret'
 *         callbackURL: 'https://www.example.net/auth/chalkable/callback'
 *       },
 *       function(accessToken, refreshToken, profile, done) {
 *         User.findOrCreate(..., function (err, user) {
 *           done(err, user);
 *         });
 *       }
 *     ));
 *
 * @param {Object} options
 * @param {Function} verify
 * @api public
 */
function Strategy(options, verify) {
  options = options || {};
  options.authorizationURL = options.authorizationURL || 'https://chalkable-access-control.accesscontrol.windows.net/v2/OAuth2-13';
  options.tokenURL = options.tokenURL || 'https://chalkable-access-control.accesscontrol.windows.net/v2/OAuth2-13';
  
  OAuth2Strategy.call(this, options, verify);
  this.name = 'chalkable';
}

/**
 * Inherit from `OAuth2Strategy`.
 */
util.inherits(Strategy, OAuth2Strategy);


/**
 * Retrieve user profile from Chalkable.
 *
 * This function constructs a normalized profile, with the following properties:
 *
 *   - `provider`         always set to `chalkable`
 *   - `id`
 *   - `name`
 *   - `displayName`
 *   - `emails`
 *
 * @param {String} accessToken
 * @param {Function} done
 * @api protected
 */
Strategy.prototype.userProfile = function(accessToken, done) {
  this._oauth2.useAuthorizationHeaderforGET(true);

  this._oauth2.get('https://chalkable.com/Person/Me.json', accessToken, function (err, body, res) {
    if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
    
    try {
      var json = JSON.parse(body);
      
      var profile = { provider: 'chalkable' };
      profile._raw = body;
      profile._json = json.data;
      profile.id = json.id;
      profile.displayName = json.fullname;
      profile.name = {
        familyName: json.lastname,
        givenName: json.firstname
      };
      profile.emails = [{ value: json.email }];
      if (json.pictureid)
        profile.photos = [{ value:'https://chalkable.com/PersonPicture/Read?pictureId=' + json.pictureid + '&width=' + json.pictureheight + '&height=' + json.picturewidth }];

      done(null, profile);
    } catch(e) {
      done(e);
    }
  });
}

/**
 * Return extra Chalkable-specific parameters to be included in the authorization
 * request.
 *
 * @param {Object} options
 * @return {Object}
 * @api protected
 */
Strategy.prototype.authorizationParams = function(options) {
  var params = {};
  return params;
}


/**
 * Expose `Strategy`.
 */
module.exports = Strategy;

@@ -2675,16 +2675,21 @@
  = json.
+data.
 id;%0A    
@@ -2717,16 +2717,21 @@
  = json.
+data.
 fullname
@@ -2780,16 +2780,21 @@
 e: json.
+data.
 lastname
@@ -2819,16 +2819,21 @@
 e: json.
+data.
 firstnam
@@ -2839,24 +2839,90 @@
 me%0A      %7D;%0A
+      profile.role = json.data.role;%0A      if (json.data.email)%0A  
       profil
@@ -2947,16 +2947,21 @@
 e: json.
+data.
 email %7D%5D
@@ -2973,24 +2973,29 @@
    if (json.
+data.
 pictureid)%0A 

fix profile