module TD.Data.AccountTtl
  ( AccountTtl(..)    
  , defaultAccountTtl 
  ) where

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

data AccountTtl
  = AccountTtl -- ^ Contains information about the period of inactivity after which the current user's account will automatically be deleted
    { AccountTtl -> Maybe Int
days :: Maybe Int -- ^ Number of days of inactivity before the account will be flagged for deletion; 30-730 days
    }
  deriving (AccountTtl -> AccountTtl -> Bool
(AccountTtl -> AccountTtl -> Bool)
-> (AccountTtl -> AccountTtl -> Bool) -> Eq AccountTtl
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AccountTtl -> AccountTtl -> Bool
== :: AccountTtl -> AccountTtl -> Bool
$c/= :: AccountTtl -> AccountTtl -> Bool
/= :: AccountTtl -> AccountTtl -> Bool
Eq, Int -> AccountTtl -> ShowS
[AccountTtl] -> ShowS
AccountTtl -> String
(Int -> AccountTtl -> ShowS)
-> (AccountTtl -> String)
-> ([AccountTtl] -> ShowS)
-> Show AccountTtl
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AccountTtl -> ShowS
showsPrec :: Int -> AccountTtl -> ShowS
$cshow :: AccountTtl -> String
show :: AccountTtl -> String
$cshowList :: [AccountTtl] -> ShowS
showList :: [AccountTtl] -> ShowS
Show)

instance I.ShortShow AccountTtl where
  shortShow :: AccountTtl -> String
shortShow AccountTtl
    { days :: AccountTtl -> Maybe Int
days = Maybe Int
days_
    }
      = String
"AccountTtl"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"days" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
days_
        ]

instance AT.FromJSON AccountTtl where
  parseJSON :: Value -> Parser AccountTtl
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
"accountTtl" -> Value -> Parser AccountTtl
parseAccountTtl Value
v
      String
_            -> Parser AccountTtl
forall a. Monoid a => a
mempty
    
    where
      parseAccountTtl :: A.Value -> AT.Parser AccountTtl
      parseAccountTtl :: Value -> Parser AccountTtl
parseAccountTtl = String
-> (Object -> Parser AccountTtl) -> Value -> Parser AccountTtl
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"AccountTtl" ((Object -> Parser AccountTtl) -> Value -> Parser AccountTtl)
-> (Object -> Parser AccountTtl) -> Value -> Parser AccountTtl
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
days_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"days"
        AccountTtl -> Parser AccountTtl
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AccountTtl -> Parser AccountTtl)
-> AccountTtl -> Parser AccountTtl
forall a b. (a -> b) -> a -> b
$ AccountTtl
          { days :: Maybe Int
days = Maybe Int
days_
          }
  parseJSON Value
_ = Parser AccountTtl
forall a. Monoid a => a
mempty

instance AT.ToJSON AccountTtl where
  toJSON :: AccountTtl -> Value
toJSON AccountTtl
    { days :: AccountTtl -> Maybe Int
days = Maybe Int
days_
    }
      = [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
"accountTtl"
        , Key
"days"  Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
days_
        ]

defaultAccountTtl :: AccountTtl
defaultAccountTtl :: AccountTtl
defaultAccountTtl =
  AccountTtl
    { days :: Maybe Int
days = Maybe Int
forall a. Maybe a
Nothing
    }