module TD.Data.BotCommands
  (BotCommands(..)) 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.BotCommand as BotCommand

data BotCommands
  = BotCommands -- ^ Contains a list of bot commands
    { BotCommands -> Maybe Int
bot_user_id :: Maybe Int                     -- ^ Bot's user identifier
    , BotCommands -> Maybe [BotCommand]
commands    :: Maybe [BotCommand.BotCommand] -- ^ List of bot commands
    }
  deriving (BotCommands -> BotCommands -> Bool
(BotCommands -> BotCommands -> Bool)
-> (BotCommands -> BotCommands -> Bool) -> Eq BotCommands
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BotCommands -> BotCommands -> Bool
== :: BotCommands -> BotCommands -> Bool
$c/= :: BotCommands -> BotCommands -> Bool
/= :: BotCommands -> BotCommands -> Bool
Eq, Int -> BotCommands -> ShowS
[BotCommands] -> ShowS
BotCommands -> String
(Int -> BotCommands -> ShowS)
-> (BotCommands -> String)
-> ([BotCommands] -> ShowS)
-> Show BotCommands
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BotCommands -> ShowS
showsPrec :: Int -> BotCommands -> ShowS
$cshow :: BotCommands -> String
show :: BotCommands -> String
$cshowList :: [BotCommands] -> ShowS
showList :: [BotCommands] -> ShowS
Show)

instance I.ShortShow BotCommands where
  shortShow :: BotCommands -> String
shortShow BotCommands
    { bot_user_id :: BotCommands -> Maybe Int
bot_user_id = Maybe Int
bot_user_id_
    , commands :: BotCommands -> Maybe [BotCommand]
commands    = Maybe [BotCommand]
commands_
    }
      = String
"BotCommands"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"bot_user_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
bot_user_id_
        , String
"commands"    String -> Maybe [BotCommand] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [BotCommand]
commands_
        ]

instance AT.FromJSON BotCommands where
  parseJSON :: Value -> Parser BotCommands
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
"botCommands" -> Value -> Parser BotCommands
parseBotCommands Value
v
      String
_             -> Parser BotCommands
forall a. Monoid a => a
mempty
    
    where
      parseBotCommands :: A.Value -> AT.Parser BotCommands
      parseBotCommands :: Value -> Parser BotCommands
parseBotCommands = String
-> (Object -> Parser BotCommands) -> Value -> Parser BotCommands
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BotCommands" ((Object -> Parser BotCommands) -> Value -> Parser BotCommands)
-> (Object -> Parser BotCommands) -> Value -> Parser BotCommands
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
bot_user_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"bot_user_id"
        Maybe [BotCommand]
commands_    <- Object
o Object -> Key -> Parser (Maybe [BotCommand])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"commands"
        BotCommands -> Parser BotCommands
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BotCommands -> Parser BotCommands)
-> BotCommands -> Parser BotCommands
forall a b. (a -> b) -> a -> b
$ BotCommands
          { bot_user_id :: Maybe Int
bot_user_id = Maybe Int
bot_user_id_
          , commands :: Maybe [BotCommand]
commands    = Maybe [BotCommand]
commands_
          }
  parseJSON Value
_ = Parser BotCommands
forall a. Monoid a => a
mempty