module TD.Data.StoryInteraction
  (StoryInteraction(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified TD.Data.MessageSender as MessageSender
import qualified TD.Data.BlockList as BlockList
import qualified TD.Data.StoryInteractionType as StoryInteractionType

data StoryInteraction
  = StoryInteraction -- ^ Represents interaction with a story
    { StoryInteraction -> Maybe MessageSender
actor_id         :: Maybe MessageSender.MessageSender               -- ^ Identifier of the user or chat that made the interaction
    , StoryInteraction -> Maybe Int
interaction_date :: Maybe Int                                       -- ^ Approximate point in time (Unix timestamp) when the interaction happened
    , StoryInteraction -> Maybe BlockList
block_list       :: Maybe BlockList.BlockList                       -- ^ Block list to which the actor is added; may be null if none or for chat stories
    , StoryInteraction -> Maybe StoryInteractionType
_type            :: Maybe StoryInteractionType.StoryInteractionType -- ^ Type of the interaction
    }
  deriving (StoryInteraction -> StoryInteraction -> Bool
(StoryInteraction -> StoryInteraction -> Bool)
-> (StoryInteraction -> StoryInteraction -> Bool)
-> Eq StoryInteraction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StoryInteraction -> StoryInteraction -> Bool
== :: StoryInteraction -> StoryInteraction -> Bool
$c/= :: StoryInteraction -> StoryInteraction -> Bool
/= :: StoryInteraction -> StoryInteraction -> Bool
Eq, Int -> StoryInteraction -> ShowS
[StoryInteraction] -> ShowS
StoryInteraction -> String
(Int -> StoryInteraction -> ShowS)
-> (StoryInteraction -> String)
-> ([StoryInteraction] -> ShowS)
-> Show StoryInteraction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StoryInteraction -> ShowS
showsPrec :: Int -> StoryInteraction -> ShowS
$cshow :: StoryInteraction -> String
show :: StoryInteraction -> String
$cshowList :: [StoryInteraction] -> ShowS
showList :: [StoryInteraction] -> ShowS
Show)

instance I.ShortShow StoryInteraction where
  shortShow :: StoryInteraction -> String
shortShow StoryInteraction
    { actor_id :: StoryInteraction -> Maybe MessageSender
actor_id         = Maybe MessageSender
actor_id_
    , interaction_date :: StoryInteraction -> Maybe Int
interaction_date = Maybe Int
interaction_date_
    , block_list :: StoryInteraction -> Maybe BlockList
block_list       = Maybe BlockList
block_list_
    , _type :: StoryInteraction -> Maybe StoryInteractionType
_type            = Maybe StoryInteractionType
_type_
    }
      = String
"StoryInteraction"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"actor_id"         String -> Maybe MessageSender -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe MessageSender
actor_id_
        , String
"interaction_date" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
interaction_date_
        , String
"block_list"       String -> Maybe BlockList -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe BlockList
block_list_
        , String
"_type"            String -> Maybe StoryInteractionType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StoryInteractionType
_type_
        ]

instance AT.FromJSON StoryInteraction where
  parseJSON :: Value -> Parser StoryInteraction
parseJSON v :: Value
v@(AT.Object Object
obj) = do
    String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String

    case String
t of
      String
"storyInteraction" -> Value -> Parser StoryInteraction
parseStoryInteraction Value
v
      String
_                  -> Parser StoryInteraction
forall a. Monoid a => a
mempty
    
    where
      parseStoryInteraction :: A.Value -> AT.Parser StoryInteraction
      parseStoryInteraction :: Value -> Parser StoryInteraction
parseStoryInteraction = String
-> (Object -> Parser StoryInteraction)
-> Value
-> Parser StoryInteraction
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoryInteraction" ((Object -> Parser StoryInteraction)
 -> Value -> Parser StoryInteraction)
-> (Object -> Parser StoryInteraction)
-> Value
-> Parser StoryInteraction
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe MessageSender
actor_id_         <- Object
o Object -> Key -> Parser (Maybe MessageSender)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"actor_id"
        Maybe Int
interaction_date_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"interaction_date"
        Maybe BlockList
block_list_       <- Object
o Object -> Key -> Parser (Maybe BlockList)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"block_list"
        Maybe StoryInteractionType
_type_            <- Object
o Object -> Key -> Parser (Maybe StoryInteractionType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"type"
        StoryInteraction -> Parser StoryInteraction
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoryInteraction -> Parser StoryInteraction)
-> StoryInteraction -> Parser StoryInteraction
forall a b. (a -> b) -> a -> b
$ StoryInteraction
          { actor_id :: Maybe MessageSender
actor_id         = Maybe MessageSender
actor_id_
          , interaction_date :: Maybe Int
interaction_date = Maybe Int
interaction_date_
          , block_list :: Maybe BlockList
block_list       = Maybe BlockList
block_list_
          , _type :: Maybe StoryInteractionType
_type            = Maybe StoryInteractionType
_type_
          }
  parseJSON Value
_ = Parser StoryInteraction
forall a. Monoid a => a
mempty