{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies      #-}

module Aws.Swf.Commands.PollForDecisionTask
       where

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

import           Aws.Swf.Commands.JTypes (TaskList (..), WorkflowExecution (..),
                                          WorkflowType (..))
--                                      ActivityTaskCancelRequestedEventAttributes (..),
--                                      ActivityTaskCanceledEventAttributes)

--import Aws.Swf.Commands.Event
import qualified Aws.Swf.Commands.Event  as E

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

data PollForDecisionTask =
  PollForDecisionTask { domain          :: T.Text,
                        identity        :: T.Text,
                        maximumPageSize :: Int,
                        rNextPageToken  :: Maybe T.Text,
                        reverseOrder    :: Bool,
                        taskList        :: TaskList }
  deriving Show

data PollForDecisionTaskResponse =
  PollForDecisionTaskResponse { events                 :: [E.HistoryEvent],
                                nextPageToken          :: Maybe T.Text,
                                previousStartedEventId :: Int,
                                startedEventId         :: Int,
                                taskToken              :: Maybe T.Text,
                                workflowExecution      :: Maybe WorkflowExecution,
                                workflowType           :: Maybe WorkflowType }
  deriving Show

instance ToJSON PollForDecisionTask where
  toJSON (PollForDecisionTask domain identity maximumPageSize rNextPageToken reverseOrder taskList) =
    object [ "domain"          .= domain,
             "identity"        .= identity,
             "maximumPageSize" .= maximumPageSize,
             "nextPageToken"   .= rNextPageToken,
             "reverseOrder"    .= reverseOrder,
             "taskList"        .= taskList ]

instance FromJSON PollForDecisionTaskResponse where
  parseJSON (Object o) = PollForDecisionTaskResponse    <$>
                         o .:? "events" .!= []          <*>
                         o .:? "nextPageToken"          <*>
                         o .:  "previousStartedEventId" <*>
                         o .:  "startedEventId"         <*>
                         o .:? "taskToken"              <*>
                         o .:? "workflowExecution"      <*>
                         o .:? "workflowType"
  parseJSON _ = mzero

instance SignQuery PollForDecisionTask where
  type ServiceConfiguration PollForDecisionTask = S.ServiceConfiguration
  signQuery req = SIG3.signRequest $ swfRequest target $ toJSON req

instance ResponseConsumer PollForDecisionTask PollForDecisionTaskResponse where
  type ResponseMetadata PollForDecisionTaskResponse = SwfMetadata
  responseConsumer _ mref = swfResponseConsumer mref $ \rsp -> jsonConsumer rsp

instance Transaction PollForDecisionTask PollForDecisionTaskResponse

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


