use std::fmt::{Debug, Formatter};
use std::sync::Arc;
use crate::device::utils::{get_caller_id, get_listen, get_proxy, get_transcribe};
use crate::{ConnectState, FlowState, NumberFormat, VoiceServer, format_number, Region};
use cal_jambonz::dial::TranscribeDial;
use cal_jambonz::listen::Listen;
use cal_jambonz::shared::shared::SIPStatus;
use serde::{Deserialize, Serialize};
use crate::device::device::{Connector, Endpoint};
use crate::device::shared::{AiOptions, CallControl, RecordOptions, TranscribeOptions};
#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct HuntGroup {
#[serde(default = "crate::device::shared::build_present")]
pub present: String,
#[serde(default = "crate::device::shared::build_format")]
pub format: NumberFormat,
#[serde(default)]
pub proxies: Vec<String>,
#[serde(default = "crate::device::shared::build_country_code")]
pub country_code: String,
#[serde(default = "crate::device::shared::build_connect_to")]
pub on_complete: String,
#[serde(default = "crate::device::shared::build_connect_to")]
pub on_busy: String,
#[serde(default = "crate::device::shared::build_connect_to")]
pub on_fail: String,
#[serde(default = "crate::device::shared::build_connect_to")]
pub on_no_answer: String,
#[serde(default = "crate::device::shared::build_ring_time")]
pub ring_time: u8,
#[serde(default = "crate::device::shared::build_call_time")]
pub max_call_time: u16,
#[serde(default)]
pub transcribe_options: TranscribeOptions,
#[serde(default)]
pub record_options: RecordOptions,
#[serde(default)]
pub devices: Vec<String>,
#[serde(default)]
pub ai_options: AiOptions,
#[serde(default)]
pub call_control: CallControl,
}
impl Connector for HuntGroup {
fn get_connect_to(&mut self, state: &mut FlowState) -> ConnectState {
match &state.get_dial_status() {
Some(status) => match status {
SIPStatus::Ok => ConnectState::complete(self.on_complete.as_str()),
SIPStatus::RequestTerminated => ConnectState::no_answer(self.on_no_answer.as_str()),
SIPStatus::BusyHere | SIPStatus::BusyEverywhere => {
ConnectState::busy(self.on_busy.as_str())
}
_ => ConnectState::fail(self.on_fail.as_str()),
},
None => ConnectState::dialling(),
}
}
}
impl Endpoint for HuntGroup {
fn get_caller_id(&self, state: &FlowState) -> String {
if self.present == "pass-through" {
get_caller_id(&self.format, state)
} else {
format_number(
&self.present,
state.get_country_code(),
&self.format.clone(),
)
}
}
fn get_called_id(&self, _state: &FlowState) -> String {
panic!(
"Use target called_id not HuntGroups as individual targets have specific destinations"
)
}
fn get_listen(&self, state: &FlowState) -> Option<Listen> {
get_listen(&self.record_options, self.ai_options.clone(), state)
}
fn get_proxy(&self, state: &FlowState, regions: Vec<Arc<Region>>) -> Option<VoiceServer> {
get_proxy(self.proxies.clone(), state, regions)
}
fn get_transcribe(&self, _state: &FlowState) -> Option<TranscribeDial> {
get_transcribe(self.transcribe_options.clone())
}
}
impl Debug for HuntGroup {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
f.debug_struct("HuntGroup")
.field("present", &self.present)
.field("format", &self.format)
.field("proxies", &self.proxies)
.field("country_code", &self.country_code)
.field("on_complete", &self.on_complete)
.field("on_busy", &self.on_busy)
.field("on_fail", &self.on_fail)
.field("on_no_answer", &self.on_no_answer)
.field("ring_time", &self.ring_time)
.field("max_call_time", &self.max_call_time)
.field("transcribe_options", &self.transcribe_options)
.field("record_options", &self.record_options)
.field("devices", &self.devices)
.field("ai_options", &self.ai_options)
.field("call_control", &self.call_control)
.finish()
}
}