-- | The generic guild channel type
module Calamity.Types.Model.Channel.Guild
    ( GuildChannel(..)
    , module Calamity.Types.Model.Channel.Guild.Category
    , module Calamity.Types.Model.Channel.Guild.Text
    , module Calamity.Types.Model.Channel.Guild.Voice ) where

import {-# SOURCE #-} Calamity.Types.Model.Channel
import           Calamity.Types.Model.Channel.ChannelType
import           Calamity.Types.Model.Channel.Guild.Category
import           Calamity.Types.Model.Channel.Guild.Text
import           Calamity.Types.Model.Channel.Guild.Voice
import {-# SOURCE #-} Calamity.Types.Model.Guild.Guild
import           Calamity.Types.Snowflake

import           Control.Lens

import           Data.Aeson
import           Data.Generics.Product.Fields

import           GHC.Generics

import           TextShow
import qualified TextShow.Generic                            as TSG

data GuildChannel
  = GuildTextChannel TextChannel
  | GuildVoiceChannel VoiceChannel
  | GuildCategory Category
  deriving ( Int -> GuildChannel -> ShowS
[GuildChannel] -> ShowS
GuildChannel -> String
(Int -> GuildChannel -> ShowS)
-> (GuildChannel -> String)
-> ([GuildChannel] -> ShowS)
-> Show GuildChannel
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GuildChannel] -> ShowS
$cshowList :: [GuildChannel] -> ShowS
show :: GuildChannel -> String
$cshow :: GuildChannel -> String
showsPrec :: Int -> GuildChannel -> ShowS
$cshowsPrec :: Int -> GuildChannel -> ShowS
Show, GuildChannel -> GuildChannel -> Bool
(GuildChannel -> GuildChannel -> Bool)
-> (GuildChannel -> GuildChannel -> Bool) -> Eq GuildChannel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GuildChannel -> GuildChannel -> Bool
$c/= :: GuildChannel -> GuildChannel -> Bool
== :: GuildChannel -> GuildChannel -> Bool
$c== :: GuildChannel -> GuildChannel -> Bool
Eq, (forall x. GuildChannel -> Rep GuildChannel x)
-> (forall x. Rep GuildChannel x -> GuildChannel)
-> Generic GuildChannel
forall x. Rep GuildChannel x -> GuildChannel
forall x. GuildChannel -> Rep GuildChannel x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GuildChannel x -> GuildChannel
$cfrom :: forall x. GuildChannel -> Rep GuildChannel x
Generic )
  deriving ( Int -> GuildChannel -> Builder
Int -> GuildChannel -> Text
Int -> GuildChannel -> Text
[GuildChannel] -> Builder
[GuildChannel] -> Text
[GuildChannel] -> Text
GuildChannel -> Builder
GuildChannel -> Text
GuildChannel -> Text
(Int -> GuildChannel -> Builder)
-> (GuildChannel -> Builder)
-> ([GuildChannel] -> Builder)
-> (Int -> GuildChannel -> Text)
-> (GuildChannel -> Text)
-> ([GuildChannel] -> Text)
-> (Int -> GuildChannel -> Text)
-> (GuildChannel -> Text)
-> ([GuildChannel] -> Text)
-> TextShow GuildChannel
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
showtlList :: [GuildChannel] -> Text
$cshowtlList :: [GuildChannel] -> Text
showtl :: GuildChannel -> Text
$cshowtl :: GuildChannel -> Text
showtlPrec :: Int -> GuildChannel -> Text
$cshowtlPrec :: Int -> GuildChannel -> Text
showtList :: [GuildChannel] -> Text
$cshowtList :: [GuildChannel] -> Text
showt :: GuildChannel -> Text
$cshowt :: GuildChannel -> Text
showtPrec :: Int -> GuildChannel -> Text
$cshowtPrec :: Int -> GuildChannel -> Text
showbList :: [GuildChannel] -> Builder
$cshowbList :: [GuildChannel] -> Builder
showb :: GuildChannel -> Builder
$cshowb :: GuildChannel -> Builder
showbPrec :: Int -> GuildChannel -> Builder
$cshowbPrec :: Int -> GuildChannel -> Builder
TextShow ) via TSG.FromGeneric GuildChannel

instance FromJSON GuildChannel where
  parseJSON :: Value -> Parser GuildChannel
parseJSON = String
-> (Object -> Parser GuildChannel) -> Value -> Parser GuildChannel
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject "GuildChannel" ((Object -> Parser GuildChannel) -> Value -> Parser GuildChannel)
-> (Object -> Parser GuildChannel) -> Value -> Parser GuildChannel
forall a b. (a -> b) -> a -> b
$ \v :: Object
v -> do
    ChannelType
type_ <- Object
v Object -> Text -> Parser ChannelType
forall a. FromJSON a => Object -> Text -> Parser a
.: "type"

    case ChannelType
type_ of
      GuildTextType     -> TextChannel -> GuildChannel
GuildTextChannel (TextChannel -> GuildChannel)
-> Parser TextChannel -> Parser GuildChannel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser TextChannel
forall a. FromJSON a => Value -> Parser a
parseJSON (Object -> Value
Object Object
v)
      GuildVoiceType    -> VoiceChannel -> GuildChannel
GuildVoiceChannel (VoiceChannel -> GuildChannel)
-> Parser VoiceChannel -> Parser GuildChannel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser VoiceChannel
forall a. FromJSON a => Value -> Parser a
parseJSON (Object -> Value
Object Object
v)
      GuildCategoryType -> Category -> GuildChannel
GuildCategory (Category -> GuildChannel)
-> Parser Category -> Parser GuildChannel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Category
forall a. FromJSON a => Value -> Parser a
parseJSON (Object -> Value
Object Object
v)
      _                 -> String -> Parser GuildChannel
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "Not a valid guild channel"

instance HasID GuildChannel GuildChannel where
  getID :: GuildChannel -> Snowflake GuildChannel
getID (GuildTextChannel a :: TextChannel
a) = Snowflake TextChannel -> Snowflake GuildChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake TextChannel -> Snowflake GuildChannel)
-> Snowflake TextChannel -> Snowflake GuildChannel
forall a b. (a -> b) -> a -> b
$ TextChannel
a TextChannel
-> Getting
     (Snowflake TextChannel) TextChannel (Snowflake TextChannel)
-> Snowflake TextChannel
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' "id" s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @"id"
  getID (GuildVoiceChannel a :: VoiceChannel
a) = Snowflake VoiceChannel -> Snowflake GuildChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake VoiceChannel -> Snowflake GuildChannel)
-> Snowflake VoiceChannel -> Snowflake GuildChannel
forall a b. (a -> b) -> a -> b
$ VoiceChannel
a VoiceChannel
-> Getting
     (Snowflake VoiceChannel) VoiceChannel (Snowflake VoiceChannel)
-> Snowflake VoiceChannel
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' "id" s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @"id"
  getID (GuildCategory a :: Category
a) = Snowflake Category -> Snowflake GuildChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake Category -> Snowflake GuildChannel)
-> Snowflake Category -> Snowflake GuildChannel
forall a b. (a -> b) -> a -> b
$ Category
a Category
-> Getting (Snowflake Category) Category (Snowflake Category)
-> Snowflake Category
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' "id" s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @"id"

instance HasID Channel GuildChannel where
  getID :: GuildChannel -> Snowflake Channel
getID = Snowflake GuildChannel -> Snowflake Channel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake GuildChannel -> Snowflake Channel)
-> (GuildChannel -> Snowflake GuildChannel)
-> GuildChannel
-> Snowflake Channel
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HasID GuildChannel a => a -> Snowflake GuildChannel
forall b a. HasID b a => a -> Snowflake b
getID @GuildChannel

instance HasID Guild GuildChannel where
  getID :: GuildChannel -> Snowflake Guild
getID (GuildTextChannel a :: TextChannel
a) = Snowflake Guild -> Snowflake Guild
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake Guild -> Snowflake Guild)
-> Snowflake Guild -> Snowflake Guild
forall a b. (a -> b) -> a -> b
$ TextChannel
a TextChannel
-> Getting (Snowflake Guild) TextChannel (Snowflake Guild)
-> Snowflake Guild
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' "guildID" s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @"guildID"
  getID (GuildVoiceChannel a :: VoiceChannel
a) = Snowflake Guild -> Snowflake Guild
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake Guild -> Snowflake Guild)
-> Snowflake Guild -> Snowflake Guild
forall a b. (a -> b) -> a -> b
$ VoiceChannel
a VoiceChannel
-> Getting (Snowflake Guild) VoiceChannel (Snowflake Guild)
-> Snowflake Guild
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' "guildID" s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @"guildID"
  getID (GuildCategory a :: Category
a) = Snowflake Guild -> Snowflake Guild
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake Guild -> Snowflake Guild)
-> Snowflake Guild -> Snowflake Guild
forall a b. (a -> b) -> a -> b
$ Category
a Category
-> Getting (Snowflake Guild) Category (Snowflake Guild)
-> Snowflake Guild
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' "guildID" s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @"guildID"