module TD.Data.MessageStatistics
  (MessageStatistics(..)) 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.StatisticalGraph as StatisticalGraph

data MessageStatistics
  = MessageStatistics -- ^ A detailed statistics about a message
    { MessageStatistics -> Maybe StatisticalGraph
message_interaction_graph :: Maybe StatisticalGraph.StatisticalGraph -- ^ A graph containing number of message views and shares
    , MessageStatistics -> Maybe StatisticalGraph
message_reaction_graph    :: Maybe StatisticalGraph.StatisticalGraph -- ^ A graph containing number of message reactions
    }
  deriving (MessageStatistics -> MessageStatistics -> Bool
(MessageStatistics -> MessageStatistics -> Bool)
-> (MessageStatistics -> MessageStatistics -> Bool)
-> Eq MessageStatistics
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MessageStatistics -> MessageStatistics -> Bool
== :: MessageStatistics -> MessageStatistics -> Bool
$c/= :: MessageStatistics -> MessageStatistics -> Bool
/= :: MessageStatistics -> MessageStatistics -> Bool
Eq, Int -> MessageStatistics -> ShowS
[MessageStatistics] -> ShowS
MessageStatistics -> String
(Int -> MessageStatistics -> ShowS)
-> (MessageStatistics -> String)
-> ([MessageStatistics] -> ShowS)
-> Show MessageStatistics
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MessageStatistics -> ShowS
showsPrec :: Int -> MessageStatistics -> ShowS
$cshow :: MessageStatistics -> String
show :: MessageStatistics -> String
$cshowList :: [MessageStatistics] -> ShowS
showList :: [MessageStatistics] -> ShowS
Show)

instance I.ShortShow MessageStatistics where
  shortShow :: MessageStatistics -> String
shortShow MessageStatistics
    { message_interaction_graph :: MessageStatistics -> Maybe StatisticalGraph
message_interaction_graph = Maybe StatisticalGraph
message_interaction_graph_
    , message_reaction_graph :: MessageStatistics -> Maybe StatisticalGraph
message_reaction_graph    = Maybe StatisticalGraph
message_reaction_graph_
    }
      = String
"MessageStatistics"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"message_interaction_graph" String -> Maybe StatisticalGraph -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StatisticalGraph
message_interaction_graph_
        , String
"message_reaction_graph"    String -> Maybe StatisticalGraph -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StatisticalGraph
message_reaction_graph_
        ]

instance AT.FromJSON MessageStatistics where
  parseJSON :: Value -> Parser MessageStatistics
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
"messageStatistics" -> Value -> Parser MessageStatistics
parseMessageStatistics Value
v
      String
_                   -> Parser MessageStatistics
forall a. Monoid a => a
mempty
    
    where
      parseMessageStatistics :: A.Value -> AT.Parser MessageStatistics
      parseMessageStatistics :: Value -> Parser MessageStatistics
parseMessageStatistics = String
-> (Object -> Parser MessageStatistics)
-> Value
-> Parser MessageStatistics
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"MessageStatistics" ((Object -> Parser MessageStatistics)
 -> Value -> Parser MessageStatistics)
-> (Object -> Parser MessageStatistics)
-> Value
-> Parser MessageStatistics
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe StatisticalGraph
message_interaction_graph_ <- Object
o Object -> Key -> Parser (Maybe StatisticalGraph)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"message_interaction_graph"
        Maybe StatisticalGraph
message_reaction_graph_    <- Object
o Object -> Key -> Parser (Maybe StatisticalGraph)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"message_reaction_graph"
        MessageStatistics -> Parser MessageStatistics
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MessageStatistics -> Parser MessageStatistics)
-> MessageStatistics -> Parser MessageStatistics
forall a b. (a -> b) -> a -> b
$ MessageStatistics
          { message_interaction_graph :: Maybe StatisticalGraph
message_interaction_graph = Maybe StatisticalGraph
message_interaction_graph_
          , message_reaction_graph :: Maybe StatisticalGraph
message_reaction_graph    = Maybe StatisticalGraph
message_reaction_graph_
          }
  parseJSON Value
_ = Parser MessageStatistics
forall a. Monoid a => a
mempty