    [cmr.message-queue.services.queue :as queue])
   (:import
    (com.amazonaws ClientConfiguration)
-   (com.amazonaws.auth.policy Condition Policy Principal Statement Statement$Effect)
+   (com.amazonaws.auth.policy Condition Policy Principal Resource Statement Statement$Effect)
    (com.amazonaws.auth.policy.actions SQSActions)
    (com.amazonaws.auth.policy.conditions ConditionFactory)
    (com.amazonaws.services.sns AmazonSNSClient)

  "Returns an access policy allowing the given SNS topic to publish to the given SQS queue."
  [sns-client sqs-client queue-name exchange-names]
  (let [conditions (topic-conditions sns-client exchange-names)
        statement (doto (Statement. Statement$Effect/Allow)
                        (.withPrincipals (into-array Principal [Principal/AllUsers]))
                        (.withActions (into-array SQSActions [SQSActions/SendMessage]))
                        (.withConditions (into-array Condition conditions)))
        policy (.withStatements (Policy.) (into-array Statement [statement]))]
    (.toJson policy)))


;; Tests to make sure SNS/SQS is working
(comment
  ; (.subscribe (:sns-client broker) "arn:aws:sns:us-east-1:688991608580:gsfc-eosdis-cmr-ingest_exchange" "sqs" "arn:aws:sqs:us-east-1:688991608580:gsfc-eosdis-cmr-ngap-cmr-test_queue")
  ;; create a broker
  (def broker (lifecycle/start (create-queue-broker {}) nil))
  ;; list the topics for the cmr-ingest_exchange exchange/topic
  (get-topic (:sns-client broker) "cmr_ingest_exchange")
  ;; list the queues for the cmr_ingest_exchange
  (queue/get-queues-bound-to-exchange broker "ngap-cmr_testB_exchange")
  ;; create a test queue
  (create-queue (:sqs-client broker) "test-ABG")
  ;; create a test exchange
  (create-exchange (:sns-client broker) "ngap-cmr_testB_exchange")
  ;; list the queues for the cmr_test_exchange
  (queue/get-queues-bound-to-exchange broker "cmr_test_exchange")
  ;; Bind the queue to the new exchange
  (bind-queue-to-exchanges (:sns-client broker) (:sqs-client broker) ["ngap-cmr_testB_exchange"] "test-ABG")
  ;; subscribe to test queue with a simple handler that prints received messages
  (queue/subscribe broker "cmr-test.queue" (fn [msg] (println "Got Message: " msg)))
  ;; publish a message to the queue to verify our subscribe worked
  (queue/publish-to-queue broker "cmr-test.queue" "{\"body\": \"ABC\"}")
  ;; publish a message to the exchange to verify the message is sent to the queue
  (queue/publish-to-exchange broker "ngap-cmr_testB_exchange" "{\"body\": \"ABC-exchange\"}"))
