module TD.Data.AutosaveSettingsScope
  (AutosaveSettingsScope(..)) where

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

-- | Describes scope of autosave settings
data AutosaveSettingsScope
  = AutosaveSettingsScopePrivateChats -- ^ Autosave settings applied to all private chats without chat-specific settings
  | AutosaveSettingsScopeGroupChats -- ^ Autosave settings applied to all basic group and supergroup chats without chat-specific settings
  | AutosaveSettingsScopeChannelChats -- ^ Autosave settings applied to all channel chats without chat-specific settings
  | AutosaveSettingsScopeChat -- ^ Autosave settings applied to a chat
    { AutosaveSettingsScope -> Maybe Int
chat_id :: Maybe Int -- ^ Chat identifier
    }
  deriving (AutosaveSettingsScope -> AutosaveSettingsScope -> Bool
(AutosaveSettingsScope -> AutosaveSettingsScope -> Bool)
-> (AutosaveSettingsScope -> AutosaveSettingsScope -> Bool)
-> Eq AutosaveSettingsScope
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AutosaveSettingsScope -> AutosaveSettingsScope -> Bool
== :: AutosaveSettingsScope -> AutosaveSettingsScope -> Bool
$c/= :: AutosaveSettingsScope -> AutosaveSettingsScope -> Bool
/= :: AutosaveSettingsScope -> AutosaveSettingsScope -> Bool
Eq, Int -> AutosaveSettingsScope -> ShowS
[AutosaveSettingsScope] -> ShowS
AutosaveSettingsScope -> String
(Int -> AutosaveSettingsScope -> ShowS)
-> (AutosaveSettingsScope -> String)
-> ([AutosaveSettingsScope] -> ShowS)
-> Show AutosaveSettingsScope
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AutosaveSettingsScope -> ShowS
showsPrec :: Int -> AutosaveSettingsScope -> ShowS
$cshow :: AutosaveSettingsScope -> String
show :: AutosaveSettingsScope -> String
$cshowList :: [AutosaveSettingsScope] -> ShowS
showList :: [AutosaveSettingsScope] -> ShowS
Show)

instance I.ShortShow AutosaveSettingsScope where
  shortShow :: AutosaveSettingsScope -> String
shortShow AutosaveSettingsScope
AutosaveSettingsScopePrivateChats
      = String
"AutosaveSettingsScopePrivateChats"
  shortShow AutosaveSettingsScope
AutosaveSettingsScopeGroupChats
      = String
"AutosaveSettingsScopeGroupChats"
  shortShow AutosaveSettingsScope
AutosaveSettingsScopeChannelChats
      = String
"AutosaveSettingsScopeChannelChats"
  shortShow AutosaveSettingsScopeChat
    { chat_id :: AutosaveSettingsScope -> Maybe Int
chat_id = Maybe Int
chat_id_
    }
      = String
"AutosaveSettingsScopeChat"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"chat_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
chat_id_
        ]

instance AT.FromJSON AutosaveSettingsScope where
  parseJSON :: Value -> Parser AutosaveSettingsScope
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
"autosaveSettingsScopePrivateChats" -> AutosaveSettingsScope -> Parser AutosaveSettingsScope
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AutosaveSettingsScope
AutosaveSettingsScopePrivateChats
      String
"autosaveSettingsScopeGroupChats"   -> AutosaveSettingsScope -> Parser AutosaveSettingsScope
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AutosaveSettingsScope
AutosaveSettingsScopeGroupChats
      String
"autosaveSettingsScopeChannelChats" -> AutosaveSettingsScope -> Parser AutosaveSettingsScope
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AutosaveSettingsScope
AutosaveSettingsScopeChannelChats
      String
"autosaveSettingsScopeChat"         -> Value -> Parser AutosaveSettingsScope
parseAutosaveSettingsScopeChat Value
v
      String
_                                   -> Parser AutosaveSettingsScope
forall a. Monoid a => a
mempty
    
    where
      parseAutosaveSettingsScopeChat :: A.Value -> AT.Parser AutosaveSettingsScope
      parseAutosaveSettingsScopeChat :: Value -> Parser AutosaveSettingsScope
parseAutosaveSettingsScopeChat = String
-> (Object -> Parser AutosaveSettingsScope)
-> Value
-> Parser AutosaveSettingsScope
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"AutosaveSettingsScopeChat" ((Object -> Parser AutosaveSettingsScope)
 -> Value -> Parser AutosaveSettingsScope)
-> (Object -> Parser AutosaveSettingsScope)
-> Value
-> Parser AutosaveSettingsScope
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
chat_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"chat_id"
        AutosaveSettingsScope -> Parser AutosaveSettingsScope
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AutosaveSettingsScope -> Parser AutosaveSettingsScope)
-> AutosaveSettingsScope -> Parser AutosaveSettingsScope
forall a b. (a -> b) -> a -> b
$ AutosaveSettingsScopeChat
          { chat_id :: Maybe Int
chat_id = Maybe Int
chat_id_
          }
  parseJSON Value
_ = Parser AutosaveSettingsScope
forall a. Monoid a => a
mempty

instance AT.ToJSON AutosaveSettingsScope where
  toJSON :: AutosaveSettingsScope -> Value
toJSON AutosaveSettingsScope
AutosaveSettingsScopePrivateChats
      = [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
"autosaveSettingsScopePrivateChats"
        ]
  toJSON AutosaveSettingsScope
AutosaveSettingsScopeGroupChats
      = [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
"autosaveSettingsScopeGroupChats"
        ]
  toJSON AutosaveSettingsScope
AutosaveSettingsScopeChannelChats
      = [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
"autosaveSettingsScopeChannelChats"
        ]
  toJSON AutosaveSettingsScopeChat
    { chat_id :: AutosaveSettingsScope -> Maybe Int
chat_id = Maybe Int
chat_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
"autosaveSettingsScopeChat"
        , Key
"chat_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
chat_id_
        ]