use crate::common::args::Cli;
use clap::ValueEnum;
use std::fmt::Display;
#[derive(Debug, Clone, Copy)]
pub enum Mode {
    Tcp,
}

impl ValueEnum for Mode {
    fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
        Some(match self {
            Self::Tcp => clap::builder::PossibleValue::new("tcp"),
        })
    }
    fn value_variants<'a>() -> &'a [Self] {
        &[Self::Tcp]
    }
    fn from_str(input: &str, ignore_case: bool) -> Result<Self, String> {
        let comp: String = if ignore_case {
            input.to_lowercase()
        } else {
            input.to_string()
        };
        match comp.as_str() {
            "tcp" => return Ok(Self::Tcp),
            _ => return Err(format!("\"{input}\" is not a valid mode")),
        }
    }
}

impl Display for Mode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let repr: String = match self {
            Self::Tcp => format!("tcp"),
        };
        write!(f, "{}", repr)
    }
}

pub struct Config {
    pub addr: std::net::SocketAddr,
    pub mode: Mode,
    pub threads: usize,
}

impl Config {
    pub fn new(cli: &Cli) -> Self {
        Config {
            addr: cli.addr.clone(),
            mode: cli.mode.clone(),
            threads: cli.threads,
        }
    }
}