{-# LANGUAGE TemplateHaskell #-}

-- | Permission overwrites
module Calamity.Types.Model.Guild.Overwrite (Overwrite (..)) where

import Calamity.Internal.Utils
import Calamity.Types.Model.Guild.Permissions
import Calamity.Types.Snowflake
import Data.Aeson ((.:))
import Data.Aeson qualified as Aeson
import Optics.TH
import TextShow.TH

data Overwrite = Overwrite
  { Overwrite -> Snowflake Overwrite
id :: Snowflake Overwrite
  , Overwrite -> Int
type_ :: Int
  , Overwrite -> Permissions
allow :: Permissions
  , Overwrite -> Permissions
deny :: Permissions
  }
  deriving (Overwrite -> Overwrite -> Bool
(Overwrite -> Overwrite -> Bool)
-> (Overwrite -> Overwrite -> Bool) -> Eq Overwrite
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Overwrite -> Overwrite -> Bool
== :: Overwrite -> Overwrite -> Bool
$c/= :: Overwrite -> Overwrite -> Bool
/= :: Overwrite -> Overwrite -> Bool
Eq, Int -> Overwrite -> ShowS
[Overwrite] -> ShowS
Overwrite -> String
(Int -> Overwrite -> ShowS)
-> (Overwrite -> String)
-> ([Overwrite] -> ShowS)
-> Show Overwrite
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Overwrite -> ShowS
showsPrec :: Int -> Overwrite -> ShowS
$cshow :: Overwrite -> String
show :: Overwrite -> String
$cshowList :: [Overwrite] -> ShowS
showList :: [Overwrite] -> ShowS
Show)
  deriving (HasID Overwrite) via HasIDField "id" Overwrite
  deriving ([Overwrite] -> Value
[Overwrite] -> Encoding
Overwrite -> Value
Overwrite -> Encoding
(Overwrite -> Value)
-> (Overwrite -> Encoding)
-> ([Overwrite] -> Value)
-> ([Overwrite] -> Encoding)
-> ToJSON Overwrite
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: Overwrite -> Value
toJSON :: Overwrite -> Value
$ctoEncoding :: Overwrite -> Encoding
toEncoding :: Overwrite -> Encoding
$ctoJSONList :: [Overwrite] -> Value
toJSONList :: [Overwrite] -> Value
$ctoEncodingList :: [Overwrite] -> Encoding
toEncodingList :: [Overwrite] -> Encoding
Aeson.ToJSON) via CalamityToJSON Overwrite

instance CalamityToJSON' Overwrite where
  toPairs :: forall kv. KeyValue kv => Overwrite -> [Maybe kv]
toPairs Overwrite {Int
Permissions
Snowflake Overwrite
$sel:id:Overwrite :: Overwrite -> Snowflake Overwrite
$sel:type_:Overwrite :: Overwrite -> Int
$sel:allow:Overwrite :: Overwrite -> Permissions
$sel:deny:Overwrite :: Overwrite -> Permissions
id :: Snowflake Overwrite
type_ :: Int
allow :: Permissions
deny :: Permissions
..} =
    [ Key
"id" Key -> Snowflake Overwrite -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Snowflake Overwrite
id
    , Key
"type" Key -> Int -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Int
type_
    , Key
"allow" Key -> Permissions -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Permissions
allow
    , Key
"deny" Key -> Permissions -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Permissions
deny
    ]

instance Aeson.FromJSON Overwrite where
  parseJSON :: Value -> Parser Overwrite
parseJSON = String -> (Object -> Parser Overwrite) -> Value -> Parser Overwrite
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Overwrite" ((Object -> Parser Overwrite) -> Value -> Parser Overwrite)
-> (Object -> Parser Overwrite) -> Value -> Parser Overwrite
forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Snowflake Overwrite
-> Int -> Permissions -> Permissions -> Overwrite
Overwrite
      (Snowflake Overwrite
 -> Int -> Permissions -> Permissions -> Overwrite)
-> Parser (Snowflake Overwrite)
-> Parser (Int -> Permissions -> Permissions -> Overwrite)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser (Snowflake Overwrite)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
      Parser (Int -> Permissions -> Permissions -> Overwrite)
-> Parser Int -> Parser (Permissions -> Permissions -> Overwrite)
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 Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type"
      Parser (Permissions -> Permissions -> Overwrite)
-> Parser Permissions -> Parser (Permissions -> Overwrite)
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 Permissions
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"allow"
      Parser (Permissions -> Overwrite)
-> Parser Permissions -> Parser Overwrite
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 Permissions
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"deny"

$(deriveTextShow ''Overwrite)
$(makeFieldLabelsNoPrefix ''Overwrite)