{-# LANGUAGE TemplateHaskell #-}

-- | A Group Group channel
module Calamity.Types.Model.Channel.Group (GroupChannel (..)) where

import {-# SOURCE #-} Calamity.Types.Model.Channel
import {-# SOURCE #-} Calamity.Types.Model.Channel.Message
import Calamity.Types.Model.User
import Calamity.Types.Snowflake
import Data.Aeson ((.:), (.:?))
import Data.Aeson qualified as Aeson
import Data.Text (Text)
import Data.Time
import Optics.TH
import TextShow qualified

data GroupChannel = GroupChannel
  { GroupChannel -> Snowflake GroupChannel
id :: Snowflake GroupChannel
  , GroupChannel -> Snowflake User
ownerID :: Snowflake User
  , GroupChannel -> Maybe (Snowflake Message)
lastMessageID :: Maybe (Snowflake Message)
  , GroupChannel -> Maybe UTCTime
lastPinTimestamp :: Maybe UTCTime
  , GroupChannel -> Maybe Text
icon :: Maybe Text
  , GroupChannel -> [User]
recipients :: [User]
  , GroupChannel -> Text
name :: Text
  }
  deriving (Int -> GroupChannel -> ShowS
[GroupChannel] -> ShowS
GroupChannel -> String
(Int -> GroupChannel -> ShowS)
-> (GroupChannel -> String)
-> ([GroupChannel] -> ShowS)
-> Show GroupChannel
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GroupChannel -> ShowS
showsPrec :: Int -> GroupChannel -> ShowS
$cshow :: GroupChannel -> String
show :: GroupChannel -> String
$cshowList :: [GroupChannel] -> ShowS
showList :: [GroupChannel] -> ShowS
Show, GroupChannel -> GroupChannel -> Bool
(GroupChannel -> GroupChannel -> Bool)
-> (GroupChannel -> GroupChannel -> Bool) -> Eq GroupChannel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GroupChannel -> GroupChannel -> Bool
== :: GroupChannel -> GroupChannel -> Bool
$c/= :: GroupChannel -> GroupChannel -> Bool
/= :: GroupChannel -> GroupChannel -> Bool
Eq)
  deriving (Int -> GroupChannel -> Text
Int -> GroupChannel -> Builder
Int -> GroupChannel -> Text
[GroupChannel] -> Text
[GroupChannel] -> Builder
[GroupChannel] -> Text
GroupChannel -> Text
GroupChannel -> Builder
GroupChannel -> Text
(Int -> GroupChannel -> Builder)
-> (GroupChannel -> Builder)
-> ([GroupChannel] -> Builder)
-> (Int -> GroupChannel -> Text)
-> (GroupChannel -> Text)
-> ([GroupChannel] -> Text)
-> (Int -> GroupChannel -> Text)
-> (GroupChannel -> Text)
-> ([GroupChannel] -> Text)
-> TextShow GroupChannel
forall a.
(Int -> a -> Builder)
-> (a -> Builder)
-> ([a] -> Builder)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> TextShow a
$cshowbPrec :: Int -> GroupChannel -> Builder
showbPrec :: Int -> GroupChannel -> Builder
$cshowb :: GroupChannel -> Builder
showb :: GroupChannel -> Builder
$cshowbList :: [GroupChannel] -> Builder
showbList :: [GroupChannel] -> Builder
$cshowtPrec :: Int -> GroupChannel -> Text
showtPrec :: Int -> GroupChannel -> Text
$cshowt :: GroupChannel -> Text
showt :: GroupChannel -> Text
$cshowtList :: [GroupChannel] -> Text
showtList :: [GroupChannel] -> Text
$cshowtlPrec :: Int -> GroupChannel -> Text
showtlPrec :: Int -> GroupChannel -> Text
$cshowtl :: GroupChannel -> Text
showtl :: GroupChannel -> Text
$cshowtlList :: [GroupChannel] -> Text
showtlList :: [GroupChannel] -> Text
TextShow.TextShow) via TextShow.FromStringShow GroupChannel
  deriving (HasID GroupChannel) via HasIDField "id" GroupChannel
  deriving (HasID Channel) via HasIDFieldCoerce' "id" GroupChannel
  deriving (HasID User) via HasIDField "ownerID" GroupChannel

$(makeFieldLabelsNoPrefix ''GroupChannel)

instance Aeson.FromJSON GroupChannel where
  parseJSON :: Value -> Parser GroupChannel
parseJSON = String
-> (Object -> Parser GroupChannel) -> Value -> Parser GroupChannel
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"GroupChannel" ((Object -> Parser GroupChannel) -> Value -> Parser GroupChannel)
-> (Object -> Parser GroupChannel) -> Value -> Parser GroupChannel
forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Snowflake GroupChannel
-> Snowflake User
-> Maybe (Snowflake Message)
-> Maybe UTCTime
-> Maybe Text
-> [User]
-> Text
-> GroupChannel
GroupChannel
      (Snowflake GroupChannel
 -> Snowflake User
 -> Maybe (Snowflake Message)
 -> Maybe UTCTime
 -> Maybe Text
 -> [User]
 -> Text
 -> GroupChannel)
-> Parser (Snowflake GroupChannel)
-> Parser
     (Snowflake User
      -> Maybe (Snowflake Message)
      -> Maybe UTCTime
      -> Maybe Text
      -> [User]
      -> Text
      -> GroupChannel)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser (Snowflake GroupChannel)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
      Parser
  (Snowflake User
   -> Maybe (Snowflake Message)
   -> Maybe UTCTime
   -> Maybe Text
   -> [User]
   -> Text
   -> GroupChannel)
-> Parser (Snowflake User)
-> Parser
     (Maybe (Snowflake Message)
      -> Maybe UTCTime -> Maybe Text -> [User] -> Text -> GroupChannel)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Snowflake User)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"owner_id"
      Parser
  (Maybe (Snowflake Message)
   -> Maybe UTCTime -> Maybe Text -> [User] -> Text -> GroupChannel)
-> Parser (Maybe (Snowflake Message))
-> Parser
     (Maybe UTCTime -> Maybe Text -> [User] -> Text -> GroupChannel)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe (Snowflake Message))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"last_message_id"
      Parser
  (Maybe UTCTime -> Maybe Text -> [User] -> Text -> GroupChannel)
-> Parser (Maybe UTCTime)
-> Parser (Maybe Text -> [User] -> Text -> GroupChannel)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe UTCTime)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"last_pin_timestamp"
      Parser (Maybe Text -> [User] -> Text -> GroupChannel)
-> Parser (Maybe Text) -> Parser ([User] -> Text -> GroupChannel)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"icon"
      Parser ([User] -> Text -> GroupChannel)
-> Parser [User] -> Parser (Text -> GroupChannel)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser [User]
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"recipients"
      Parser (Text -> GroupChannel) -> Parser Text -> Parser GroupChannel
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"