module TD.Data.StoryOrigin
  (StoryOrigin(..)) where

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

-- | Contains information about the origin of a story that was reposted
data StoryOrigin
  = StoryOriginPublicStory -- ^ The original story was a public story with known sender
    { StoryOrigin -> Maybe Int
chat_id  :: Maybe Int -- ^ Identifier of the chat that posted original story
    , StoryOrigin -> Maybe Int
story_id :: Maybe Int -- ^ Story identifier of the original story
    }
  | StoryOriginHiddenUser -- ^ The original story was sent by an unknown user
    { StoryOrigin -> Maybe Text
sender_name :: Maybe T.Text -- ^ Name of the story sender
    }
  deriving (StoryOrigin -> StoryOrigin -> Bool
(StoryOrigin -> StoryOrigin -> Bool)
-> (StoryOrigin -> StoryOrigin -> Bool) -> Eq StoryOrigin
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StoryOrigin -> StoryOrigin -> Bool
== :: StoryOrigin -> StoryOrigin -> Bool
$c/= :: StoryOrigin -> StoryOrigin -> Bool
/= :: StoryOrigin -> StoryOrigin -> Bool
Eq, Int -> StoryOrigin -> ShowS
[StoryOrigin] -> ShowS
StoryOrigin -> String
(Int -> StoryOrigin -> ShowS)
-> (StoryOrigin -> String)
-> ([StoryOrigin] -> ShowS)
-> Show StoryOrigin
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StoryOrigin -> ShowS
showsPrec :: Int -> StoryOrigin -> ShowS
$cshow :: StoryOrigin -> String
show :: StoryOrigin -> String
$cshowList :: [StoryOrigin] -> ShowS
showList :: [StoryOrigin] -> ShowS
Show)

instance I.ShortShow StoryOrigin where
  shortShow :: StoryOrigin -> String
shortShow StoryOriginPublicStory
    { chat_id :: StoryOrigin -> Maybe Int
chat_id  = Maybe Int
chat_id_
    , story_id :: StoryOrigin -> Maybe Int
story_id = Maybe Int
story_id_
    }
      = String
"StoryOriginPublicStory"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"chat_id"  String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
chat_id_
        , String
"story_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
story_id_
        ]
  shortShow StoryOriginHiddenUser
    { sender_name :: StoryOrigin -> Maybe Text
sender_name = Maybe Text
sender_name_
    }
      = String
"StoryOriginHiddenUser"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"sender_name" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
sender_name_
        ]

instance AT.FromJSON StoryOrigin where
  parseJSON :: Value -> Parser StoryOrigin
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
"storyOriginPublicStory" -> Value -> Parser StoryOrigin
parseStoryOriginPublicStory Value
v
      String
"storyOriginHiddenUser"  -> Value -> Parser StoryOrigin
parseStoryOriginHiddenUser Value
v
      String
_                        -> Parser StoryOrigin
forall a. Monoid a => a
mempty
    
    where
      parseStoryOriginPublicStory :: A.Value -> AT.Parser StoryOrigin
      parseStoryOriginPublicStory :: Value -> Parser StoryOrigin
parseStoryOriginPublicStory = String
-> (Object -> Parser StoryOrigin) -> Value -> Parser StoryOrigin
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoryOriginPublicStory" ((Object -> Parser StoryOrigin) -> Value -> Parser StoryOrigin)
-> (Object -> Parser StoryOrigin) -> Value -> Parser StoryOrigin
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
chat_id_  <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"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"
        StoryOrigin -> Parser StoryOrigin
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoryOrigin -> Parser StoryOrigin)
-> StoryOrigin -> Parser StoryOrigin
forall a b. (a -> b) -> a -> b
$ StoryOriginPublicStory
          { chat_id :: Maybe Int
chat_id  = Maybe Int
chat_id_
          , story_id :: Maybe Int
story_id = Maybe Int
story_id_
          }
      parseStoryOriginHiddenUser :: A.Value -> AT.Parser StoryOrigin
      parseStoryOriginHiddenUser :: Value -> Parser StoryOrigin
parseStoryOriginHiddenUser = String
-> (Object -> Parser StoryOrigin) -> Value -> Parser StoryOrigin
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoryOriginHiddenUser" ((Object -> Parser StoryOrigin) -> Value -> Parser StoryOrigin)
-> (Object -> Parser StoryOrigin) -> Value -> Parser StoryOrigin
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
sender_name_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sender_name"
        StoryOrigin -> Parser StoryOrigin
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoryOrigin -> Parser StoryOrigin)
-> StoryOrigin -> Parser StoryOrigin
forall a b. (a -> b) -> a -> b
$ StoryOriginHiddenUser
          { sender_name :: Maybe Text
sender_name = Maybe Text
sender_name_
          }
  parseJSON Value
_ = Parser StoryOrigin
forall a. Monoid a => a
mempty