module TD.Data.BackgroundType
  (BackgroundType(..)) where

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

-- | Describes the type of background
data BackgroundType
  = BackgroundTypeWallpaper -- ^ A wallpaper in JPEG format
    { BackgroundType -> Maybe Bool
is_blurred :: Maybe Bool -- ^ True, if the wallpaper must be downscaled to fit in 450x450 square and then box-blurred with radius 12
    , BackgroundType -> Maybe Bool
is_moving  :: Maybe Bool -- ^ True, if the background needs to be slightly moved when device is tilted
    }
  | BackgroundTypePattern -- ^ A PNG or TGV (gzipped subset of SVG with MIME type "application/x-tgwallpattern") pattern to be combined with the background fill chosen by the user
    { BackgroundType -> Maybe BackgroundFill
fill        :: Maybe BackgroundFill.BackgroundFill -- ^ Fill of the background
    , BackgroundType -> Maybe Int
intensity   :: Maybe Int                           -- ^ Intensity of the pattern when it is shown above the filled background; 0-100
    , BackgroundType -> Maybe Bool
is_inverted :: Maybe Bool                          -- ^ True, if the background fill must be applied only to the pattern itself. All other pixels are black in this case. For dark themes only
    , is_moving   :: Maybe Bool                          -- ^ True, if the background needs to be slightly moved when device is tilted
    }
  | BackgroundTypeFill -- ^ A filled background
    { fill :: Maybe BackgroundFill.BackgroundFill -- ^ The background fill
    }
  | BackgroundTypeChatTheme -- ^ A background from a chat theme; can be used only as a chat background in channels
    { BackgroundType -> Maybe Text
theme_name :: Maybe T.Text -- ^ Name of the chat theme
    }
  deriving (BackgroundType -> BackgroundType -> Bool
(BackgroundType -> BackgroundType -> Bool)
-> (BackgroundType -> BackgroundType -> Bool) -> Eq BackgroundType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BackgroundType -> BackgroundType -> Bool
== :: BackgroundType -> BackgroundType -> Bool
$c/= :: BackgroundType -> BackgroundType -> Bool
/= :: BackgroundType -> BackgroundType -> Bool
Eq, Int -> BackgroundType -> ShowS
[BackgroundType] -> ShowS
BackgroundType -> String
(Int -> BackgroundType -> ShowS)
-> (BackgroundType -> String)
-> ([BackgroundType] -> ShowS)
-> Show BackgroundType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BackgroundType -> ShowS
showsPrec :: Int -> BackgroundType -> ShowS
$cshow :: BackgroundType -> String
show :: BackgroundType -> String
$cshowList :: [BackgroundType] -> ShowS
showList :: [BackgroundType] -> ShowS
Show)

instance I.ShortShow BackgroundType where
  shortShow :: BackgroundType -> String
shortShow BackgroundTypeWallpaper
    { is_blurred :: BackgroundType -> Maybe Bool
is_blurred = Maybe Bool
is_blurred_
    , is_moving :: BackgroundType -> Maybe Bool
is_moving  = Maybe Bool
is_moving_
    }
      = String
"BackgroundTypeWallpaper"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"is_blurred" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_blurred_
        , String
"is_moving"  String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_moving_
        ]
  shortShow BackgroundTypePattern
    { fill :: BackgroundType -> Maybe BackgroundFill
fill        = Maybe BackgroundFill
fill_
    , intensity :: BackgroundType -> Maybe Int
intensity   = Maybe Int
intensity_
    , is_inverted :: BackgroundType -> Maybe Bool
is_inverted = Maybe Bool
is_inverted_
    , is_moving :: BackgroundType -> Maybe Bool
is_moving   = Maybe Bool
is_moving_
    }
      = String
"BackgroundTypePattern"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"fill"        String -> Maybe BackgroundFill -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe BackgroundFill
fill_
        , String
"intensity"   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
intensity_
        , String
"is_inverted" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_inverted_
        , String
"is_moving"   String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_moving_
        ]
  shortShow BackgroundTypeFill
    { fill :: BackgroundType -> Maybe BackgroundFill
fill = Maybe BackgroundFill
fill_
    }
      = String
"BackgroundTypeFill"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"fill" String -> Maybe BackgroundFill -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe BackgroundFill
fill_
        ]
  shortShow BackgroundTypeChatTheme
    { theme_name :: BackgroundType -> Maybe Text
theme_name = Maybe Text
theme_name_
    }
      = String
"BackgroundTypeChatTheme"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"theme_name" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
theme_name_
        ]

instance AT.FromJSON BackgroundType where
  parseJSON :: Value -> Parser BackgroundType
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
"backgroundTypeWallpaper" -> Value -> Parser BackgroundType
parseBackgroundTypeWallpaper Value
v
      String
"backgroundTypePattern"   -> Value -> Parser BackgroundType
parseBackgroundTypePattern Value
v
      String
"backgroundTypeFill"      -> Value -> Parser BackgroundType
parseBackgroundTypeFill Value
v
      String
"backgroundTypeChatTheme" -> Value -> Parser BackgroundType
parseBackgroundTypeChatTheme Value
v
      String
_                         -> Parser BackgroundType
forall a. Monoid a => a
mempty
    
    where
      parseBackgroundTypeWallpaper :: A.Value -> AT.Parser BackgroundType
      parseBackgroundTypeWallpaper :: Value -> Parser BackgroundType
parseBackgroundTypeWallpaper = String
-> (Object -> Parser BackgroundType)
-> Value
-> Parser BackgroundType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BackgroundTypeWallpaper" ((Object -> Parser BackgroundType)
 -> Value -> Parser BackgroundType)
-> (Object -> Parser BackgroundType)
-> Value
-> Parser BackgroundType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
is_blurred_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_blurred"
        Maybe Bool
is_moving_  <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_moving"
        BackgroundType -> Parser BackgroundType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BackgroundType -> Parser BackgroundType)
-> BackgroundType -> Parser BackgroundType
forall a b. (a -> b) -> a -> b
$ BackgroundTypeWallpaper
          { is_blurred :: Maybe Bool
is_blurred = Maybe Bool
is_blurred_
          , is_moving :: Maybe Bool
is_moving  = Maybe Bool
is_moving_
          }
      parseBackgroundTypePattern :: A.Value -> AT.Parser BackgroundType
      parseBackgroundTypePattern :: Value -> Parser BackgroundType
parseBackgroundTypePattern = String
-> (Object -> Parser BackgroundType)
-> Value
-> Parser BackgroundType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BackgroundTypePattern" ((Object -> Parser BackgroundType)
 -> Value -> Parser BackgroundType)
-> (Object -> Parser BackgroundType)
-> Value
-> Parser BackgroundType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe BackgroundFill
fill_        <- Object
o Object -> Key -> Parser (Maybe BackgroundFill)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"fill"
        Maybe Int
intensity_   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"intensity"
        Maybe Bool
is_inverted_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_inverted"
        Maybe Bool
is_moving_   <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_moving"
        BackgroundType -> Parser BackgroundType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BackgroundType -> Parser BackgroundType)
-> BackgroundType -> Parser BackgroundType
forall a b. (a -> b) -> a -> b
$ BackgroundTypePattern
          { fill :: Maybe BackgroundFill
fill        = Maybe BackgroundFill
fill_
          , intensity :: Maybe Int
intensity   = Maybe Int
intensity_
          , is_inverted :: Maybe Bool
is_inverted = Maybe Bool
is_inverted_
          , is_moving :: Maybe Bool
is_moving   = Maybe Bool
is_moving_
          }
      parseBackgroundTypeFill :: A.Value -> AT.Parser BackgroundType
      parseBackgroundTypeFill :: Value -> Parser BackgroundType
parseBackgroundTypeFill = String
-> (Object -> Parser BackgroundType)
-> Value
-> Parser BackgroundType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BackgroundTypeFill" ((Object -> Parser BackgroundType)
 -> Value -> Parser BackgroundType)
-> (Object -> Parser BackgroundType)
-> Value
-> Parser BackgroundType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe BackgroundFill
fill_ <- Object
o Object -> Key -> Parser (Maybe BackgroundFill)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"fill"
        BackgroundType -> Parser BackgroundType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BackgroundType -> Parser BackgroundType)
-> BackgroundType -> Parser BackgroundType
forall a b. (a -> b) -> a -> b
$ BackgroundTypeFill
          { fill :: Maybe BackgroundFill
fill = Maybe BackgroundFill
fill_
          }
      parseBackgroundTypeChatTheme :: A.Value -> AT.Parser BackgroundType
      parseBackgroundTypeChatTheme :: Value -> Parser BackgroundType
parseBackgroundTypeChatTheme = String
-> (Object -> Parser BackgroundType)
-> Value
-> Parser BackgroundType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BackgroundTypeChatTheme" ((Object -> Parser BackgroundType)
 -> Value -> Parser BackgroundType)
-> (Object -> Parser BackgroundType)
-> Value
-> Parser BackgroundType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
theme_name_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"theme_name"
        BackgroundType -> Parser BackgroundType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BackgroundType -> Parser BackgroundType)
-> BackgroundType -> Parser BackgroundType
forall a b. (a -> b) -> a -> b
$ BackgroundTypeChatTheme
          { theme_name :: Maybe Text
theme_name = Maybe Text
theme_name_
          }
  parseJSON Value
_ = Parser BackgroundType
forall a. Monoid a => a
mempty

instance AT.ToJSON BackgroundType where
  toJSON :: BackgroundType -> Value
toJSON BackgroundTypeWallpaper
    { is_blurred :: BackgroundType -> Maybe Bool
is_blurred = Maybe Bool
is_blurred_
    , is_moving :: BackgroundType -> Maybe Bool
is_moving  = Maybe Bool
is_moving_
    }
      = [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
"backgroundTypeWallpaper"
        , Key
"is_blurred" Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
is_blurred_
        , Key
"is_moving"  Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
is_moving_
        ]
  toJSON BackgroundTypePattern
    { fill :: BackgroundType -> Maybe BackgroundFill
fill        = Maybe BackgroundFill
fill_
    , intensity :: BackgroundType -> Maybe Int
intensity   = Maybe Int
intensity_
    , is_inverted :: BackgroundType -> Maybe Bool
is_inverted = Maybe Bool
is_inverted_
    , is_moving :: BackgroundType -> Maybe Bool
is_moving   = Maybe Bool
is_moving_
    }
      = [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
"backgroundTypePattern"
        , Key
"fill"        Key -> Maybe BackgroundFill -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe BackgroundFill
fill_
        , Key
"intensity"   Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
intensity_
        , Key
"is_inverted" Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
is_inverted_
        , Key
"is_moving"   Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
is_moving_
        ]
  toJSON BackgroundTypeFill
    { fill :: BackgroundType -> Maybe BackgroundFill
fill = Maybe BackgroundFill
fill_
    }
      = [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
"backgroundTypeFill"
        , Key
"fill"  Key -> Maybe BackgroundFill -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe BackgroundFill
fill_
        ]
  toJSON BackgroundTypeChatTheme
    { theme_name :: BackgroundType -> Maybe Text
theme_name = Maybe Text
theme_name_
    }
      = [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
"backgroundTypeChatTheme"
        , Key
"theme_name" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
theme_name_
        ]