use std::{
    thread::available_parallelism, process::exit,
};

use tokio::{net::TcpListener, signal::unix::{SignalKind, signal}, runtime, select};

use crate::{
    handlers::handlers::handle_connection,
    handling::routes::{Route, Uri},
};

#[derive(Clone)]
pub struct MountPoint<'a> {
    pub mountpoint: Uri<'a>,
    pub routes: Vec<Route<'a>>,
}

pub struct Config {
    mountpoints: Option<Vec<MountPoint<'static>>>,
    address: TcpListener,
}

impl<'a> Config {
    pub fn mount(mut self, mountpoint: Uri<'static>, routes: Vec<Route<'static>>) -> Config {
        let mut mount_message = format!("  >> \x1b[35m{}\x1b[0m\n", mountpoint);
        for (index, route) in routes.iter().enumerate() {
            let indent_sign;
            match index {
                i if i == routes.len() - 1 => indent_sign = "└─",
                _ => indent_sign = "├─",
            };

            mount_message += &format!(
            "     \x1b[35m{indent_sign}\x1b[0m \x1b[36m(\x1b[0m{}\x1b[36m)\x1b[0m \x1b[32m{}\x1b[0m \x1b[34;4m{}\x1b[24m{}\x1b[0m\n",
                route.name.unwrap_or(""),
                route.method,
                mountpoint,
                route.uri
            )
        }

        println!("{mount_message}");
        let mut temp_mountpoints = None;
        std::mem::swap(&mut self.mountpoints, &mut temp_mountpoints);

        if let Some(mut mountpoints) = temp_mountpoints {
            mountpoints.push(MountPoint { mountpoint, routes });
            self.mountpoints = Some(mountpoints);
        } else {
            self.mountpoints = Some(vec![MountPoint { mountpoint, routes }]);
        }
        self
    }
    pub async fn launch(self) {
        let mut sigint = signal(SignalKind::interrupt()).unwrap();
        loop {
            select! {
                _ = sigint.recv() => {
                    println!("Shutting down...");
                    break;
                }

                income = self.address.accept() => {
                    let income = income.unwrap();
                    let (socket, _) = income;
                    let mountpoints = self.mountpoints.clone().unwrap();
                    tokio::spawn(async move { handle_connection(socket, mountpoints).await; });
                }
            }
        }
    }
}
pub async fn build(ip: &str) -> Config {
    let listener = TcpListener::bind(ip).await.unwrap();
    let ip = ip.splitn(2, ":").collect::<Vec<&str>>();
    if ip.len() != 2 {
        panic!("Invalid IP Address");
    }
    let port = ip[1];
    let ip = ip[0];
    let workers = available_parallelism().unwrap().get();
    println!(
        "\x1b[34m⚙ Configuration\x1b[0m
  >> \x1b[34mIp\x1b[0m: {ip}
  >> \x1b[34mPort\x1b[0m: {port}
  >> \x1b[34mWorkers\x1b[0m: {workers}
\x1b[35m🛪 Mountpoints\x1b[0m"
    );
    Config {
        mountpoints: None,
        address: listener,
    }
}