module TD.Data.ChatType
  (ChatType(..)) where

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

-- | Describes the type of chat
data ChatType
  = ChatTypePrivate -- ^ An ordinary chat with a user
    { ChatType -> Maybe Int
user_id :: Maybe Int -- ^ User identifier
    }
  | ChatTypeBasicGroup -- ^ A basic group (a chat with 0-200 other users)
    { ChatType -> Maybe Int
basic_group_id :: Maybe Int -- ^ Basic group identifier
    }
  | ChatTypeSupergroup -- ^ A supergroup or channel (with unlimited members)
    { ChatType -> Maybe Int
supergroup_id :: Maybe Int  -- ^ Supergroup or channel identifier
    , ChatType -> Maybe Bool
is_channel    :: Maybe Bool -- ^ True, if the supergroup is a channel
    }
  | ChatTypeSecret -- ^ A secret chat with a user
    { ChatType -> Maybe Int
secret_chat_id :: Maybe Int -- ^ Secret chat identifier
    , user_id        :: Maybe Int -- ^ User identifier of the other user in the secret chat
    }
  deriving (ChatType -> ChatType -> Bool
(ChatType -> ChatType -> Bool)
-> (ChatType -> ChatType -> Bool) -> Eq ChatType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChatType -> ChatType -> Bool
== :: ChatType -> ChatType -> Bool
$c/= :: ChatType -> ChatType -> Bool
/= :: ChatType -> ChatType -> Bool
Eq, Int -> ChatType -> ShowS
[ChatType] -> ShowS
ChatType -> String
(Int -> ChatType -> ShowS)
-> (ChatType -> String) -> ([ChatType] -> ShowS) -> Show ChatType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChatType -> ShowS
showsPrec :: Int -> ChatType -> ShowS
$cshow :: ChatType -> String
show :: ChatType -> String
$cshowList :: [ChatType] -> ShowS
showList :: [ChatType] -> ShowS
Show)

instance I.ShortShow ChatType where
  shortShow :: ChatType -> String
shortShow ChatTypePrivate
    { user_id :: ChatType -> Maybe Int
user_id = Maybe Int
user_id_
    }
      = String
"ChatTypePrivate"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"user_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
user_id_
        ]
  shortShow ChatTypeBasicGroup
    { basic_group_id :: ChatType -> Maybe Int
basic_group_id = Maybe Int
basic_group_id_
    }
      = String
"ChatTypeBasicGroup"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"basic_group_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
basic_group_id_
        ]
  shortShow ChatTypeSupergroup
    { supergroup_id :: ChatType -> Maybe Int
supergroup_id = Maybe Int
supergroup_id_
    , is_channel :: ChatType -> Maybe Bool
is_channel    = Maybe Bool
is_channel_
    }
      = String
"ChatTypeSupergroup"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"supergroup_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
supergroup_id_
        , String
"is_channel"    String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_channel_
        ]
  shortShow ChatTypeSecret
    { secret_chat_id :: ChatType -> Maybe Int
secret_chat_id = Maybe Int
secret_chat_id_
    , user_id :: ChatType -> Maybe Int
user_id        = Maybe Int
user_id_
    }
      = String
"ChatTypeSecret"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"secret_chat_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
secret_chat_id_
        , String
"user_id"        String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
user_id_
        ]

instance AT.FromJSON ChatType where
  parseJSON :: Value -> Parser ChatType
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
"chatTypePrivate"    -> Value -> Parser ChatType
parseChatTypePrivate Value
v
      String
"chatTypeBasicGroup" -> Value -> Parser ChatType
parseChatTypeBasicGroup Value
v
      String
"chatTypeSupergroup" -> Value -> Parser ChatType
parseChatTypeSupergroup Value
v
      String
"chatTypeSecret"     -> Value -> Parser ChatType
parseChatTypeSecret Value
v
      String
_                    -> Parser ChatType
forall a. Monoid a => a
mempty
    
    where
      parseChatTypePrivate :: A.Value -> AT.Parser ChatType
      parseChatTypePrivate :: Value -> Parser ChatType
parseChatTypePrivate = String -> (Object -> Parser ChatType) -> Value -> Parser ChatType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatTypePrivate" ((Object -> Parser ChatType) -> Value -> Parser ChatType)
-> (Object -> Parser ChatType) -> Value -> Parser ChatType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
user_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"user_id"
        ChatType -> Parser ChatType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatType -> Parser ChatType) -> ChatType -> Parser ChatType
forall a b. (a -> b) -> a -> b
$ ChatTypePrivate
          { user_id :: Maybe Int
user_id = Maybe Int
user_id_
          }
      parseChatTypeBasicGroup :: A.Value -> AT.Parser ChatType
      parseChatTypeBasicGroup :: Value -> Parser ChatType
parseChatTypeBasicGroup = String -> (Object -> Parser ChatType) -> Value -> Parser ChatType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatTypeBasicGroup" ((Object -> Parser ChatType) -> Value -> Parser ChatType)
-> (Object -> Parser ChatType) -> Value -> Parser ChatType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
basic_group_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"basic_group_id"
        ChatType -> Parser ChatType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatType -> Parser ChatType) -> ChatType -> Parser ChatType
forall a b. (a -> b) -> a -> b
$ ChatTypeBasicGroup
          { basic_group_id :: Maybe Int
basic_group_id = Maybe Int
basic_group_id_
          }
      parseChatTypeSupergroup :: A.Value -> AT.Parser ChatType
      parseChatTypeSupergroup :: Value -> Parser ChatType
parseChatTypeSupergroup = String -> (Object -> Parser ChatType) -> Value -> Parser ChatType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatTypeSupergroup" ((Object -> Parser ChatType) -> Value -> Parser ChatType)
-> (Object -> Parser ChatType) -> Value -> Parser ChatType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
supergroup_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"supergroup_id"
        Maybe Bool
is_channel_    <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_channel"
        ChatType -> Parser ChatType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatType -> Parser ChatType) -> ChatType -> Parser ChatType
forall a b. (a -> b) -> a -> b
$ ChatTypeSupergroup
          { supergroup_id :: Maybe Int
supergroup_id = Maybe Int
supergroup_id_
          , is_channel :: Maybe Bool
is_channel    = Maybe Bool
is_channel_
          }
      parseChatTypeSecret :: A.Value -> AT.Parser ChatType
      parseChatTypeSecret :: Value -> Parser ChatType
parseChatTypeSecret = String -> (Object -> Parser ChatType) -> Value -> Parser ChatType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatTypeSecret" ((Object -> Parser ChatType) -> Value -> Parser ChatType)
-> (Object -> Parser ChatType) -> Value -> Parser ChatType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
secret_chat_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"secret_chat_id"
        Maybe Int
user_id_        <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"user_id"
        ChatType -> Parser ChatType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatType -> Parser ChatType) -> ChatType -> Parser ChatType
forall a b. (a -> b) -> a -> b
$ ChatTypeSecret
          { secret_chat_id :: Maybe Int
secret_chat_id = Maybe Int
secret_chat_id_
          , user_id :: Maybe Int
user_id        = Maybe Int
user_id_
          }
  parseJSON Value
_ = Parser ChatType
forall a. Monoid a => a
mempty