use reqwest;
use tokio;
use regex::Regex;
use std::error::Error;
use std::fmt;

#[derive(Debug)]
enum MyError {
    Reqwest(reqwest::Error),
    Io(std::io::Error),
}

impl fmt::Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MyError::Reqwest(e) => write!(f, "Reqwest error: {}", e),
            MyError::Io(e) => write!(f, "IO error: {}", e),
        }
    }
}

impl Error for MyError {}

impl From<reqwest::Error> for MyError {
    fn from(e: reqwest::Error) -> Self {
        MyError::Reqwest(e)
    }
}

impl From<std::io::Error> for MyError {
    fn from(e: std::io::Error) -> Self {
        MyError::Io(e)
    }
}
#[tokio::main]
async fn main() -> Result<(), MyError> {
    let body = reqwest::get("http://anthony-mansuy.fr/Enseignements-CPGE.html")
        .await?
        .text()
        .await?;

    let re = Regex::new(r#"href="([^"]*)""#).unwrap();
    
    let mut urlsvect = Vec::new(); // Changed to a mutable Vec
    for url in re.captures_iter(&body) {
        if let Some(url) = url.get(1) {
            let url_str = url.as_str();
            if url_str.contains("pdf") {
                urlsvect.push(url_str.to_string());
            }
        }
    };

    let urlsvect2 = urlsvect.clone();
    //println!("{:?}", urlsvect2);
    let prefix = "http://anthony-mansuy.fr/";
    for url in urlsvect.iter_mut() { // Changed to mutable iteration
        url.insert_str(0, prefix);
    }


    for i in 0..urlsvect.len() {
        //println!("{}", url);
        let url = &urlsvect[i];
        //println!("{}", url);
        let response = reqwest::get(url).await?;
        let content = response.bytes().await?;
        tokio::fs::write(format!("{}",urlsvect2[i]), content.clone()).await?;
    }

    Ok(())
}