///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
use crate::{client::Client, error::Error, payload, proto::common::*};

impl Client {
    /// Close an open position, either fully or partially.
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    /// To close fully, pass the full position volume.
    pub async fn close_position(
        &self,
        ctid_trader_account_id: i64,
        position_id: i64,
        volume: i64,
    ) -> Result<ProtoOaExecutionEvent, Error> {
        let req = ProtoOaClosePositionReq {
            payload_type: Some(payload::OA_CLOSE_POSITION_REQ as i32),
            ctid_trader_account_id,
            position_id,
            volume,
        };

        self.command(
            payload::OA_CLOSE_POSITION_REQ,
            req,
            payload::OA_EXECUTION_EVENT,
        )
        .await
    }

    /// Amend the Stop Loss and/or Take Profit of an open position.
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    /// `stop_loss` and `take_profit` are absolute price levels.
    pub async fn amend_position_sltp(
        &self,
        ctid_trader_account_id: i64,
        position_id: i64,
        stop_loss: Option<f64>,
        take_profit: Option<f64>,
        trailing_stop_loss: Option<bool>,
    ) -> Result<ProtoOaExecutionEvent, Error> {
        let req = ProtoOaAmendPositionSltpReq {
            payload_type: Some(ProtoOaPayloadType::ProtoOaAmendPositionSltpReq as i32),
            ctid_trader_account_id,
            position_id,
            stop_loss,
            take_profit,
            trailing_stop_loss,
            ..Default::default()
        };

        self.command(
            payload::OA_AMEND_POSITION_SLTP_REQ,
            req,
            payload::OA_EXECUTION_EVENT,
        )
        .await
    }

    /// Get unrealized P&L for all open positions.
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    /// Note: monetary values in the response are scaled by `10^moneyDigits`.
    pub async fn position_unrealized_pnl(
        &self,
        ctid_trader_account_id: i64,
    ) -> Result<ProtoOaGetPositionUnrealizedPnLRes, Error> {
        let req = ProtoOaGetPositionUnrealizedPnLReq {
            payload_type: Some(payload::OA_GET_POSITION_UNREALIZED_PNL_REQ as i32),
            ctid_trader_account_id,
        };

        self.command(
            payload::OA_GET_POSITION_UNREALIZED_PNL_REQ,
            req,
            payload::OA_GET_POSITION_UNREALIZED_PNL_RES,
        )
        .await
    }
}

#[cfg(test)]
mod tests {

    #[tokio::test]
    async fn test() {}
}
