{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoDeriveAnyClass #-}

-- | Guild permissions
module Calamity.Types.Model.Guild.Permissions
  ( Permissions (..),
    createInstantInvite,
    kickMembers,
    banMembers,
    administrator,
    manageChannels,
    manageGuild,
    addReactions,
    viewAuditLog,
    prioritySpeaker,
    stream,
    viewChannel,
    sendMessages,
    sendTtsMessages,
    manageMessages,
    embedLinks,
    attachFiles,
    readMessageHistory,
    mentionEveryone,
    useExternalEmojis,
    viewGuildInsights,
    connect,
    speak,
    muteMembers,
    deafenMembers,
    moveMembers,
    useVad,
    changeNickname,
    manageNicknames,
    manageRoles,
    manageWebhooks,
    manageEmojis,
  )
where

import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.Aeson.Types (parseFail)
import Data.Flags ()
import Data.Flags.TH
import Data.Text.Read (decimal)
import Data.Word
import TextShow
import Control.DeepSeq (NFData)

$( bitmaskWrapper
     "Permissions"
     ''Word64
     []
     [ ("createInstantInvite", 0x00000001),
       ("kickMembers", 0x00000002),
       ("banMembers", 0x00000004),
       ("administrator", 0x00000008),
       ("manageChannels", 0x00000010),
       ("manageGuild", 0x00000020),
       ("addReactions", 0x00000040),
       ("viewAuditLog", 0x00000080),
       ("prioritySpeaker", 0x00000100),
       ("stream", 0x00000200),
       ("viewChannel", 0x00000400),
       ("sendMessages", 0x00000800),
       ("sendTtsMessages", 0x00001000),
       ("manageMessages", 0x00002000),
       ("embedLinks", 0x00004000),
       ("attachFiles", 0x00008000),
       ("readMessageHistory", 0x00010000),
       ("mentionEveryone", 0x00020000),
       ("useExternalEmojis", 0x00040000),
       ("viewGuildInsights", 0x00080000),
       ("connect", 0x00100000),
       ("speak", 0x00200000),
       ("muteMembers", 0x00400000),
       ("deafenMembers", 0x00800000),
       ("moveMembers", 0x01000000),
       ("useVad", 0x02000000),
       ("changeNickname", 0x04000000),
       ("manageNicknames", 0x08000000),
       ("manageRoles", 0x10000000),
       ("manageWebhooks", 0x20000000),
       ("manageEmojis", 0x4000000)
     ]
 )

instance ToJSON Permissions where
  toJSON :: Permissions -> Value
toJSON = Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Text -> Value) -> (Permissions -> Text) -> Permissions -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Permissions -> Text
forall a. TextShow a => a -> Text
showt
  toEncoding :: Permissions -> Encoding
toEncoding = Text -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding (Text -> Encoding)
-> (Permissions -> Text) -> Permissions -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Permissions -> Text
forall a. TextShow a => a -> Text
showt

instance FromJSON Permissions where
  parseJSON :: Value -> Parser Permissions
parseJSON Value
a = do
    Text
asText <- Value -> Parser Text
forall a. FromJSON a => Value -> Parser a
parseJSON Value
a
    case Reader Word64
forall a. Integral a => Reader a
decimal Text
asText of
      Right (Word64
n, Text
_) -> Permissions -> Parser Permissions
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Permissions -> Parser Permissions)
-> Permissions -> Parser Permissions
forall a b. (a -> b) -> a -> b
$ Word64 -> Permissions
Permissions Word64
n
      Left String
e -> String -> Parser Permissions
forall a. String -> Parser a
parseFail String
e

deriving via FromStringShow Permissions instance TextShow Permissions
deriving via Word64 instance NFData Permissions