 # MIT License
 ###
 
+q = require "q"
+url = require "url"
+https = require "https"
 crypto = require "crypto"
-
+querystring = require "querystring"
+
+oauth_req_path = "/oauth/authorize"
+oauth_access_path = "/oauth/access_token"
+oauth_proto = "https" 
+
+code_regex = /^[a-z0-9]{20}$/gi
+code_regex.compile code_regex
+
+#Stuff for the standalone use case (used by express/connect functionality)
 class PersonalApp
     ###
     PersonalApp is the central class to the library.  Instantiate it to access the Personal API.
     ###
     
-    constructor: (@config) ->
+    constructor: (config) ->
         ###
         config:
             client_id: string - <client id> (required)
             client_secret: string - <client secret> (required)
-        ###
-        @_state = {}
-        @_access = {}
+            sandbox: boolean - use sandbox if true and production if false (default: false)
+        ###
+        @_config = config
+        @_config.hostname = "#{if config.sandbox then "api-sandbox" else "api"}.personal.com"
     
     get_auth_request_url: (options) ->
         ###

        redir_url_obj = url.parse options.redirect_uri, true
        redir_url_obj.search = undefined
        redir_url_obj.query.state = state_param
        @_state[state_param] = url.format(redir_url_obj)
        
        #create url object for user redirect
        if options.sandbox
            urlObj = url.parse("https://api-sandbox.personal.com/oauth/authorize", true)
        else
            urlObj = url.parse("https://api.personal.com/oauth/authorize", true)
        urlObj.search = undefined
        urlObj.query =
            client_id: @config.client_id
            response_type: "code"
            redirect_uri: @_state[state_param]
            scope: options.scope.to_s()
            update: (update == false ? false : true)
        return urlObj

    get_access_token_auth: (code, state, callback) ->
        ###
        Get the access token for Personal API access using authorization code flow

        code: string - code returned in querystring of callback url (required)
        state: string - state parameter return from query string of callback url (required)
        callback: function - function(access_token){console.log(access_token);} (required)
        ###

        #run post
        #place returned infos into @_access
        #run callback(access_token)


    get_client: (access_code) ->
        ###

        ###

class PersonalClient
    ###
    Client 
    ###

class PersonalScope
    ###

            redirect_uri: string - The callback URL that the user will return to after authorization (required)
            scope: PersonalScope - Object representing the scope for which you are requesting authorization (required)
            update: boolean - specifies if the selection UI dialog should be presented even if the 3rd party already has access to the requested resource(default: true)

        returns an object containing
            url: string - a formatted URL string (same formatted output as url.format() - see http://nodejs.org/api/url.html) 
            state: the state parameter in the redirect_uri
        ###

        #add state param to redirect uri for the securitys
