module TD.Data.Community
  (Community(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.Text as T
import qualified TD.Data.ChatPhotoInfo as ChatPhotoInfo
import qualified TD.Data.CommunityMemberStatus as CommunityMemberStatus
import qualified TD.Data.CommunityPermissions as CommunityPermissions

data Community
  = Community -- ^ Represents a community consisting of supergroup chats, channel chats and chats with bots
    { Community -> Maybe Int
_id                 :: Maybe Int                                         -- ^ Community identifier
    , Community -> Maybe Bool
have_access         :: Maybe Bool                                        -- ^ If false, the community is inaccessible, and the only information known about the community is inside this class. The identifier of the community can't be passed to any method
    , Community -> Maybe Text
name                :: Maybe T.Text                                      -- ^ Community name
    , Community -> Maybe ChatPhotoInfo
photo               :: Maybe ChatPhotoInfo.ChatPhotoInfo                 -- ^ Community photo; may be null
    , Community -> Maybe Int
date                :: Maybe Int                                         -- ^ Point in time (Unix timestamp) when the community was joined, or the point in time when the community was created, in case the user is not a member of any chat in the community
    , Community -> Maybe CommunityMemberStatus
status              :: Maybe CommunityMemberStatus.CommunityMemberStatus -- ^ Status of the current user in the community
    , Community -> Maybe CommunityPermissions
permissions         :: Maybe CommunityPermissions.CommunityPermissions   -- ^ Actions that non-administrator community members are allowed to take in the community
    , Community -> Maybe Bool
are_chats_collapsed :: Maybe Bool                                        -- ^ True, if all chats of the community are collapsed into one entry in the chat list
    }
  deriving (Community -> Community -> Bool
(Community -> Community -> Bool)
-> (Community -> Community -> Bool) -> Eq Community
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Community -> Community -> Bool
== :: Community -> Community -> Bool
$c/= :: Community -> Community -> Bool
/= :: Community -> Community -> Bool
Eq, Int -> Community -> ShowS
[Community] -> ShowS
Community -> String
(Int -> Community -> ShowS)
-> (Community -> String)
-> ([Community] -> ShowS)
-> Show Community
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Community -> ShowS
showsPrec :: Int -> Community -> ShowS
$cshow :: Community -> String
show :: Community -> String
$cshowList :: [Community] -> ShowS
showList :: [Community] -> ShowS
Show)

instance I.ShortShow Community where
  shortShow :: Community -> String
shortShow Community
    { _id :: Community -> Maybe Int
_id                 = Maybe Int
_id_
    , have_access :: Community -> Maybe Bool
have_access         = Maybe Bool
have_access_
    , name :: Community -> Maybe Text
name                = Maybe Text
name_
    , photo :: Community -> Maybe ChatPhotoInfo
photo               = Maybe ChatPhotoInfo
photo_
    , date :: Community -> Maybe Int
date                = Maybe Int
date_
    , status :: Community -> Maybe CommunityMemberStatus
status              = Maybe CommunityMemberStatus
status_
    , permissions :: Community -> Maybe CommunityPermissions
permissions         = Maybe CommunityPermissions
permissions_
    , are_chats_collapsed :: Community -> Maybe Bool
are_chats_collapsed = Maybe Bool
are_chats_collapsed_
    }
      = String
"Community"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"                 String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_id_
        , String
"have_access"         String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
have_access_
        , String
"name"                String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
name_
        , String
"photo"               String -> Maybe ChatPhotoInfo -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ChatPhotoInfo
photo_
        , String
"date"                String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
        , String
"status"              String -> Maybe CommunityMemberStatus -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe CommunityMemberStatus
status_
        , String
"permissions"         String -> Maybe CommunityPermissions -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe CommunityPermissions
permissions_
        , String
"are_chats_collapsed" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
are_chats_collapsed_
        ]

instance AT.FromJSON Community where
  parseJSON :: Value -> Parser Community
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
"community" -> Value -> Parser Community
parseCommunity Value
v
      String
_           -> Parser Community
forall a. Monoid a => a
mempty
    
    where
      parseCommunity :: A.Value -> AT.Parser Community
      parseCommunity :: Value -> Parser Community
parseCommunity = String -> (Object -> Parser Community) -> Value -> Parser Community
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Community" ((Object -> Parser Community) -> Value -> Parser Community)
-> (Object -> Parser Community) -> Value -> Parser Community
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_                 <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe Bool
have_access_         <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"have_access"
        Maybe Text
name_                <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"name"
        Maybe ChatPhotoInfo
photo_               <- Object
o Object -> Key -> Parser (Maybe ChatPhotoInfo)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"photo"
        Maybe Int
date_                <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"date"
        Maybe CommunityMemberStatus
status_              <- Object
o Object -> Key -> Parser (Maybe CommunityMemberStatus)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"status"
        Maybe CommunityPermissions
permissions_         <- Object
o Object -> Key -> Parser (Maybe CommunityPermissions)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"permissions"
        Maybe Bool
are_chats_collapsed_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"are_chats_collapsed"
        Community -> Parser Community
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Community -> Parser Community) -> Community -> Parser Community
forall a b. (a -> b) -> a -> b
$ Community
          { _id :: Maybe Int
_id                 = Maybe Int
_id_
          , have_access :: Maybe Bool
have_access         = Maybe Bool
have_access_
          , name :: Maybe Text
name                = Maybe Text
name_
          , photo :: Maybe ChatPhotoInfo
photo               = Maybe ChatPhotoInfo
photo_
          , date :: Maybe Int
date                = Maybe Int
date_
          , status :: Maybe CommunityMemberStatus
status              = Maybe CommunityMemberStatus
status_
          , permissions :: Maybe CommunityPermissions
permissions         = Maybe CommunityPermissions
permissions_
          , are_chats_collapsed :: Maybe Bool
are_chats_collapsed = Maybe Bool
are_chats_collapsed_
          }
  parseJSON Value
_ = Parser Community
forall a. Monoid a => a
mempty