use fantoccini::error::CmdError;
use fantoccini::{ClientBuilder, Locator};

#[tokio::main]
async fn main() {
    let filters = vec!["RX 6800", "RX 6700"];

    loop {
        let products = get_html_products().await.unwrap();

        let filtered: Vec<Product> = products
            .into_iter()
            .filter(|el| {
                for s in &filters {
                    if el.name.contains(s) {
                        return el.available;
                    }
                }
                false
            })
            .collect();

        if filtered.len() > 0 {
            println!("The following products are available");
        } else {
            println!("Sorry, all products are out of stock. :/");
        }
    }
}

#[derive(Debug)]
pub struct Product {
    name: String,
    price: String,
    available: bool,
}

async fn get_html_products() -> Result<Vec<Product>, CmdError> {
    // navigate to amd-site
    let mut webc = ClientBuilder::native()
        .connect("http://localhost:4444")
        .await
        .expect("failed to connect to WebDriver");
    webc.goto("https://www.amd.com/de/direct-buy/de")
        .await
        .expect("Could not connect to the amd Website");

    // find all articles that are for sale
    let html_elements = webc
        .find_all(Locator::Css("article"))
        .await
        .expect("find_all error");

    //Interate over all items in the shop and parse the relevant strings
    // in the struct Product
    let mut products: Vec<Product> = Vec::new();
    for mut el in html_elements {
        let name = el.find(Locator::Css(".shop-title")).await?.text().await?;
        let price = el.find(Locator::Css(".shop-price")).await?.text().await?;
        let avail = el.find(Locator::Css(".shop-links")).await?.text().await?;

        let product = Product {
            name,
            price,
            available: !avail.contains("ergriffen"),
        };
        products.push(product);
    }

    // we do not need the webbrowser anymore
    webc.close()
        .await
        .expect("Failed to terminate webdriver sessions");
    return Ok(products);
}