             [stonecutter-client.view.voting :as voting]
             [stonecutter-client.view.view-poll :as view-poll]))
 
+;; ======================================================
+(defn authorisation-redirect-response  [stonecutter-config]
+  (let  [callback-uri  (:callback-uri stonecutter-config)
+         oauth-authorisation-path  (str  (:auth-provider-url stonecutter-config)
+                                        "/authorisation?client_id="  (:client-id stonecutter-config)
+                                        "&response_type=code&redirect_uri=" callback-uri)]
+    (->  (r/redirect oauth-authorisation-path)
+        (assoc-in  [:headers "accept"] "text/html"))))
+
+(defn request-access-token!  [stonecutter-config auth-code]
+  (let  [callback-uri  (:callback-uri stonecutter-config)
+         oauth-token-path  (str  (:auth-provider-url stonecutter-config) "/api/token")
+         token-response (http/post oauth-token-path  {:form-params  {:grant_type    "authorization_code"
+                                                                      :redirect_uri  callback-uri
+                                                                      :code          auth-code
+                                                                      :client_id     (:client-id stonecutter-config)
+                                                                      :client_secret  (:client-secret stonecutter-config)}})]
+    (-> token-response :body  (json/parse-string keyword))))
+
+(defn configure  [auth-provider-url
+                  client-id
+                  client-secret
+                  callback-uri]
+  (if  (and auth-provider-url client-id client-secret callback-uri)
+    {:auth-provider-url auth-provider-url
+     :client-id client-id
+     :client-secret client-secret
+     :callback-uri callback-uri}
+    :invalid-configuration))
+;; ======================================================
 (defn get-env
   ([k]
    (get-env k nil))


(defn oauth-callback [request]
  (if-let [auth-code (get-in request [:params :code])]
  (let [client-id (get-env :client-id)
        client-secret (get-env :client-secret)
        callback-uri (str (base-url) "/callback")
        oauth-token-path (str (auth-url) "/api/token")
        token-response (http/post oauth-token-path {:form-params {:grant_type    "authorization_code"
                                                                  :redirect_uri  callback-uri
                                                                  :code          auth-code
                                                                  :client_id     client-id
                                                                  :client_secret client-secret}})
        token-body (-> token-response
                       :body
                       (json/parse-string keyword))]
    (-> (r/redirect (absolute-path :voting))
        (assoc :session {:access-token (:access_token token-body)
                         :user (:user-email token-body)})))
    (r/redirect (absolute-path :home))))

(defn voting [request]

    (r/redirect (absolute-path :voting))
    (html-response (login/login-page request))))

(def stonecutter-config (configure (auth-url)
                                   (get-env :client-id)
                                   (get-env :client-secret)
                                   (str (base-url) "/callback")))

(defn login [request]
  (authorisation-redirect-response stonecutter-config))

(defn logout [request]
  (-> (r/redirect (absolute-path :home))
