{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Discord.Internal.Rest.Interactions where

import Data.Aeson (encode)
import qualified Data.ByteString.Lazy as BL
import Discord.Internal.Rest.Prelude
import Discord.Internal.Types
import Discord.Internal.Types.Interactions
import Network.HTTP.Client.MultipartFormData (PartM, partBS)
import Network.HTTP.Req as R

data InteractionResponseRequest a where
  CreateInteractionResponse :: InteractionId -> InteractionToken -> InteractionResponse -> InteractionResponseRequest ()
  GetOriginalInteractionResponse :: ApplicationId -> InteractionToken -> InteractionResponseRequest Message
  EditOriginalInteractionResponse :: ApplicationId -> InteractionToken -> InteractionResponseMessage -> InteractionResponseRequest Message
  DeleteOriginalInteractionResponse :: ApplicationId -> InteractionToken -> InteractionResponseRequest ()
  CreateFollowupInteractionMessage :: ApplicationId -> InteractionToken -> InteractionResponseMessage -> InteractionResponseRequest Message
  GetFollowupInteractionMessage :: ApplicationId -> InteractionToken -> MessageId -> InteractionResponseRequest Message
  EditFollowupInteractionMessage :: ApplicationId -> InteractionToken -> MessageId -> InteractionResponse -> InteractionResponseRequest Message
  DeleteFollowupInteractionMessage :: ApplicationId -> InteractionToken -> MessageId -> InteractionResponseRequest ()

instance Request (InteractionResponseRequest a) where
  jsonRequest :: InteractionResponseRequest a -> JsonRequest
jsonRequest = InteractionResponseRequest a -> JsonRequest
forall a. InteractionResponseRequest a -> JsonRequest
interactionResponseJsonRequest
  majorRoute :: InteractionResponseRequest a -> String
majorRoute = InteractionResponseRequest a -> String
forall a. InteractionResponseRequest a -> String
interactionResponseMajorRoute

interactionResponseMajorRoute :: InteractionResponseRequest a -> String
interactionResponseMajorRoute :: InteractionResponseRequest a -> String
interactionResponseMajorRoute InteractionResponseRequest a
a = case InteractionResponseRequest a
a of
  (CreateInteractionResponse InteractionId
iid InteractionToken
_ InteractionResponse
_) -> String
"intresp " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> InteractionId -> String
forall a. Show a => a -> String
show InteractionId
iid
  (GetOriginalInteractionResponse InteractionId
aid InteractionToken
_) -> String
"intresp " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> InteractionId -> String
forall a. Show a => a -> String
show InteractionId
aid
  (EditOriginalInteractionResponse InteractionId
aid InteractionToken
_ InteractionResponseMessage
_) -> String
"intresp " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> InteractionId -> String
forall a. Show a => a -> String
show InteractionId
aid
  (DeleteOriginalInteractionResponse InteractionId
aid InteractionToken
_) -> String
"intresp " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> InteractionId -> String
forall a. Show a => a -> String
show InteractionId
aid
  (CreateFollowupInteractionMessage InteractionId
iid InteractionToken
_ InteractionResponseMessage
_) -> String
"intrespf " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> InteractionId -> String
forall a. Show a => a -> String
show InteractionId
iid
  (GetFollowupInteractionMessage InteractionId
aid InteractionToken
_ InteractionId
_) -> String
"intrespf " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> InteractionId -> String
forall a. Show a => a -> String
show InteractionId
aid
  (EditFollowupInteractionMessage InteractionId
aid InteractionToken
_ InteractionId
_ InteractionResponse
_) -> String
"intrespf " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> InteractionId -> String
forall a. Show a => a -> String
show InteractionId
aid
  (DeleteFollowupInteractionMessage InteractionId
aid InteractionToken
_ InteractionId
_) -> String
"intrespf " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> InteractionId -> String
forall a. Show a => a -> String
show InteractionId
aid

interaction :: ApplicationId -> InteractionToken -> R.Url 'R.Https
interaction :: InteractionId -> InteractionToken -> Url 'Https
interaction InteractionId
aid InteractionToken
it = Url 'Https
baseUrl Url 'Https -> InteractionToken -> Url 'Https
forall (scheme :: Scheme).
Url scheme -> InteractionToken -> Url scheme
/: InteractionToken
"webhooks" Url 'Https -> InteractionId -> Url 'Https
forall a (scheme :: Scheme).
Show a =>
Url scheme -> a -> Url scheme
// InteractionId
aid Url 'Https -> InteractionToken -> Url 'Https
forall (scheme :: Scheme).
Url scheme -> InteractionToken -> Url scheme
/: InteractionToken
it Url 'Https -> InteractionToken -> Url 'Https
forall (scheme :: Scheme).
Url scheme -> InteractionToken -> Url scheme
/: InteractionToken
"messages"

interactionResponseJsonRequest :: InteractionResponseRequest a -> JsonRequest
interactionResponseJsonRequest :: InteractionResponseRequest a -> JsonRequest
interactionResponseJsonRequest InteractionResponseRequest a
a = case InteractionResponseRequest a
a of
  (CreateInteractionResponse InteractionId
iid InteractionToken
it InteractionResponse
i) ->
    Url 'Https
-> RestIO ReqBodyMultipart -> Option 'Https -> JsonRequest
forall a.
HttpBody a =>
Url 'Https -> RestIO a -> Option 'Https -> JsonRequest
Post (Url 'Https
baseUrl Url 'Https -> InteractionToken -> Url 'Https
forall (scheme :: Scheme).
Url scheme -> InteractionToken -> Url scheme
/: InteractionToken
"interactions" Url 'Https -> InteractionId -> Url 'Https
forall a (scheme :: Scheme).
Show a =>
Url scheme -> a -> Url scheme
// InteractionId
iid Url 'Https -> InteractionToken -> Url 'Https
forall (scheme :: Scheme).
Url scheme -> InteractionToken -> Url scheme
/: InteractionToken
it Url 'Https -> InteractionToken -> Url 'Https
forall (scheme :: Scheme).
Url scheme -> InteractionToken -> Url scheme
/: InteractionToken
"callback") (InteractionResponse -> RestIO ReqBodyMultipart
convert InteractionResponse
i) Option 'Https
forall a. Monoid a => a
mempty
  (GetOriginalInteractionResponse InteractionId
aid InteractionToken
it) ->
    Url 'Https -> Option 'Https -> JsonRequest
Get (InteractionId -> InteractionToken -> Url 'Https
interaction InteractionId
aid InteractionToken
it Url 'Https -> InteractionToken -> Url 'Https
forall (scheme :: Scheme).
Url scheme -> InteractionToken -> Url scheme
/: InteractionToken
"@original") Option 'Https
forall a. Monoid a => a
mempty
  (EditOriginalInteractionResponse InteractionId
aid InteractionToken
it InteractionResponseMessage
i) ->
    Url 'Https
-> RestIO ReqBodyMultipart -> Option 'Https -> JsonRequest
forall a.
HttpBody a =>
Url 'Https -> RestIO a -> Option 'Https -> JsonRequest
Patch (InteractionId -> InteractionToken -> Url 'Https
interaction InteractionId
aid InteractionToken
it Url 'Https -> InteractionToken -> Url 'Https
forall (scheme :: Scheme).
Url scheme -> InteractionToken -> Url scheme
/: InteractionToken
"@original") (InteractionResponseMessage -> RestIO ReqBodyMultipart
convertIRM InteractionResponseMessage
i) Option 'Https
forall a. Monoid a => a
mempty
  (DeleteOriginalInteractionResponse InteractionId
aid InteractionToken
it) ->
    Url 'Https -> Option 'Https -> JsonRequest
Delete (InteractionId -> InteractionToken -> Url 'Https
interaction InteractionId
aid InteractionToken
it Url 'Https -> InteractionToken -> Url 'Https
forall (scheme :: Scheme).
Url scheme -> InteractionToken -> Url scheme
/: InteractionToken
"@original") Option 'Https
forall a. Monoid a => a
mempty
  (CreateFollowupInteractionMessage InteractionId
aid InteractionToken
it InteractionResponseMessage
i) ->
    Url 'Https
-> RestIO ReqBodyMultipart -> Option 'Https -> JsonRequest
forall a.
HttpBody a =>
Url 'Https -> RestIO a -> Option 'Https -> JsonRequest
Post (Url 'Https
baseUrl Url 'Https -> InteractionToken -> Url 'Https
forall (scheme :: Scheme).
Url scheme -> InteractionToken -> Url scheme
/: InteractionToken
"webhooks" Url 'Https -> InteractionId -> Url 'Https
forall a (scheme :: Scheme).
Show a =>
Url scheme -> a -> Url scheme
// InteractionId
aid Url 'Https -> InteractionToken -> Url 'Https
forall (scheme :: Scheme).
Url scheme -> InteractionToken -> Url scheme
/: InteractionToken
it) (InteractionResponseMessage -> RestIO ReqBodyMultipart
convertIRM InteractionResponseMessage
i) Option 'Https
forall a. Monoid a => a
mempty
  (GetFollowupInteractionMessage InteractionId
aid InteractionToken
it InteractionId
mid) ->
    Url 'Https -> Option 'Https -> JsonRequest
Get (InteractionId -> InteractionToken -> Url 'Https
interaction InteractionId
aid InteractionToken
it Url 'Https -> InteractionId -> Url 'Https
forall a (scheme :: Scheme).
Show a =>
Url scheme -> a -> Url scheme
// InteractionId
mid) Option 'Https
forall a. Monoid a => a
mempty
  (EditFollowupInteractionMessage InteractionId
aid InteractionToken
it InteractionId
mid InteractionResponse
i) ->
    Url 'Https
-> RestIO ReqBodyMultipart -> Option 'Https -> JsonRequest
forall a.
HttpBody a =>
Url 'Https -> RestIO a -> Option 'Https -> JsonRequest
Patch (InteractionId -> InteractionToken -> Url 'Https
interaction InteractionId
aid InteractionToken
it Url 'Https -> InteractionId -> Url 'Https
forall a (scheme :: Scheme).
Show a =>
Url scheme -> a -> Url scheme
// InteractionId
mid) (InteractionResponse -> RestIO ReqBodyMultipart
convert InteractionResponse
i) Option 'Https
forall a. Monoid a => a
mempty
  (DeleteFollowupInteractionMessage InteractionId
aid InteractionToken
it InteractionId
mid) ->
    Url 'Https -> Option 'Https -> JsonRequest
Delete (InteractionId -> InteractionToken -> Url 'Https
interaction InteractionId
aid InteractionToken
it Url 'Https -> InteractionId -> Url 'Https
forall a (scheme :: Scheme).
Show a =>
Url scheme -> a -> Url scheme
// InteractionId
mid) Option 'Https
forall a. Monoid a => a
mempty
  where
    convert :: InteractionResponse -> RestIO ReqBodyMultipart
    convert :: InteractionResponse -> RestIO ReqBodyMultipart
convert ir :: InteractionResponse
ir@(InteractionResponseChannelMessage InteractionResponseMessage
irm) = [Part] -> RestIO ReqBodyMultipart
forall (m :: * -> *). MonadIO m => [Part] -> m ReqBodyMultipart
R.reqBodyMultipart (InteractionToken -> ByteString -> Part
forall (m :: * -> *).
Applicative m =>
InteractionToken -> ByteString -> PartM m
partBS InteractionToken
"payload_json" (ByteString -> ByteString
BL.toStrict (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ InteractionResponse -> ByteString
forall a. ToJSON a => a -> ByteString
encode InteractionResponse
ir) Part -> [Part] -> [Part]
forall a. a -> [a] -> [a]
: InteractionResponseMessage -> [Part]
convert' InteractionResponseMessage
irm)
    convert ir :: InteractionResponse
ir@(InteractionResponseUpdateMessage InteractionResponseMessage
irm) = [Part] -> RestIO ReqBodyMultipart
forall (m :: * -> *). MonadIO m => [Part] -> m ReqBodyMultipart
R.reqBodyMultipart (InteractionToken -> ByteString -> Part
forall (m :: * -> *).
Applicative m =>
InteractionToken -> ByteString -> PartM m
partBS InteractionToken
"payload_json" (ByteString -> ByteString
BL.toStrict (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ InteractionResponse -> ByteString
forall a. ToJSON a => a -> ByteString
encode InteractionResponse
ir) Part -> [Part] -> [Part]
forall a. a -> [a] -> [a]
: InteractionResponseMessage -> [Part]
convert' InteractionResponseMessage
irm)
    convert InteractionResponse
ir = [Part] -> RestIO ReqBodyMultipart
forall (m :: * -> *). MonadIO m => [Part] -> m ReqBodyMultipart
R.reqBodyMultipart [InteractionToken -> ByteString -> Part
forall (m :: * -> *).
Applicative m =>
InteractionToken -> ByteString -> PartM m
partBS InteractionToken
"payload_json" (ByteString -> Part) -> ByteString -> Part
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
BL.toStrict (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ InteractionResponse -> ByteString
forall a. ToJSON a => a -> ByteString
encode InteractionResponse
ir]
    convertIRM :: InteractionResponseMessage -> RestIO ReqBodyMultipart
    convertIRM :: InteractionResponseMessage -> RestIO ReqBodyMultipart
convertIRM InteractionResponseMessage
irm = [Part] -> RestIO ReqBodyMultipart
forall (m :: * -> *). MonadIO m => [Part] -> m ReqBodyMultipart
R.reqBodyMultipart (InteractionToken -> ByteString -> Part
forall (m :: * -> *).
Applicative m =>
InteractionToken -> ByteString -> PartM m
partBS InteractionToken
"payload_json" (ByteString -> ByteString
BL.toStrict (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ InteractionResponseMessage -> ByteString
forall a. ToJSON a => a -> ByteString
encode InteractionResponseMessage
irm) Part -> [Part] -> [Part]
forall a. a -> [a] -> [a]
: InteractionResponseMessage -> [Part]
convert' InteractionResponseMessage
irm)
    convert' :: InteractionResponseMessage -> [PartM IO]
    convert' :: InteractionResponseMessage -> [Part]
convert' InteractionResponseMessage {Maybe Bool
Maybe [CreateEmbed]
Maybe [ComponentActionRow]
Maybe [Attachment]
Maybe InteractionToken
Maybe AllowedMentions
Maybe InteractionResponseMessageFlags
interactionResponseMessageAttachments :: InteractionResponseMessage -> Maybe [Attachment]
interactionResponseMessageComponents :: InteractionResponseMessage -> Maybe [ComponentActionRow]
interactionResponseMessageFlags :: InteractionResponseMessage -> Maybe InteractionResponseMessageFlags
interactionResponseMessageAllowedMentions :: InteractionResponseMessage -> Maybe AllowedMentions
interactionResponseMessageEmbeds :: InteractionResponseMessage -> Maybe [CreateEmbed]
interactionResponseMessageContent :: InteractionResponseMessage -> Maybe InteractionToken
interactionResponseMessageTTS :: InteractionResponseMessage -> Maybe Bool
interactionResponseMessageAttachments :: Maybe [Attachment]
interactionResponseMessageComponents :: Maybe [ComponentActionRow]
interactionResponseMessageFlags :: Maybe InteractionResponseMessageFlags
interactionResponseMessageAllowedMentions :: Maybe AllowedMentions
interactionResponseMessageEmbeds :: Maybe [CreateEmbed]
interactionResponseMessageContent :: Maybe InteractionToken
interactionResponseMessageTTS :: Maybe Bool
..} = case Maybe [CreateEmbed]
interactionResponseMessageEmbeds of
      Maybe [CreateEmbed]
Nothing -> []
      Just [CreateEmbed]
f -> (Maybe CreateEmbed -> [Part]
maybeEmbed (Maybe CreateEmbed -> [Part])
-> (CreateEmbed -> Maybe CreateEmbed) -> CreateEmbed -> [Part]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CreateEmbed -> Maybe CreateEmbed
forall a. a -> Maybe a
Just) (CreateEmbed -> [Part]) -> [CreateEmbed] -> [Part]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [CreateEmbed]
f