use actix_http::encoding::Decoder;
use actix_web::{
    client::{ClientRequest, ClientResponse, SendRequestError},
    dev::Payload,
    error::BlockingError,
    http::header::InvalidHeaderValue,
};
use std::{fmt::Display, future::Future, pin::Pin};
use crate::{Config, PrepareSignError, Sign};
pub mod middleware;
#[cfg(feature = "sha-2")]
mod sha2;
#[cfg(feature = "sha-3")]
mod sha3;
mod sign;
pub trait DigestCreate {
        const NAME: &'static str;
        fn compute(&mut self, input: &[u8]) -> String;
}
pub trait DigestVerify {
                fn verify(&mut self, digests: &[DigestPart], payload: &[u8]) -> bool;
}
pub trait SignExt: Sign {
        fn authorization_signature_with_digest<F, E, K, D, V>(
        self,
        config: Config,
        key_id: K,
        digest: D,
        v: V,
        f: F,
    ) -> Pin<Box<dyn Future<Output = Result<DigestClient<V>, E>>>>
    where
        F: FnOnce(&str) -> Result<String, E> + Send + 'static,
        E: From<BlockingError<E>>
            + From<PrepareSignError>
            + From<InvalidHeaderValue>
            + std::fmt::Debug
            + Send
            + 'static,
        K: Display + 'static,
        D: DigestCreate + Send + 'static,
        V: AsRef<[u8]> + Send + 'static,
        Self: Sized;
        fn signature_with_digest<F, E, K, D, V>(
        self,
        config: Config,
        key_id: K,
        digest: D,
        v: V,
        f: F,
    ) -> Pin<Box<dyn Future<Output = Result<DigestClient<V>, E>>>>
    where
        F: FnOnce(&str) -> Result<String, E> + Send + 'static,
        E: From<BlockingError<E>>
            + From<PrepareSignError>
            + From<InvalidHeaderValue>
            + std::fmt::Debug
            + Send
            + 'static,
        K: Display + 'static,
        D: DigestCreate + Send + 'static,
        V: AsRef<[u8]> + Send + 'static,
        Self: Sized;
}
pub struct DigestPart {
        pub algorithm: String,
        pub digest: String,
}
pub struct DigestClient<V> {
    req: ClientRequest,
    body: V,
}
impl<V> DigestClient<V>
where
    V: AsRef<[u8]>,
{
    fn new(req: ClientRequest, body: V) -> Self {
        DigestClient { req, body }
    }
                    pub fn send(
        self,
    ) -> impl Future<Output = Result<ClientResponse<Decoder<Payload>>, SendRequestError>> {
        self.req.send_body(self.body.as_ref().to_vec())
    }
}