extern crate html5ever;

use std::time::Instant;

use db::{connect, Website};
use parser::parse;
use s3::S3;
use surrealdb::{engine::remote::ws::Client, Surreal};
use tokio::sync::broadcast::Receiver;
use tracing::{debug, info, trace, trace_span};
use tracing_subscriber::EnvFilter;
use url::Url;

mod db;
mod parser;
mod s3;

struct Config<'a> {
    surreal_ns: &'a str,
    surreal_db: &'a str,
    surreal_url: &'a str,
    surreal_username: &'a str,
    surreal_password: &'a str,

    s3_url: &'a str,
    s3_bucket: &'a str,
    s3_access_key: &'a str,
    s3_secret_key: &'a str,
}

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .with_line_number(true)
        .without_time()
        .init();
    debug!("Starting...");

    let config = Config {
        surreal_url: "localhost:8000",
        surreal_username: "root",
        surreal_password: "root",
        surreal_ns: "test",
        surreal_db: "v1.9",
        s3_bucket: "v1.9",
        s3_url: "http://localhost:9000",
        s3_access_key: "0zv7GbLQsw4ZI8TclMps",
        s3_secret_key: "5dB7QkGFw7fYbUJ5LpHk2GbWR7Bl710HlRz4NbzB",
    };

    // Would probably take these in as parameters from a cli
    // let starting_url = "https://oliveratkinson.net/";
    let starting_url = "https://en.wikipedia.org/wiki/Main_Page";

    let s3 = S3::connect(&config)
        .await
        .expect("Failed to connect to minio, aborting.");
    let db = connect(&config)
        .await
        .expect("Failed to connect to surreal, aborting.");

    let mut site = spider::website::Website::new(&starting_url)
        .with_limit(4)
        .with_depth(0)
        .build()
        .unwrap();

    let mut rx: Receiver<spider::page::Page> = site.subscribe(0).unwrap();

    let subscriber = tokio::spawn(async move {
        let span = trace_span!("Sub");
        let span = span.enter();
        while let Ok(res) = rx.recv().await {
            // Get body
            let data = res.get_html();
            let url = Url::parse(res.get_url()).unwrap();

            trace!("Got '{}'", url.to_string());
            // Store document
            s3.store(&data, &url).await;

            // Parse document and store relationships
            let mut page = Website::new(url, true);
            page.store(&db).await;

            // Relate this page to all the pages it links to
            let span = trace_span!("Linking");
            let span = span.enter();
            let found_links = parse( &page, data).await;
            let mut stored_links = Vec::new();
            for mut link in found_links {
                if let Some(id) = link.store(&db).await {
                    stored_links.push(id);
                }
            }
            page.links_to(stored_links, &db).await;
            drop(span);
        }
        drop(span);
    });

    let timer = Timer::start("Crawled");

    site.crawl().await;
    site.unsubscribe();

    drop(timer);

    subscriber.await.unwrap();

    info!("Done");
}

/// Returns uncrawled links
async fn get_uncrawled_links(db: &Surreal<Client>, mut count: usize) -> Vec<Website> {
    if count > 100 {
        count = 100
    }

    let mut response = db
        .query("SELECT * FROM website WHERE crawled = false LIMIT $count")
        .bind(("count", count))
        .await
        .expect("Hard-coded query failed..?");
    response
        .take(0)
        .expect("Returned websites couldn't be parsed")
}

pub struct Timer<'a> {
    start: Instant,
    msg: &'a str,
}

impl<'a> Timer<'a> {
    #[inline]
    pub fn start(msg: &'a str) -> Self {
        Self {
            start: Instant::now(),
            msg,
        }
    }
    pub fn stop(&self) -> f64 {
        let dif = self.start.elapsed().as_micros();
        let ms = dif as f64 / 1000.;
        debug!("{}", format!("{} in {:.3}ms", self.msg, ms));
        ms
    }
}

impl Drop for Timer<'_> {
    fn drop(&mut self) {
        self.stop();
    }
}
