use async_trait::async_trait;
use futures_core::Stream;
use crate::domain::{
AccountType, Asset, Balance, BuyBinaryRequest, Candle, CandleRequest, CandleSubscribe, Event,
MarketStatus, OperationResult, OptionType, OrderId, Payout, PositionHistory,
};
use super::config::BrokerParams;
use super::error::BrokerError;
use super::extras::IqOptionExtras;
use super::session::Session;
pub type CandleStream = std::pin::Pin<Box<dyn Stream<Item = Result<Candle, BrokerError>> + Send>>;
pub type EventStream = std::pin::Pin<Box<dyn Stream<Item = Event> + Send>>;
#[async_trait]
pub trait Broker: Send + Sync {
fn name(&self) -> &'static str;
async fn connect(params: BrokerParams) -> Result<Self, BrokerError>
where
Self: Sized;
async fn disconnect(&mut self) -> Result<(), BrokerError>;
async fn is_connected(&self) -> bool;
async fn session(&self) -> Result<Session, BrokerError>;
async fn get_balance(&self) -> Result<Balance, BrokerError>;
async fn get_account_type(&self) -> Result<AccountType, BrokerError>;
async fn switch_account(&mut self, kind: AccountType) -> Result<(), BrokerError>;
async fn list_assets(&self) -> Result<Vec<Asset>, BrokerError>;
async fn asset_status(&self, symbol: &str) -> Result<MarketStatus, BrokerError>;
async fn get_candles(&self, req: CandleRequest) -> Result<Vec<Candle>, BrokerError>;
async fn subscribe_candles(&self, req: CandleSubscribe) -> Result<CandleStream, BrokerError>;
async fn get_position_history(
&self,
symbol: Option<&str>,
limit: usize,
offset: usize,
) -> Result<PositionHistory, BrokerError>;
async fn get_payout(
&self,
symbol: &str,
expiration_seconds: u64,
) -> Result<Payout, BrokerError>;
async fn buy_binary(&self, req: BuyBinaryRequest) -> Result<OrderId, BrokerError>;
async fn sell_option(&self, order_id: &OrderId) -> Result<(), BrokerError>;
async fn get_operation_result(
&self,
order_id: &OrderId,
) -> Result<OperationResult, BrokerError>;
fn iqoption_extras(&self) -> Option<&dyn IqOptionExtras> {
None
}
async fn subscribe_ticks(&self, symbol: &str) -> Result<(), BrokerError> {
let _ = symbol;
Err(capacidad_no_soportada(self.name(), "stream de ticks"))
}
async fn events(&self) -> Result<EventStream, BrokerError>;
}
fn capacidad_no_soportada(broker: &str, capacidad: &str) -> BrokerError {
BrokerError::Rejected(format!("{broker}: el bróker no ofrece {capacidad}"))
}
const fn _assert_broker_bounds<T: Broker + Send + Sync>() {}
pub const MVP_OPTION_TYPES: &[OptionType] = &[OptionType::Binary, OptionType::Digital];