module TD.Data.SessionType
  (SessionType(..)) where

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

-- | Describes type of user session
data SessionType
  = SessionTypeDevice -- ^ A regular session from a device
    { SessionType -> Maybe Int
session_id :: Maybe Int -- ^ Unique identifier of the session. Use terminateSession to terminate it or confirmSession to confirm it if it isn't confirmed yet
    }
  | SessionTypeConnectedBot -- ^ A business bot connected to the current user's account
    { SessionType -> Maybe Int
bot_user_id :: Maybe Int -- ^ User identifier of the bot. Use deleteBusinessConnectedBot to remove it or confirmBusinessConnectedBot to confirm it if it isn't confirmed yet
    }
  deriving (SessionType -> SessionType -> Bool
(SessionType -> SessionType -> Bool)
-> (SessionType -> SessionType -> Bool) -> Eq SessionType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SessionType -> SessionType -> Bool
== :: SessionType -> SessionType -> Bool
$c/= :: SessionType -> SessionType -> Bool
/= :: SessionType -> SessionType -> Bool
Eq, Int -> SessionType -> ShowS
[SessionType] -> ShowS
SessionType -> String
(Int -> SessionType -> ShowS)
-> (SessionType -> String)
-> ([SessionType] -> ShowS)
-> Show SessionType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SessionType -> ShowS
showsPrec :: Int -> SessionType -> ShowS
$cshow :: SessionType -> String
show :: SessionType -> String
$cshowList :: [SessionType] -> ShowS
showList :: [SessionType] -> ShowS
Show)

instance I.ShortShow SessionType where
  shortShow :: SessionType -> String
shortShow SessionTypeDevice
    { session_id :: SessionType -> Maybe Int
session_id = Maybe Int
session_id_
    }
      = String
"SessionTypeDevice"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"session_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
session_id_
        ]
  shortShow SessionTypeConnectedBot
    { bot_user_id :: SessionType -> Maybe Int
bot_user_id = Maybe Int
bot_user_id_
    }
      = String
"SessionTypeConnectedBot"
        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_
        ]

instance AT.FromJSON SessionType where
  parseJSON :: Value -> Parser SessionType
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
"sessionTypeDevice"       -> Value -> Parser SessionType
parseSessionTypeDevice Value
v
      String
"sessionTypeConnectedBot" -> Value -> Parser SessionType
parseSessionTypeConnectedBot Value
v
      String
_                         -> Parser SessionType
forall a. Monoid a => a
mempty
    
    where
      parseSessionTypeDevice :: A.Value -> AT.Parser SessionType
      parseSessionTypeDevice :: Value -> Parser SessionType
parseSessionTypeDevice = String
-> (Object -> Parser SessionType) -> Value -> Parser SessionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"SessionTypeDevice" ((Object -> Parser SessionType) -> Value -> Parser SessionType)
-> (Object -> Parser SessionType) -> Value -> Parser SessionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
session_id_ <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"session_id"
        SessionType -> Parser SessionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SessionType -> Parser SessionType)
-> SessionType -> Parser SessionType
forall a b. (a -> b) -> a -> b
$ SessionTypeDevice
          { session_id :: Maybe Int
session_id = Maybe Int
session_id_
          }
      parseSessionTypeConnectedBot :: A.Value -> AT.Parser SessionType
      parseSessionTypeConnectedBot :: Value -> Parser SessionType
parseSessionTypeConnectedBot = String
-> (Object -> Parser SessionType) -> Value -> Parser SessionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"SessionTypeConnectedBot" ((Object -> Parser SessionType) -> Value -> Parser SessionType)
-> (Object -> Parser SessionType) -> Value -> Parser SessionType
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"
        SessionType -> Parser SessionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SessionType -> Parser SessionType)
-> SessionType -> Parser SessionType
forall a b. (a -> b) -> a -> b
$ SessionTypeConnectedBot
          { bot_user_id :: Maybe Int
bot_user_id = Maybe Int
bot_user_id_
          }
  parseJSON Value
_ = Parser SessionType
forall a. Monoid a => a
mempty