module TD.Data.InputBackground
  (InputBackground(..)) 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.InputFile as InputFile

-- | Contains information about background to set
data InputBackground
  = InputBackgroundLocal -- ^ A background from a local file
    { InputBackground -> Maybe InputFile
background :: Maybe InputFile.InputFile -- ^ Background file to use. Only inputFileLocal and inputFileGenerated are supported. The file must be in JPEG format for wallpapers and in PNG format for patterns
    }
  | InputBackgroundRemote -- ^ A background from the server
    { InputBackground -> Maybe Int
background_id :: Maybe Int -- ^ The background identifier
    }
  | InputBackgroundPrevious -- ^ A background previously set in the chat; for chat backgrounds only
    { InputBackground -> Maybe Int
message_id :: Maybe Int -- ^ Identifier of the message with the background
    }
  deriving (InputBackground -> InputBackground -> Bool
(InputBackground -> InputBackground -> Bool)
-> (InputBackground -> InputBackground -> Bool)
-> Eq InputBackground
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InputBackground -> InputBackground -> Bool
== :: InputBackground -> InputBackground -> Bool
$c/= :: InputBackground -> InputBackground -> Bool
/= :: InputBackground -> InputBackground -> Bool
Eq, Int -> InputBackground -> ShowS
[InputBackground] -> ShowS
InputBackground -> String
(Int -> InputBackground -> ShowS)
-> (InputBackground -> String)
-> ([InputBackground] -> ShowS)
-> Show InputBackground
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InputBackground -> ShowS
showsPrec :: Int -> InputBackground -> ShowS
$cshow :: InputBackground -> String
show :: InputBackground -> String
$cshowList :: [InputBackground] -> ShowS
showList :: [InputBackground] -> ShowS
Show)

instance I.ShortShow InputBackground where
  shortShow :: InputBackground -> String
shortShow InputBackgroundLocal
    { background :: InputBackground -> Maybe InputFile
background = Maybe InputFile
background_
    }
      = String
"InputBackgroundLocal"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"background" String -> Maybe InputFile -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe InputFile
background_
        ]
  shortShow InputBackgroundRemote
    { background_id :: InputBackground -> Maybe Int
background_id = Maybe Int
background_id_
    }
      = String
"InputBackgroundRemote"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"background_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
background_id_
        ]
  shortShow InputBackgroundPrevious
    { message_id :: InputBackground -> Maybe Int
message_id = Maybe Int
message_id_
    }
      = String
"InputBackgroundPrevious"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"message_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
message_id_
        ]

instance AT.FromJSON InputBackground where
  parseJSON :: Value -> Parser InputBackground
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
"inputBackgroundLocal"    -> Value -> Parser InputBackground
parseInputBackgroundLocal Value
v
      String
"inputBackgroundRemote"   -> Value -> Parser InputBackground
parseInputBackgroundRemote Value
v
      String
"inputBackgroundPrevious" -> Value -> Parser InputBackground
parseInputBackgroundPrevious Value
v
      String
_                         -> Parser InputBackground
forall a. Monoid a => a
mempty
    
    where
      parseInputBackgroundLocal :: A.Value -> AT.Parser InputBackground
      parseInputBackgroundLocal :: Value -> Parser InputBackground
parseInputBackgroundLocal = String
-> (Object -> Parser InputBackground)
-> Value
-> Parser InputBackground
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputBackgroundLocal" ((Object -> Parser InputBackground)
 -> Value -> Parser InputBackground)
-> (Object -> Parser InputBackground)
-> Value
-> Parser InputBackground
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe InputFile
background_ <- Object
o Object -> Key -> Parser (Maybe InputFile)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"background"
        InputBackground -> Parser InputBackground
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputBackground -> Parser InputBackground)
-> InputBackground -> Parser InputBackground
forall a b. (a -> b) -> a -> b
$ InputBackgroundLocal
          { background :: Maybe InputFile
background = Maybe InputFile
background_
          }
      parseInputBackgroundRemote :: A.Value -> AT.Parser InputBackground
      parseInputBackgroundRemote :: Value -> Parser InputBackground
parseInputBackgroundRemote = String
-> (Object -> Parser InputBackground)
-> Value
-> Parser InputBackground
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputBackgroundRemote" ((Object -> Parser InputBackground)
 -> Value -> Parser InputBackground)
-> (Object -> Parser InputBackground)
-> Value
-> Parser InputBackground
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
background_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
"background_id"
        InputBackground -> Parser InputBackground
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputBackground -> Parser InputBackground)
-> InputBackground -> Parser InputBackground
forall a b. (a -> b) -> a -> b
$ InputBackgroundRemote
          { background_id :: Maybe Int
background_id = Maybe Int
background_id_
          }
      parseInputBackgroundPrevious :: A.Value -> AT.Parser InputBackground
      parseInputBackgroundPrevious :: Value -> Parser InputBackground
parseInputBackgroundPrevious = String
-> (Object -> Parser InputBackground)
-> Value
-> Parser InputBackground
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputBackgroundPrevious" ((Object -> Parser InputBackground)
 -> Value -> Parser InputBackground)
-> (Object -> Parser InputBackground)
-> Value
-> Parser InputBackground
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
message_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"message_id"
        InputBackground -> Parser InputBackground
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputBackground -> Parser InputBackground)
-> InputBackground -> Parser InputBackground
forall a b. (a -> b) -> a -> b
$ InputBackgroundPrevious
          { message_id :: Maybe Int
message_id = Maybe Int
message_id_
          }
  parseJSON Value
_ = Parser InputBackground
forall a. Monoid a => a
mempty

instance AT.ToJSON InputBackground where
  toJSON :: InputBackground -> Value
toJSON InputBackgroundLocal
    { background :: InputBackground -> Maybe InputFile
background = Maybe InputFile
background_
    }
      = [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
"inputBackgroundLocal"
        , Key
"background" Key -> Maybe InputFile -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe InputFile
background_
        ]
  toJSON InputBackgroundRemote
    { background_id :: InputBackground -> Maybe Int
background_id = Maybe Int
background_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
"inputBackgroundRemote"
        , Key
"background_id" Key -> Maybe Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= (Int -> Value) -> Maybe Int -> Maybe Value
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> Value
I.writeInt64  Maybe Int
background_id_
        ]
  toJSON InputBackgroundPrevious
    { message_id :: InputBackground -> Maybe Int
message_id = Maybe Int
message_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
"inputBackgroundPrevious"
        , Key
"message_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
message_id_
        ]