module Proof.Assistant.Request where

import Data.ByteString (ByteString)
import Data.Text.Encoding (encodeUtf8)
import Telegram.Bot.API
import Telegram.Bot.Simple.UpdateParser

-- | Request from Telegram.
data InterpreterRequest = InterpreterRequest
  { InterpreterRequest -> ChatId
interpreterRequestTelegramChatId :: !ChatId -- ^ Telegram ChatId (for reply and isolation).
  , InterpreterRequest -> MessageId
interpreterRequestTelegramMessageId :: !MessageId -- ^ Telegram MessageId (for reply).
  , InterpreterRequest -> ByteString
interpreterRequestMessage :: !ByteString -- ^ Message content.
  }

-- | Cast Telegram Update to 'InterpreterRequest'.
updateToRequest :: Update -> Maybe InterpreterRequest
updateToRequest :: Update -> Maybe InterpreterRequest
updateToRequest Update
upd = ChatId -> MessageId -> ByteString -> InterpreterRequest
InterpreterRequest
  (ChatId -> MessageId -> ByteString -> InterpreterRequest)
-> Maybe ChatId
-> Maybe (MessageId -> ByteString -> InterpreterRequest)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Update -> Maybe ChatId
updateChatId Update
upd -- chatId
  Maybe (MessageId -> ByteString -> InterpreterRequest)
-> Maybe MessageId -> Maybe (ByteString -> InterpreterRequest)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((Message -> MessageId) -> Maybe Message -> Maybe MessageId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Message -> MessageId
messageMessageId (Maybe Message -> Maybe MessageId)
-> (Update -> Maybe Message) -> Update -> Maybe MessageId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Update -> Maybe Message
extractUpdateMessage) Update
upd -- messageId
  Maybe (ByteString -> InterpreterRequest)
-> Maybe ByteString -> Maybe InterpreterRequest
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ByteString
encodeUtf8 (Text -> ByteString) -> Maybe Text -> Maybe ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Update -> Maybe Text
updateMessageText Update
upd)