module TD.Data.GiftSettings
  ( GiftSettings(..)    
  , defaultGiftSettings 
  ) 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.AcceptedGiftTypes as AcceptedGiftTypes

data GiftSettings
  = GiftSettings -- ^ Contains settings for gift receiving for a user
    { GiftSettings -> Maybe Bool
show_gift_button    :: Maybe Bool                                -- ^ True, if a button for sending a gift to the user or by the user must always be shown in the input field
    , GiftSettings -> Maybe AcceptedGiftTypes
accepted_gift_types :: Maybe AcceptedGiftTypes.AcceptedGiftTypes -- ^ Types of gifts accepted by the user; for Telegram Premium users only
    }
  deriving (GiftSettings -> GiftSettings -> Bool
(GiftSettings -> GiftSettings -> Bool)
-> (GiftSettings -> GiftSettings -> Bool) -> Eq GiftSettings
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GiftSettings -> GiftSettings -> Bool
== :: GiftSettings -> GiftSettings -> Bool
$c/= :: GiftSettings -> GiftSettings -> Bool
/= :: GiftSettings -> GiftSettings -> Bool
Eq, Int -> GiftSettings -> ShowS
[GiftSettings] -> ShowS
GiftSettings -> String
(Int -> GiftSettings -> ShowS)
-> (GiftSettings -> String)
-> ([GiftSettings] -> ShowS)
-> Show GiftSettings
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GiftSettings -> ShowS
showsPrec :: Int -> GiftSettings -> ShowS
$cshow :: GiftSettings -> String
show :: GiftSettings -> String
$cshowList :: [GiftSettings] -> ShowS
showList :: [GiftSettings] -> ShowS
Show)

instance I.ShortShow GiftSettings where
  shortShow :: GiftSettings -> String
shortShow GiftSettings
    { show_gift_button :: GiftSettings -> Maybe Bool
show_gift_button    = Maybe Bool
show_gift_button_
    , accepted_gift_types :: GiftSettings -> Maybe AcceptedGiftTypes
accepted_gift_types = Maybe AcceptedGiftTypes
accepted_gift_types_
    }
      = String
"GiftSettings"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"show_gift_button"    String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
show_gift_button_
        , String
"accepted_gift_types" String -> Maybe AcceptedGiftTypes -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe AcceptedGiftTypes
accepted_gift_types_
        ]

instance AT.FromJSON GiftSettings where
  parseJSON :: Value -> Parser GiftSettings
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
"giftSettings" -> Value -> Parser GiftSettings
parseGiftSettings Value
v
      String
_              -> Parser GiftSettings
forall a. Monoid a => a
mempty
    
    where
      parseGiftSettings :: A.Value -> AT.Parser GiftSettings
      parseGiftSettings :: Value -> Parser GiftSettings
parseGiftSettings = String
-> (Object -> Parser GiftSettings) -> Value -> Parser GiftSettings
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"GiftSettings" ((Object -> Parser GiftSettings) -> Value -> Parser GiftSettings)
-> (Object -> Parser GiftSettings) -> Value -> Parser GiftSettings
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
show_gift_button_    <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"show_gift_button"
        Maybe AcceptedGiftTypes
accepted_gift_types_ <- Object
o Object -> Key -> Parser (Maybe AcceptedGiftTypes)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"accepted_gift_types"
        GiftSettings -> Parser GiftSettings
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GiftSettings -> Parser GiftSettings)
-> GiftSettings -> Parser GiftSettings
forall a b. (a -> b) -> a -> b
$ GiftSettings
          { show_gift_button :: Maybe Bool
show_gift_button    = Maybe Bool
show_gift_button_
          , accepted_gift_types :: Maybe AcceptedGiftTypes
accepted_gift_types = Maybe AcceptedGiftTypes
accepted_gift_types_
          }
  parseJSON Value
_ = Parser GiftSettings
forall a. Monoid a => a
mempty

instance AT.ToJSON GiftSettings where
  toJSON :: GiftSettings -> Value
toJSON GiftSettings
    { show_gift_button :: GiftSettings -> Maybe Bool
show_gift_button    = Maybe Bool
show_gift_button_
    , accepted_gift_types :: GiftSettings -> Maybe AcceptedGiftTypes
accepted_gift_types = Maybe AcceptedGiftTypes
accepted_gift_types_
    }
      = [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
"giftSettings"
        , Key
"show_gift_button"    Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
show_gift_button_
        , Key
"accepted_gift_types" Key -> Maybe AcceptedGiftTypes -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe AcceptedGiftTypes
accepted_gift_types_
        ]

defaultGiftSettings :: GiftSettings
defaultGiftSettings :: GiftSettings
defaultGiftSettings =
  GiftSettings
    { show_gift_button :: Maybe Bool
show_gift_button    = Maybe Bool
forall a. Maybe a
Nothing
    , accepted_gift_types :: Maybe AcceptedGiftTypes
accepted_gift_types = Maybe AcceptedGiftTypes
forall a. Maybe a
Nothing
    }