module TD.Query.WriteGeneratedFilePart
  (WriteGeneratedFilePart(..)
  , defaultWriteGeneratedFilePart
  ) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.ByteString as BS

-- | Writes a part of a generated file. This method is intended to be used only if the application has no direct access to TDLib's file system, because it is usually slower than a direct write to the destination file. Returns 'TD.Data.Ok.Ok'
data WriteGeneratedFilePart
  = WriteGeneratedFilePart
    { WriteGeneratedFilePart -> Maybe Int
generation_id :: Maybe Int           -- ^ The identifier of the generation process
    , WriteGeneratedFilePart -> Maybe Int
offset        :: Maybe Int           -- ^ The offset from which to write the data to the file
    , WriteGeneratedFilePart -> Maybe ByteString
_data         :: Maybe BS.ByteString -- ^ The data to write
    }
  deriving (WriteGeneratedFilePart -> WriteGeneratedFilePart -> Bool
(WriteGeneratedFilePart -> WriteGeneratedFilePart -> Bool)
-> (WriteGeneratedFilePart -> WriteGeneratedFilePart -> Bool)
-> Eq WriteGeneratedFilePart
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WriteGeneratedFilePart -> WriteGeneratedFilePart -> Bool
== :: WriteGeneratedFilePart -> WriteGeneratedFilePart -> Bool
$c/= :: WriteGeneratedFilePart -> WriteGeneratedFilePart -> Bool
/= :: WriteGeneratedFilePart -> WriteGeneratedFilePart -> Bool
Eq, Int -> WriteGeneratedFilePart -> ShowS
[WriteGeneratedFilePart] -> ShowS
WriteGeneratedFilePart -> String
(Int -> WriteGeneratedFilePart -> ShowS)
-> (WriteGeneratedFilePart -> String)
-> ([WriteGeneratedFilePart] -> ShowS)
-> Show WriteGeneratedFilePart
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WriteGeneratedFilePart -> ShowS
showsPrec :: Int -> WriteGeneratedFilePart -> ShowS
$cshow :: WriteGeneratedFilePart -> String
show :: WriteGeneratedFilePart -> String
$cshowList :: [WriteGeneratedFilePart] -> ShowS
showList :: [WriteGeneratedFilePart] -> ShowS
Show)

instance I.ShortShow WriteGeneratedFilePart where
  shortShow :: WriteGeneratedFilePart -> String
shortShow
    WriteGeneratedFilePart
      { generation_id :: WriteGeneratedFilePart -> Maybe Int
generation_id = Maybe Int
generation_id_
      , offset :: WriteGeneratedFilePart -> Maybe Int
offset        = Maybe Int
offset_
      , _data :: WriteGeneratedFilePart -> Maybe ByteString
_data         = Maybe ByteString
_data_
      }
        = String
"WriteGeneratedFilePart"
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
          [ String
"generation_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
generation_id_
          , String
"offset"        String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
offset_
          , String
"_data"         String -> Maybe ByteString -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ByteString
_data_
          ]

instance AT.ToJSON WriteGeneratedFilePart where
  toJSON :: WriteGeneratedFilePart -> Value
toJSON
    WriteGeneratedFilePart
      { generation_id :: WriteGeneratedFilePart -> Maybe Int
generation_id = Maybe Int
generation_id_
      , offset :: WriteGeneratedFilePart -> Maybe Int
offset        = Maybe Int
offset_
      , _data :: WriteGeneratedFilePart -> Maybe ByteString
_data         = Maybe ByteString
_data_
      }
        = [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
"writeGeneratedFilePart"
          , Key
"generation_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
generation_id_
          , Key
"offset"        Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
offset_
          , Key
"data"          Key -> Maybe Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= (ByteString -> Value) -> Maybe ByteString -> Maybe Value
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Value
I.writeBytes  Maybe ByteString
_data_
          ]

defaultWriteGeneratedFilePart :: WriteGeneratedFilePart
defaultWriteGeneratedFilePart :: WriteGeneratedFilePart
defaultWriteGeneratedFilePart =
  WriteGeneratedFilePart
    { generation_id :: Maybe Int
generation_id = Maybe Int
forall a. Maybe a
Nothing
    , offset :: Maybe Int
offset        = Maybe Int
forall a. Maybe a
Nothing
    , _data :: Maybe ByteString
_data         = Maybe ByteString
forall a. Maybe a
Nothing
    }