module Aws.Swf.Commands.ListDomains (
  DomainInfo (..),
  ListDomains (..),
  ListDomainsResponse (..)
  ) where

import Data.ByteString               as B
import Control.Monad                 (mzero)
import Control.Applicative           ((<*>), (<$>))
import Data.Aeson                    (Value (Object) , ToJSON (..), FromJSON (..),
                                      object, (.=), (.:), (.:?))
import Aws.Core                      (Transaction, ResponseConsumer (..), AsMemoryResponse (..))
import Aws.Core.SignClass            (SignQuery (..))
import qualified Aws.Core.Sign as S (ServiceConfiguration)
import qualified Aws.Core.Sign3      as SIG3 (signRequest)
import Aws.Swf.Sign                  (swfRequest)
import Aws.Swf.Response              (SwfMetadata, swfResponseConsumer, jsonConsumer)
import qualified Data.Text           as T

target :: B.ByteString
target = "com.amazonaws.swf.service.model.SimpleWorkflowService.ListDomains"

data ListDomains = ListDomains { registrationStatus :: T.Text,
                                 maximumPageSize    :: Int,
                                 reverseOrder       :: Bool,
                                 nextPageTokenReq   :: Maybe T.Text }
                 deriving (Show, Eq)

data DomainInfo = DomainInfo {
  description :: T.Text,
  name :: T.Text,
  status :: T.Text
  } deriving (Show, Eq)

data ListDomainsResponse =
  ListDomainsResponse { domainInfo :: [DomainInfo],
                        nextPageToken :: Maybe T.Text
                      } deriving Show

instance ToJSON ListDomains where
  toJSON (ListDomains registrationStatus maximumPageSize reverseOrder nextPageToken) =
    object [ "maximumPageSize"    .= maximumPageSize,
             "nextPageToken"      .= nextPageToken,
             "registrationStatus" .= registrationStatus,
             "reverseOrder"       .= reverseOrder ]

instance FromJSON DomainInfo where
  parseJSON (Object o) =
    DomainInfo <$>
      o .: "description" <*>
      o .: "name" <*>
      o .: "status"
  parseJSON _ = mzero

instance FromJSON ListDomainsResponse where
  parseJSON (Object o) = ListDomainsResponse <$>
                         o .: "domainInfos" <*>
                         o .:? "nextPageToken"
  parseJSON _ = mzero

instance SignQuery ListDomains where
  type ServiceConfiguration ListDomains = S.ServiceConfiguration

  signQuery lstdoms = SIG3.signRequest $ swfRequest target $ toJSON lstdoms

instance ResponseConsumer ListDomains ListDomainsResponse where

  type ResponseMetadata ListDomainsResponse = SwfMetadata

  responseConsumer _ mref = swfResponseConsumer mref $ \rsp -> jsonConsumer rsp

instance Transaction ListDomains ListDomainsResponse

instance AsMemoryResponse ListDomainsResponse where
  type MemoryResponse ListDomainsResponse = ListDomainsResponse
  loadToMemory = return
