module TD.Data.ActiveStoryState
  (ActiveStoryState(..)) where

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

-- | Describes state of active stories posted by a chat
data ActiveStoryState
  = ActiveStoryStateLive -- ^ The chat has an active live story
    { ActiveStoryState -> Maybe Int
story_id :: Maybe Int -- ^ Identifier of the active live story
    }
  | ActiveStoryStateUnread -- ^ The chat has some unread active stories
  | ActiveStoryStateRead -- ^ The chat has active stories, all of which were read
  deriving (ActiveStoryState -> ActiveStoryState -> Bool
(ActiveStoryState -> ActiveStoryState -> Bool)
-> (ActiveStoryState -> ActiveStoryState -> Bool)
-> Eq ActiveStoryState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ActiveStoryState -> ActiveStoryState -> Bool
== :: ActiveStoryState -> ActiveStoryState -> Bool
$c/= :: ActiveStoryState -> ActiveStoryState -> Bool
/= :: ActiveStoryState -> ActiveStoryState -> Bool
Eq, Int -> ActiveStoryState -> ShowS
[ActiveStoryState] -> ShowS
ActiveStoryState -> String
(Int -> ActiveStoryState -> ShowS)
-> (ActiveStoryState -> String)
-> ([ActiveStoryState] -> ShowS)
-> Show ActiveStoryState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ActiveStoryState -> ShowS
showsPrec :: Int -> ActiveStoryState -> ShowS
$cshow :: ActiveStoryState -> String
show :: ActiveStoryState -> String
$cshowList :: [ActiveStoryState] -> ShowS
showList :: [ActiveStoryState] -> ShowS
Show)

instance I.ShortShow ActiveStoryState where
  shortShow :: ActiveStoryState -> String
shortShow ActiveStoryStateLive
    { story_id :: ActiveStoryState -> Maybe Int
story_id = Maybe Int
story_id_
    }
      = String
"ActiveStoryStateLive"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"story_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
story_id_
        ]
  shortShow ActiveStoryState
ActiveStoryStateUnread
      = String
"ActiveStoryStateUnread"
  shortShow ActiveStoryState
ActiveStoryStateRead
      = String
"ActiveStoryStateRead"

instance AT.FromJSON ActiveStoryState where
  parseJSON :: Value -> Parser ActiveStoryState
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
"activeStoryStateLive"   -> Value -> Parser ActiveStoryState
parseActiveStoryStateLive Value
v
      String
"activeStoryStateUnread" -> ActiveStoryState -> Parser ActiveStoryState
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ActiveStoryState
ActiveStoryStateUnread
      String
"activeStoryStateRead"   -> ActiveStoryState -> Parser ActiveStoryState
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ActiveStoryState
ActiveStoryStateRead
      String
_                        -> Parser ActiveStoryState
forall a. Monoid a => a
mempty
    
    where
      parseActiveStoryStateLive :: A.Value -> AT.Parser ActiveStoryState
      parseActiveStoryStateLive :: Value -> Parser ActiveStoryState
parseActiveStoryStateLive = String
-> (Object -> Parser ActiveStoryState)
-> Value
-> Parser ActiveStoryState
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ActiveStoryStateLive" ((Object -> Parser ActiveStoryState)
 -> Value -> Parser ActiveStoryState)
-> (Object -> Parser ActiveStoryState)
-> Value
-> Parser ActiveStoryState
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
story_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"story_id"
        ActiveStoryState -> Parser ActiveStoryState
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ActiveStoryState -> Parser ActiveStoryState)
-> ActiveStoryState -> Parser ActiveStoryState
forall a b. (a -> b) -> a -> b
$ ActiveStoryStateLive
          { story_id :: Maybe Int
story_id = Maybe Int
story_id_
          }
  parseJSON Value
_ = Parser ActiveStoryState
forall a. Monoid a => a
mempty