 (ns api.core
-  (:import [com.google.appengine.tools.remoteapi RemoteApiInstaller RemoteApiOptions]
+  (:import [com.gallatinsystems.common Constants]
+           [com.gallatinsystems.survey.dao SurveyDAO SurveyGroupDAO]
            [com.gallatinsystems.user.dao UserDao]
-           [com.gallatinsystems.survey.dao SurveyDAO]
-           [com.gallatinsystems.common Constants]
-           [org.apache.commons.codec.binary Base64]
+           [com.google.appengine.tools.remoteapi RemoteApiInstaller RemoteApiOptions]
            [com.google.apphosting.utils.config AppEngineWebXmlReader]
-           [java.io ByteArrayInputStream])
-  (:require [clojure.string :as str]
+           [java.io ByteArrayInputStream]
+           [org.apache.commons.codec.binary Base64])
+  (:require [clj-http.client :as http]
             [clojure.java.io :as io]
-            [clj-http.client :as http]))
+            [clojure.string :as str]))
 
-(def p12-dir (System/getProperty "java.io.tmpdir"))
-
-#_(let [auth-token ""
-        email ""
-        alias-map (get-alias-map instance-map)
-        instance-id (get alias-map "uat1")
-        host (str instance-id ".appspot.com")
-        port 443
-        iam-account (get-in instance-map [instance-id "serviceAccountId"])
-        p12-file (get-p12 instance-id auth-token)]
-    (get-filtered-surveys host iam-account (.getAbsolutePath p12-file) email))
+(def tmp-dir (System/getProperty "java.io.tmpdir"))
 
 (defn get-filtered-surveys
   [host iam-account p12-path email]

        (finally
          (.uninstall installer))))))

(defn get-instances [auth-token]
  (let [folders (->> (http/get "https://api.github.com/repos/akvo/akvo-flow-server-config/contents/"
                               {:headers {"Authorization" (format "token %s" auth-token)}
                                :as :json})
                     :body
                     (filter #(= "dir" (:type %)))
                     (map :name))]
    folders))

(defn get-instance-props [instance-id auth-token]
  (let [temp-file (java.io.File/createTempFile "appengine" ".xml")
        _ (spit temp-file
                (String.
                 (Base64/decodeBase64
                  (-> (http/get (format "https://api.github.com/repos/akvo/akvo-flow-server-config/contents/%s/appengine-web.xml"
                                        instance-id)
                                {:headers {"Authorization" (format "token %s"
                                                                   auth-token)}
                                 :as :json})
                      :body
                      :content))))
        ae-reader (AppEngineWebXmlReader. (System/getProperty "java.io.tmpdir")
                                          (.getName temp-file))]
    {:instance-id instance-id
     :properties (.getSystemProperties (.readAppEngineWebXml ae-reader))}))

(defn get-instance-map [auth-token]
  (let [instances (get-instances auth-token)]
    (reduce (fn [m {:keys [properties instance-id]}]
              (if (nil? instance-id)
                m
                (assoc m instance-id properties)))
            {}
            (map (fn [instance-id]
                   (try
                     (get-instance-props instance-id auth-token)
                     (catch clojure.lang.ExceptionInfo e)))
                 instances))))

(defn get-alias-map [instances-map]
  (reduce (fn [m [instance-id props]]

          {}
          instances-map))

(defn fetch-p12
  "Fetch the p12 file from github. Save it and return the File.
  Returns nil if the file can't be found"
  [instance-id auth-token]
  (try
    (let [bytes (-> (github-contents auth-token (format "/%1$s/%1$s.p12" instance-id))
                    :content
                    Base64/decodeBase64)
          file (io/file tmp-dir (str instance-id ".p12"))]
      (io/copy bytes file)
      file)
    (catch clojure.lang.ExceptionInfo e)))

(defn get-p12 [instance-id auth-token]
  (let [file (io/file tmp-dir (str instance-id ".p12"))]
    (if (.exists file)
      file
      (fetch-p12 instance-id auth-token))))

#_(let [auth-token ""
        email ""
        alias-map (get-alias-map instance-map)
        instance-id (get alias-map "uat1")
        host (str instance-id ".appspot.com")
        port 443
        iam-account (get-in instance-map [instance-id "serviceAccountId"])
        p12-file (get-p12 instance-id auth-token)]
    (get-filtered-surveys host iam-account (.getAbsolutePath p12-file) email))
