module TD.Data.TermsOfService
  (TermsOfService(..)) 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.FormattedText as FormattedText

data TermsOfService
  = TermsOfService -- ^ Contains Telegram terms of service
    { TermsOfService -> Maybe FormattedText
text         :: Maybe FormattedText.FormattedText -- ^ Text of the terms of service
    , TermsOfService -> Maybe Int
min_user_age :: Maybe Int                         -- ^ The minimum age of a user to be able to accept the terms; 0 if age isn't restricted
    , TermsOfService -> Maybe Bool
show_popup   :: Maybe Bool                        -- ^ True, if a blocking popup with terms of service must be shown to the user
    }
  deriving (TermsOfService -> TermsOfService -> Bool
(TermsOfService -> TermsOfService -> Bool)
-> (TermsOfService -> TermsOfService -> Bool) -> Eq TermsOfService
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TermsOfService -> TermsOfService -> Bool
== :: TermsOfService -> TermsOfService -> Bool
$c/= :: TermsOfService -> TermsOfService -> Bool
/= :: TermsOfService -> TermsOfService -> Bool
Eq, Int -> TermsOfService -> ShowS
[TermsOfService] -> ShowS
TermsOfService -> String
(Int -> TermsOfService -> ShowS)
-> (TermsOfService -> String)
-> ([TermsOfService] -> ShowS)
-> Show TermsOfService
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TermsOfService -> ShowS
showsPrec :: Int -> TermsOfService -> ShowS
$cshow :: TermsOfService -> String
show :: TermsOfService -> String
$cshowList :: [TermsOfService] -> ShowS
showList :: [TermsOfService] -> ShowS
Show)

instance I.ShortShow TermsOfService where
  shortShow :: TermsOfService -> String
shortShow TermsOfService
    { text :: TermsOfService -> Maybe FormattedText
text         = Maybe FormattedText
text_
    , min_user_age :: TermsOfService -> Maybe Int
min_user_age = Maybe Int
min_user_age_
    , show_popup :: TermsOfService -> Maybe Bool
show_popup   = Maybe Bool
show_popup_
    }
      = String
"TermsOfService"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"text"         String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
text_
        , String
"min_user_age" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
min_user_age_
        , String
"show_popup"   String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
show_popup_
        ]

instance AT.FromJSON TermsOfService where
  parseJSON :: Value -> Parser TermsOfService
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
"termsOfService" -> Value -> Parser TermsOfService
parseTermsOfService Value
v
      String
_                -> Parser TermsOfService
forall a. Monoid a => a
mempty
    
    where
      parseTermsOfService :: A.Value -> AT.Parser TermsOfService
      parseTermsOfService :: Value -> Parser TermsOfService
parseTermsOfService = String
-> (Object -> Parser TermsOfService)
-> Value
-> Parser TermsOfService
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TermsOfService" ((Object -> Parser TermsOfService)
 -> Value -> Parser TermsOfService)
-> (Object -> Parser TermsOfService)
-> Value
-> Parser TermsOfService
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe FormattedText
text_         <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"text"
        Maybe Int
min_user_age_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"min_user_age"
        Maybe Bool
show_popup_   <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"show_popup"
        TermsOfService -> Parser TermsOfService
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TermsOfService -> Parser TermsOfService)
-> TermsOfService -> Parser TermsOfService
forall a b. (a -> b) -> a -> b
$ TermsOfService
          { text :: Maybe FormattedText
text         = Maybe FormattedText
text_
          , min_user_age :: Maybe Int
min_user_age = Maybe Int
min_user_age_
          , show_popup :: Maybe Bool
show_popup   = Maybe Bool
show_popup_
          }
  parseJSON Value
_ = Parser TermsOfService
forall a. Monoid a => a
mempty