module TD.Data.BotAccessSettings
  ( BotAccessSettings(..)    
  , defaultBotAccessSettings 
  ) where

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

data BotAccessSettings
  = BotAccessSettings -- ^ Describes users that have access to a bot
    { BotAccessSettings -> Maybe Bool
is_restricted  :: Maybe Bool  -- ^ True, if access to the bot is restricted to its owner and selected users
    , BotAccessSettings -> Maybe [Int]
added_user_ids :: Maybe [Int] -- ^ Identifiers of the users who can use the bot additionally to the owner of the bot
    }
  deriving (BotAccessSettings -> BotAccessSettings -> Bool
(BotAccessSettings -> BotAccessSettings -> Bool)
-> (BotAccessSettings -> BotAccessSettings -> Bool)
-> Eq BotAccessSettings
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BotAccessSettings -> BotAccessSettings -> Bool
== :: BotAccessSettings -> BotAccessSettings -> Bool
$c/= :: BotAccessSettings -> BotAccessSettings -> Bool
/= :: BotAccessSettings -> BotAccessSettings -> Bool
Eq, Int -> BotAccessSettings -> ShowS
[BotAccessSettings] -> ShowS
BotAccessSettings -> String
(Int -> BotAccessSettings -> ShowS)
-> (BotAccessSettings -> String)
-> ([BotAccessSettings] -> ShowS)
-> Show BotAccessSettings
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BotAccessSettings -> ShowS
showsPrec :: Int -> BotAccessSettings -> ShowS
$cshow :: BotAccessSettings -> String
show :: BotAccessSettings -> String
$cshowList :: [BotAccessSettings] -> ShowS
showList :: [BotAccessSettings] -> ShowS
Show)

instance I.ShortShow BotAccessSettings where
  shortShow :: BotAccessSettings -> String
shortShow BotAccessSettings
    { is_restricted :: BotAccessSettings -> Maybe Bool
is_restricted  = Maybe Bool
is_restricted_
    , added_user_ids :: BotAccessSettings -> Maybe [Int]
added_user_ids = Maybe [Int]
added_user_ids_
    }
      = String
"BotAccessSettings"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"is_restricted"  String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_restricted_
        , String
"added_user_ids" String -> Maybe [Int] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Int]
added_user_ids_
        ]

instance AT.FromJSON BotAccessSettings where
  parseJSON :: Value -> Parser BotAccessSettings
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
"botAccessSettings" -> Value -> Parser BotAccessSettings
parseBotAccessSettings Value
v
      String
_                   -> Parser BotAccessSettings
forall a. Monoid a => a
mempty
    
    where
      parseBotAccessSettings :: A.Value -> AT.Parser BotAccessSettings
      parseBotAccessSettings :: Value -> Parser BotAccessSettings
parseBotAccessSettings = String
-> (Object -> Parser BotAccessSettings)
-> Value
-> Parser BotAccessSettings
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BotAccessSettings" ((Object -> Parser BotAccessSettings)
 -> Value -> Parser BotAccessSettings)
-> (Object -> Parser BotAccessSettings)
-> Value
-> Parser BotAccessSettings
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
is_restricted_  <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_restricted"
        Maybe [Int]
added_user_ids_ <- Object
o Object -> Key -> Parser (Maybe [Int])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"added_user_ids"
        BotAccessSettings -> Parser BotAccessSettings
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BotAccessSettings -> Parser BotAccessSettings)
-> BotAccessSettings -> Parser BotAccessSettings
forall a b. (a -> b) -> a -> b
$ BotAccessSettings
          { is_restricted :: Maybe Bool
is_restricted  = Maybe Bool
is_restricted_
          , added_user_ids :: Maybe [Int]
added_user_ids = Maybe [Int]
added_user_ids_
          }
  parseJSON Value
_ = Parser BotAccessSettings
forall a. Monoid a => a
mempty

instance AT.ToJSON BotAccessSettings where
  toJSON :: BotAccessSettings -> Value
toJSON BotAccessSettings
    { is_restricted :: BotAccessSettings -> Maybe Bool
is_restricted  = Maybe Bool
is_restricted_
    , added_user_ids :: BotAccessSettings -> Maybe [Int]
added_user_ids = Maybe [Int]
added_user_ids_
    }
      = [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
"botAccessSettings"
        , Key
"is_restricted"  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_restricted_
        , Key
"added_user_ids" Key -> Maybe [Int] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe [Int]
added_user_ids_
        ]

defaultBotAccessSettings :: BotAccessSettings
defaultBotAccessSettings :: BotAccessSettings
defaultBotAccessSettings =
  BotAccessSettings
    { is_restricted :: Maybe Bool
is_restricted  = Maybe Bool
forall a. Maybe a
Nothing
    , added_user_ids :: Maybe [Int]
added_user_ids = Maybe [Int]
forall a. Maybe a
Nothing
    }