module TD.Data.Background
  (Background(..)) 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.Document as Document
import qualified TD.Data.BackgroundType as BackgroundType

data Background
  = Background -- ^ Describes a chat background
    { Background -> Maybe Int
_id        :: Maybe Int                           -- ^ Unique background identifier
    , Background -> Maybe Bool
is_default :: Maybe Bool                          -- ^ True, if this is one of default backgrounds
    , Background -> Maybe Bool
is_dark    :: Maybe Bool                          -- ^ True, if the background is dark and is recommended to be used with dark theme
    , Background -> Maybe Text
name       :: Maybe T.Text                        -- ^ Unique background name
    , Background -> Maybe Document
document   :: Maybe Document.Document             -- ^ Document with the background; may be null. Null only for filled and chat theme backgrounds
    , Background -> Maybe BackgroundType
_type      :: Maybe BackgroundType.BackgroundType -- ^ Type of the background
    }
  deriving (Background -> Background -> Bool
(Background -> Background -> Bool)
-> (Background -> Background -> Bool) -> Eq Background
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Background -> Background -> Bool
== :: Background -> Background -> Bool
$c/= :: Background -> Background -> Bool
/= :: Background -> Background -> Bool
Eq, Int -> Background -> ShowS
[Background] -> ShowS
Background -> String
(Int -> Background -> ShowS)
-> (Background -> String)
-> ([Background] -> ShowS)
-> Show Background
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Background -> ShowS
showsPrec :: Int -> Background -> ShowS
$cshow :: Background -> String
show :: Background -> String
$cshowList :: [Background] -> ShowS
showList :: [Background] -> ShowS
Show)

instance I.ShortShow Background where
  shortShow :: Background -> String
shortShow Background
    { _id :: Background -> Maybe Int
_id        = Maybe Int
_id_
    , is_default :: Background -> Maybe Bool
is_default = Maybe Bool
is_default_
    , is_dark :: Background -> Maybe Bool
is_dark    = Maybe Bool
is_dark_
    , name :: Background -> Maybe Text
name       = Maybe Text
name_
    , document :: Background -> Maybe Document
document   = Maybe Document
document_
    , _type :: Background -> Maybe BackgroundType
_type      = Maybe BackgroundType
_type_
    }
      = String
"Background"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"        String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_id_
        , String
"is_default" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_default_
        , String
"is_dark"    String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_dark_
        , String
"name"       String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
name_
        , String
"document"   String -> Maybe Document -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Document
document_
        , String
"_type"      String -> Maybe BackgroundType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe BackgroundType
_type_
        ]

instance AT.FromJSON Background where
  parseJSON :: Value -> Parser Background
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
"background" -> Value -> Parser Background
parseBackground Value
v
      String
_            -> Parser Background
forall a. Monoid a => a
mempty
    
    where
      parseBackground :: A.Value -> AT.Parser Background
      parseBackground :: Value -> Parser Background
parseBackground = String
-> (Object -> Parser Background) -> Value -> Parser Background
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Background" ((Object -> Parser Background) -> Value -> Parser Background)
-> (Object -> Parser Background) -> Value -> Parser Background
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_        <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe Bool
is_default_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"is_default"
        Maybe Bool
is_dark_    <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"is_dark"
        Maybe Text
name_       <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"name"
        Maybe Document
document_   <- Object
o Object -> Key -> Parser (Maybe Document)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"document"
        Maybe BackgroundType
_type_      <- Object
o Object -> Key -> Parser (Maybe BackgroundType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"type"
        Background -> Parser Background
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Background -> Parser Background)
-> Background -> Parser Background
forall a b. (a -> b) -> a -> b
$ Background
          { _id :: Maybe Int
_id        = Maybe Int
_id_
          , is_default :: Maybe Bool
is_default = Maybe Bool
is_default_
          , is_dark :: Maybe Bool
is_dark    = Maybe Bool
is_dark_
          , name :: Maybe Text
name       = Maybe Text
name_
          , document :: Maybe Document
document   = Maybe Document
document_
          , _type :: Maybe BackgroundType
_type      = Maybe BackgroundType
_type_
          }
  parseJSON Value
_ = Parser Background
forall a. Monoid a => a
mempty