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

-- | Points to a file
data InputFile
  = InputFileId -- ^ A file defined by its unique identifier
    { InputFile -> Maybe Int
_id :: Maybe Int -- ^ Unique file identifier
    }
  | InputFileRemote -- ^ A file defined by its remote identifier. The remote identifier is guaranteed to be usable only if the corresponding file is still accessible to the user and known to TDLib. For example, if the file is from a message, then the message must be not deleted and accessible to the user. If the file database is disabled, then the corresponding object with the file must be preloaded by the application
    { InputFile -> Maybe Text
__id :: Maybe T.Text -- ^ Remote file identifier
    }
  | InputFileLocal -- ^ A file defined by a local path
    { InputFile -> Maybe Text
path :: Maybe T.Text -- ^ Local path to the file
    }
  | InputFileGenerated -- ^ A file generated by the application. The application must handle updates updateFileGenerationStart and updateFileGenerationStop to generate the file when asked by TDLib
    { InputFile -> Maybe Text
original_path :: Maybe T.Text -- ^ Local path to a file from which the file is generated. The path doesn't have to be a valid path and is used by TDLib only to detect name and MIME type of the generated file
    , InputFile -> Maybe Text
conversion    :: Maybe T.Text -- ^ String specifying the conversion applied to the original file; must be persistent across application restarts. Conversions beginning with '#' are reserved for internal TDLib usage
    , InputFile -> Maybe Int
expected_size :: Maybe Int    -- ^ Expected size of the generated file, in bytes; pass 0 if unknown
    }
  deriving (InputFile -> InputFile -> Bool
(InputFile -> InputFile -> Bool)
-> (InputFile -> InputFile -> Bool) -> Eq InputFile
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InputFile -> InputFile -> Bool
== :: InputFile -> InputFile -> Bool
$c/= :: InputFile -> InputFile -> Bool
/= :: InputFile -> InputFile -> Bool
Eq, Int -> InputFile -> ShowS
[InputFile] -> ShowS
InputFile -> String
(Int -> InputFile -> ShowS)
-> (InputFile -> String)
-> ([InputFile] -> ShowS)
-> Show InputFile
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InputFile -> ShowS
showsPrec :: Int -> InputFile -> ShowS
$cshow :: InputFile -> String
show :: InputFile -> String
$cshowList :: [InputFile] -> ShowS
showList :: [InputFile] -> ShowS
Show)

instance I.ShortShow InputFile where
  shortShow :: InputFile -> String
shortShow InputFileId
    { _id :: InputFile -> Maybe Int
_id = Maybe Int
_id_
    }
      = String
"InputFileId"
        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_
        ]
  shortShow InputFileRemote
    { __id :: InputFile -> Maybe Text
__id = Maybe Text
__id_
    }
      = String
"InputFileRemote"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"__id" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
__id_
        ]
  shortShow InputFileLocal
    { path :: InputFile -> Maybe Text
path = Maybe Text
path_
    }
      = String
"InputFileLocal"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"path" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
path_
        ]
  shortShow InputFileGenerated
    { original_path :: InputFile -> Maybe Text
original_path = Maybe Text
original_path_
    , conversion :: InputFile -> Maybe Text
conversion    = Maybe Text
conversion_
    , expected_size :: InputFile -> Maybe Int
expected_size = Maybe Int
expected_size_
    }
      = String
"InputFileGenerated"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"original_path" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
original_path_
        , String
"conversion"    String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
conversion_
        , String
"expected_size" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
expected_size_
        ]

instance AT.FromJSON InputFile where
  parseJSON :: Value -> Parser InputFile
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
"inputFileId"        -> Value -> Parser InputFile
parseInputFileId Value
v
      String
"inputFileRemote"    -> Value -> Parser InputFile
parseInputFileRemote Value
v
      String
"inputFileLocal"     -> Value -> Parser InputFile
parseInputFileLocal Value
v
      String
"inputFileGenerated" -> Value -> Parser InputFile
parseInputFileGenerated Value
v
      String
_                    -> Parser InputFile
forall a. Monoid a => a
mempty
    
    where
      parseInputFileId :: A.Value -> AT.Parser InputFile
      parseInputFileId :: Value -> Parser InputFile
parseInputFileId = String -> (Object -> Parser InputFile) -> Value -> Parser InputFile
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputFileId" ((Object -> Parser InputFile) -> Value -> Parser InputFile)
-> (Object -> Parser InputFile) -> Value -> Parser InputFile
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        InputFile -> Parser InputFile
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputFile -> Parser InputFile) -> InputFile -> Parser InputFile
forall a b. (a -> b) -> a -> b
$ InputFileId
          { _id :: Maybe Int
_id = Maybe Int
_id_
          }
      parseInputFileRemote :: A.Value -> AT.Parser InputFile
      parseInputFileRemote :: Value -> Parser InputFile
parseInputFileRemote = String -> (Object -> Parser InputFile) -> Value -> Parser InputFile
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputFileRemote" ((Object -> Parser InputFile) -> Value -> Parser InputFile)
-> (Object -> Parser InputFile) -> Value -> Parser InputFile
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
__id_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        InputFile -> Parser InputFile
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputFile -> Parser InputFile) -> InputFile -> Parser InputFile
forall a b. (a -> b) -> a -> b
$ InputFileRemote
          { __id :: Maybe Text
__id = Maybe Text
__id_
          }
      parseInputFileLocal :: A.Value -> AT.Parser InputFile
      parseInputFileLocal :: Value -> Parser InputFile
parseInputFileLocal = String -> (Object -> Parser InputFile) -> Value -> Parser InputFile
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputFileLocal" ((Object -> Parser InputFile) -> Value -> Parser InputFile)
-> (Object -> Parser InputFile) -> Value -> Parser InputFile
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
path_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"path"
        InputFile -> Parser InputFile
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputFile -> Parser InputFile) -> InputFile -> Parser InputFile
forall a b. (a -> b) -> a -> b
$ InputFileLocal
          { path :: Maybe Text
path = Maybe Text
path_
          }
      parseInputFileGenerated :: A.Value -> AT.Parser InputFile
      parseInputFileGenerated :: Value -> Parser InputFile
parseInputFileGenerated = String -> (Object -> Parser InputFile) -> Value -> Parser InputFile
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputFileGenerated" ((Object -> Parser InputFile) -> Value -> Parser InputFile)
-> (Object -> Parser InputFile) -> Value -> Parser InputFile
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
original_path_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"original_path"
        Maybe Text
conversion_    <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"conversion"
        Maybe Int
expected_size_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"expected_size"
        InputFile -> Parser InputFile
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputFile -> Parser InputFile) -> InputFile -> Parser InputFile
forall a b. (a -> b) -> a -> b
$ InputFileGenerated
          { original_path :: Maybe Text
original_path = Maybe Text
original_path_
          , conversion :: Maybe Text
conversion    = Maybe Text
conversion_
          , expected_size :: Maybe Int
expected_size = Maybe Int
expected_size_
          }
  parseJSON Value
_ = Parser InputFile
forall a. Monoid a => a
mempty

instance AT.ToJSON InputFile where
  toJSON :: InputFile -> Value
toJSON InputFileId
    { _id :: InputFile -> Maybe Int
_id = Maybe Int
_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
"inputFileId"
        , Key
"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
_id_
        ]
  toJSON InputFileRemote
    { __id :: InputFile -> Maybe Text
__id = Maybe Text
__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
"inputFileRemote"
        , Key
"id"    Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
__id_
        ]
  toJSON InputFileLocal
    { path :: InputFile -> Maybe Text
path = Maybe Text
path_
    }
      = [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
"inputFileLocal"
        , Key
"path"  Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
path_
        ]
  toJSON InputFileGenerated
    { original_path :: InputFile -> Maybe Text
original_path = Maybe Text
original_path_
    , conversion :: InputFile -> Maybe Text
conversion    = Maybe Text
conversion_
    , expected_size :: InputFile -> Maybe Int
expected_size = Maybe Int
expected_size_
    }
      = [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
"inputFileGenerated"
        , Key
"original_path" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
original_path_
        , Key
"conversion"    Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
conversion_
        , Key
"expected_size" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
expected_size_
        ]