use std::ops::Deref;
use async_trait::async_trait;
use crate::core::base_structs::SenderBase;
use crate::core::traits::Responder;
use crate::core::voiceflow::{VoiceflowBlock, VoiceflowMessage};
use crate::core::voiceflow::dialog_blocks::{VoiceflowButtons, VoiceflowCard, VoiceflowCarousel, VoiceflowImage, VoiceflowText};
use crate::errors::{VoiceflousionError, VoiceflousionResult};
#[async_trait]
pub trait Sender: Deref<Target=SenderBase> + Send + Sync + Sized {
type SenderResponder: Responder;
async fn send_message(&self, client_id: &String, chat_id: &String, message: VoiceflowMessage) -> VoiceflousionResult<Vec<Self::SenderResponder>> {
let mut responses = Vec::with_capacity(message.len());
for block in message.into_iter() {
match block {
VoiceflowBlock::Text(text) => {
let result = self.send_text(client_id, text, chat_id).await?;
responses.push(result)
},
VoiceflowBlock::Image(image) => {
let result = self.send_image(client_id, image, chat_id).await?;
responses.push(result)
},
VoiceflowBlock::Buttons(buttons) => {
let result = self.send_buttons(client_id, buttons, chat_id).await?;
responses.push(result)
},
VoiceflowBlock::Card(card) => {
let result = self.send_card(client_id, card, chat_id).await?;
responses.push(result)
},
VoiceflowBlock::Carousel(carousel) => {
if !carousel.is_empty() {
let result = self.send_carousel(client_id, carousel, chat_id).await?;
responses.push(result)
}
}
_ => {
return Err(VoiceflousionError::ClientRequestInvalidBodyError(
"Sender send_message".to_string(),
"Unsendable block type in the VoiceflowMessage".to_string(),
))
},
}
}
Ok(responses)
}
async fn send_text(&self, client_id: &String, text: VoiceflowText, chat_id: &String) -> VoiceflousionResult<Self::SenderResponder>;
async fn send_image(&self, client_id: &String, image: VoiceflowImage, chat_id: &String) -> VoiceflousionResult<Self::SenderResponder>;
async fn send_buttons(&self, client_id: &String, buttons: VoiceflowButtons, chat_id: &String) -> VoiceflousionResult<Self::SenderResponder>;
async fn send_card(&self, client_id: &String, card: VoiceflowCard, chat_id: &String) -> VoiceflousionResult<Self::SenderResponder>;
async fn send_carousel(&self, client_id: &String, carousel: VoiceflowCarousel, chat_id: &String) -> VoiceflousionResult<Self::SenderResponder>;
}