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

impl Client {
    /// Application auth
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    pub async fn application_auth(&self) -> Result<(), Error> {
        let req = ProtoOaApplicationAuthReq {
            payload_type: Some(payload::OA_APPLICATION_AUTH_REQ as i32),
            client_id: self.config.client_id.clone(),
            client_secret: self.config.client_secret.clone(),
        };
        let _: ProtoOaApplicationAuthRes = self
            .command(
                payload::OA_APPLICATION_AUTH_REQ,
                req,
                payload::OA_APPLICATION_AUTH_RES,
            )
            .await?;
        Ok(())
    }

    /// Authenticate a trader account.  Must be called before any account-scoped
    /// request (symbols, deals, orders, …).
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    /// Mirrors `ProtoOAAccountAuthReq` in the Go integration test.
    pub async fn account_auth(
        &self,
        ctid_trader_account_id: i64,
        access_token: &str,
    ) -> Result<ProtoOaAccountAuthRes, Error> {
        let req = ProtoOaAccountAuthReq {
            payload_type: Some(payload::OA_ACCOUNT_AUTH_REQ as i32),
            ctid_trader_account_id,
            access_token: access_token.to_string(),
        };
        self.command(
            payload::OA_ACCOUNT_AUTH_REQ,
            req,
            payload::OA_ACCOUNT_AUTH_RES,
        )
        .await
    }

    /// Refresh an expired access token.
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    ///
    pub async fn refresh_token(
        &self,
        refresh_token: &str,
    ) -> Result<ProtoOaRefreshTokenRes, Error> {
        let req = ProtoOaRefreshTokenReq {
            payload_type: Some(payload::OA_REFRESH_TOKEN_REQ as i32),
            refresh_token: refresh_token.to_string(),
        };
        self.command(
            payload::OA_REFRESH_TOKEN_REQ,
            req,
            payload::OA_REFRESH_TOKEN_RES,
        )
        .await
    }
}

#[cfg(test)]
mod tests {

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