module TD.Data.StoryFullId
  ( StoryFullId(..)    
  , defaultStoryFullId 
  ) where

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

data StoryFullId
  = StoryFullId -- ^ Contains identifier of a story along with identifier of its sender
    { StoryFullId -> Maybe Int
sender_chat_id :: Maybe Int -- ^ Identifier of the chat that posted the story
    , StoryFullId -> Maybe Int
story_id       :: Maybe Int -- ^ Unique story identifier among stories of the given sender
    }
  deriving (StoryFullId -> StoryFullId -> Bool
(StoryFullId -> StoryFullId -> Bool)
-> (StoryFullId -> StoryFullId -> Bool) -> Eq StoryFullId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StoryFullId -> StoryFullId -> Bool
== :: StoryFullId -> StoryFullId -> Bool
$c/= :: StoryFullId -> StoryFullId -> Bool
/= :: StoryFullId -> StoryFullId -> Bool
Eq, Int -> StoryFullId -> ShowS
[StoryFullId] -> ShowS
StoryFullId -> String
(Int -> StoryFullId -> ShowS)
-> (StoryFullId -> String)
-> ([StoryFullId] -> ShowS)
-> Show StoryFullId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StoryFullId -> ShowS
showsPrec :: Int -> StoryFullId -> ShowS
$cshow :: StoryFullId -> String
show :: StoryFullId -> String
$cshowList :: [StoryFullId] -> ShowS
showList :: [StoryFullId] -> ShowS
Show)

instance I.ShortShow StoryFullId where
  shortShow :: StoryFullId -> String
shortShow StoryFullId
    { sender_chat_id :: StoryFullId -> Maybe Int
sender_chat_id = Maybe Int
sender_chat_id_
    , story_id :: StoryFullId -> Maybe Int
story_id       = Maybe Int
story_id_
    }
      = String
"StoryFullId"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"sender_chat_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
sender_chat_id_
        , String
"story_id"       String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
story_id_
        ]

instance AT.FromJSON StoryFullId where
  parseJSON :: Value -> Parser StoryFullId
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
"storyFullId" -> Value -> Parser StoryFullId
parseStoryFullId Value
v
      String
_             -> Parser StoryFullId
forall a. Monoid a => a
mempty
    
    where
      parseStoryFullId :: A.Value -> AT.Parser StoryFullId
      parseStoryFullId :: Value -> Parser StoryFullId
parseStoryFullId = String
-> (Object -> Parser StoryFullId) -> Value -> Parser StoryFullId
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoryFullId" ((Object -> Parser StoryFullId) -> Value -> Parser StoryFullId)
-> (Object -> Parser StoryFullId) -> Value -> Parser StoryFullId
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
sender_chat_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sender_chat_id"
        Maybe Int
story_id_       <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"story_id"
        StoryFullId -> Parser StoryFullId
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoryFullId -> Parser StoryFullId)
-> StoryFullId -> Parser StoryFullId
forall a b. (a -> b) -> a -> b
$ StoryFullId
          { sender_chat_id :: Maybe Int
sender_chat_id = Maybe Int
sender_chat_id_
          , story_id :: Maybe Int
story_id       = Maybe Int
story_id_
          }
  parseJSON Value
_ = Parser StoryFullId
forall a. Monoid a => a
mempty

instance AT.ToJSON StoryFullId where
  toJSON :: StoryFullId -> Value
toJSON StoryFullId
    { sender_chat_id :: StoryFullId -> Maybe Int
sender_chat_id = Maybe Int
sender_chat_id_
    , story_id :: StoryFullId -> Maybe Int
story_id       = Maybe Int
story_id_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"          Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"storyFullId"
        , Key
"sender_chat_id" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
sender_chat_id_
        , Key
"story_id"       Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
story_id_
        ]

defaultStoryFullId :: StoryFullId
defaultStoryFullId :: StoryFullId
defaultStoryFullId =
  StoryFullId
    { sender_chat_id :: Maybe Int
sender_chat_id = Maybe Int
forall a. Maybe a
Nothing
    , story_id :: Maybe Int
story_id       = Maybe Int
forall a. Maybe a
Nothing
    }