use std::sync::Arc;

use rand::RngCore;
use rustls::{
    crypto::{CryptoProvider, GetRandomFailed, KeyProvider, SecureRandom},
    pki_types::PrivateKeyDer,
    sign::SigningKey,
};

pub(crate) use verifier::InsecureServerCertificateVerifier;

mod algorithms;
mod verifier;

#[derive(Debug)]
struct Provider;

pub fn provider() -> CryptoProvider {
    CryptoProvider {
        cipher_suites: algorithms::CIPHER_SUITES.to_vec(),
        kx_groups: algorithms::KX_GROUPS.to_vec(),
        signature_verification_algorithms: algorithms::SIGNATURE_VERIFICATION_ALGORITHMS,
        secure_random: &Provider,
        key_provider: &Provider,
    }
}

impl SecureRandom for Provider {
    fn fill(&self, buf: &mut [u8]) -> Result<(), GetRandomFailed> {
        rand_core::OsRng
            .try_fill_bytes(buf)
            .map_err(|_| GetRandomFailed)
    }
}

impl KeyProvider for Provider {
    fn load_private_key(
        &self,
        key_der: PrivateKeyDer<'static>,
    ) -> Result<Arc<dyn SigningKey>, rustls::Error> {
        let _ = key_der;
        todo!("TODO: implement signing key provider for TLS")
    }
}
