module TD.Data.ChatTheme
  (ChatTheme(..)) 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
import qualified TD.Data.GiftChatTheme as GiftChatTheme

-- | Describes a chat theme
data ChatTheme
  = ChatThemeEmoji -- ^ A chat theme based on an emoji
    { ChatTheme -> Maybe Text
name :: Maybe T.Text -- ^ Name of the theme; full theme description is received through updateEmojiChatThemes
    }
  | ChatThemeGift -- ^ A chat theme based on an upgraded gift
    { ChatTheme -> Maybe GiftChatTheme
gift_theme :: Maybe GiftChatTheme.GiftChatTheme -- ^ The chat theme
    }
  deriving (ChatTheme -> ChatTheme -> Bool
(ChatTheme -> ChatTheme -> Bool)
-> (ChatTheme -> ChatTheme -> Bool) -> Eq ChatTheme
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChatTheme -> ChatTheme -> Bool
== :: ChatTheme -> ChatTheme -> Bool
$c/= :: ChatTheme -> ChatTheme -> Bool
/= :: ChatTheme -> ChatTheme -> Bool
Eq, Int -> ChatTheme -> ShowS
[ChatTheme] -> ShowS
ChatTheme -> String
(Int -> ChatTheme -> ShowS)
-> (ChatTheme -> String)
-> ([ChatTheme] -> ShowS)
-> Show ChatTheme
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChatTheme -> ShowS
showsPrec :: Int -> ChatTheme -> ShowS
$cshow :: ChatTheme -> String
show :: ChatTheme -> String
$cshowList :: [ChatTheme] -> ShowS
showList :: [ChatTheme] -> ShowS
Show)

instance I.ShortShow ChatTheme where
  shortShow :: ChatTheme -> String
shortShow ChatThemeEmoji
    { name :: ChatTheme -> Maybe Text
name = Maybe Text
name_
    }
      = String
"ChatThemeEmoji"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"name" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
name_
        ]
  shortShow ChatThemeGift
    { gift_theme :: ChatTheme -> Maybe GiftChatTheme
gift_theme = Maybe GiftChatTheme
gift_theme_
    }
      = String
"ChatThemeGift"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"gift_theme" String -> Maybe GiftChatTheme -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe GiftChatTheme
gift_theme_
        ]

instance AT.FromJSON ChatTheme where
  parseJSON :: Value -> Parser ChatTheme
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
"chatThemeEmoji" -> Value -> Parser ChatTheme
parseChatThemeEmoji Value
v
      String
"chatThemeGift"  -> Value -> Parser ChatTheme
parseChatThemeGift Value
v
      String
_                -> Parser ChatTheme
forall a. Monoid a => a
mempty
    
    where
      parseChatThemeEmoji :: A.Value -> AT.Parser ChatTheme
      parseChatThemeEmoji :: Value -> Parser ChatTheme
parseChatThemeEmoji = String -> (Object -> Parser ChatTheme) -> Value -> Parser ChatTheme
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatThemeEmoji" ((Object -> Parser ChatTheme) -> Value -> Parser ChatTheme)
-> (Object -> Parser ChatTheme) -> Value -> Parser ChatTheme
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
name_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"name"
        ChatTheme -> Parser ChatTheme
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatTheme -> Parser ChatTheme) -> ChatTheme -> Parser ChatTheme
forall a b. (a -> b) -> a -> b
$ ChatThemeEmoji
          { name :: Maybe Text
name = Maybe Text
name_
          }
      parseChatThemeGift :: A.Value -> AT.Parser ChatTheme
      parseChatThemeGift :: Value -> Parser ChatTheme
parseChatThemeGift = String -> (Object -> Parser ChatTheme) -> Value -> Parser ChatTheme
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatThemeGift" ((Object -> Parser ChatTheme) -> Value -> Parser ChatTheme)
-> (Object -> Parser ChatTheme) -> Value -> Parser ChatTheme
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe GiftChatTheme
gift_theme_ <- Object
o Object -> Key -> Parser (Maybe GiftChatTheme)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"gift_theme"
        ChatTheme -> Parser ChatTheme
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatTheme -> Parser ChatTheme) -> ChatTheme -> Parser ChatTheme
forall a b. (a -> b) -> a -> b
$ ChatThemeGift
          { gift_theme :: Maybe GiftChatTheme
gift_theme = Maybe GiftChatTheme
gift_theme_
          }
  parseJSON Value
_ = Parser ChatTheme
forall a. Monoid a => a
mempty