module TD.Data.Sessions
  (Sessions(..)) 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.Session as Session

data Sessions
  = Sessions -- ^ Contains a list of sessions
    { Sessions -> Maybe [Session]
sessions                  :: Maybe [Session.Session] -- ^ List of sessions
    , Sessions -> Maybe Int
inactive_session_ttl_days :: Maybe Int               -- ^ Number of days of inactivity before sessions will automatically be terminated; 1-366 days
    }
  deriving (Sessions -> Sessions -> Bool
(Sessions -> Sessions -> Bool)
-> (Sessions -> Sessions -> Bool) -> Eq Sessions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Sessions -> Sessions -> Bool
== :: Sessions -> Sessions -> Bool
$c/= :: Sessions -> Sessions -> Bool
/= :: Sessions -> Sessions -> Bool
Eq, Int -> Sessions -> ShowS
[Sessions] -> ShowS
Sessions -> String
(Int -> Sessions -> ShowS)
-> (Sessions -> String) -> ([Sessions] -> ShowS) -> Show Sessions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Sessions -> ShowS
showsPrec :: Int -> Sessions -> ShowS
$cshow :: Sessions -> String
show :: Sessions -> String
$cshowList :: [Sessions] -> ShowS
showList :: [Sessions] -> ShowS
Show)

instance I.ShortShow Sessions where
  shortShow :: Sessions -> String
shortShow Sessions
    { sessions :: Sessions -> Maybe [Session]
sessions                  = Maybe [Session]
sessions_
    , inactive_session_ttl_days :: Sessions -> Maybe Int
inactive_session_ttl_days = Maybe Int
inactive_session_ttl_days_
    }
      = String
"Sessions"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"sessions"                  String -> Maybe [Session] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Session]
sessions_
        , String
"inactive_session_ttl_days" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
inactive_session_ttl_days_
        ]

instance AT.FromJSON Sessions where
  parseJSON :: Value -> Parser Sessions
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
"sessions" -> Value -> Parser Sessions
parseSessions Value
v
      String
_          -> Parser Sessions
forall a. Monoid a => a
mempty
    
    where
      parseSessions :: A.Value -> AT.Parser Sessions
      parseSessions :: Value -> Parser Sessions
parseSessions = String -> (Object -> Parser Sessions) -> Value -> Parser Sessions
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Sessions" ((Object -> Parser Sessions) -> Value -> Parser Sessions)
-> (Object -> Parser Sessions) -> Value -> Parser Sessions
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [Session]
sessions_                  <- Object
o Object -> Key -> Parser (Maybe [Session])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sessions"
        Maybe Int
inactive_session_ttl_days_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"inactive_session_ttl_days"
        Sessions -> Parser Sessions
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Sessions -> Parser Sessions) -> Sessions -> Parser Sessions
forall a b. (a -> b) -> a -> b
$ Sessions
          { sessions :: Maybe [Session]
sessions                  = Maybe [Session]
sessions_
          , inactive_session_ttl_days :: Maybe Int
inactive_session_ttl_days = Maybe Int
inactive_session_ttl_days_
          }
  parseJSON Value
_ = Parser Sessions
forall a. Monoid a => a
mempty