// module to handle pre-boot params of the monitor
#[allow(unused_imports)]
use anyhow::{Result, Ok, Error};
use clap::Parser;
use std::path::PathBuf;
use std::env::var;
use dotenv::dotenv;

const SOCKET_PATH: &str = "/var/run/enode/hostagent.sock";

///
enum EnvVars {
    NoxisNoHagent,
    NoxisNoLogs,
    NoxisRefreshLogs,
    NoxisNoRemoteConfig,
    NoxisNoConfigSub,
    NoxisSocketPath,
    NoxisLogTo,
    NoxisRemoteServerUrl,
    NoxisConfig,
    NoxisMetrics,
}

///
impl std::fmt::Display for EnvVars {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            EnvVars::NoxisNoHagent => write!(f, "NOXIS_NO_HAGENT"),
            EnvVars::NoxisNoLogs => write!(f, "NOXIS_NO_LOGS"),
            EnvVars::NoxisRefreshLogs => write!(f, "NOXIS_REFRESH_LOGS"),
            EnvVars::NoxisNoRemoteConfig => write!(f, "NOXIS_NO_REMOTE_CONFIG"),
            EnvVars::NoxisNoConfigSub => write!(f, "NOXIS_NO_CONFIG_SUB"),
            EnvVars::NoxisSocketPath => write!(f, "NOXIS_SOCKET_PATH"),
            EnvVars::NoxisLogTo => write!(f, "NOXIS_LOG_TO"),
            EnvVars::NoxisRemoteServerUrl => write!(f, "NOXIS_REMOTE_SERVER_URL"),
            EnvVars::NoxisConfig => write!(f, "NOXIS_CONFIG"),
            EnvVars::NoxisMetrics => write!(f, "NOXIS_METRICS"),
        }
    }
}

///
impl<'a> EnvVars {
    // Default trait func is not satisfying this issue
    fn default(self) -> &'a str {
        match self {
            EnvVars::NoxisNoHagent => "false",
            EnvVars::NoxisNoLogs => "false",
            EnvVars::NoxisRefreshLogs => "false",
            EnvVars::NoxisNoRemoteConfig => "false",
            EnvVars::NoxisNoConfigSub => "false",
            EnvVars::NoxisSocketPath => "/var/run/enode/hostagent.sock",
            EnvVars::NoxisLogTo => "./",
            EnvVars::NoxisRemoteServerUrl => "localhost",
            EnvVars::NoxisConfig => "./settings.json",
            EnvVars::NoxisMetrics => "full",
        }
    }
    fn process_env_var(self, preboot_value: &str) {
        // let default = self.default();
        match var(self.to_string()) {
            std::result::Result::Ok(val) => {
                if val != preboot_value {
                    std::env::set_var(self.to_string(), self.default());
                }
            },
            Err(_) => {
                std::env::set_var(self.to_string(), preboot_value);
            },
        }
    }
    pub fn setup(preboot: &PrebootParams) {
        // setup default if not exists
        //  check values and save preboot states in env vars if not equal

        Self::NoxisNoHagent.process_env_var(&preboot.no_hostagent.to_string());
        Self::NoxisNoLogs.process_env_var(&preboot.no_logs.to_string());
        Self::NoxisRefreshLogs.process_env_var(&preboot.refresh_logs.to_string());
        Self::NoxisNoRemoteConfig.process_env_var(&preboot.no_remote_config.to_string());
        Self::NoxisNoConfigSub.process_env_var(&preboot.no_sub.to_string());
        Self::NoxisSocketPath.process_env_var(preboot.socket_path.to_str().unwrap());
        Self::NoxisLogTo.process_env_var(preboot.log_to.to_str().unwrap());
        Self::NoxisRemoteServerUrl.process_env_var(&preboot.remote_server_url);
        Self::NoxisConfig.process_env_var(preboot.config.to_str().unwrap());
        Self::NoxisMetrics.process_env_var(&preboot.metrics.to_string());

    }
}

/// # Enum `MetricsPrebootParams`
/// ## for setting up metrics mode as preboot param from command prompt 
/// 
/// examples:
/// ``` bash
/// noxis-rs ... --metrics full 
/// noxis-rs ... --metrics system 
/// noxis-rs ... --metrics processes
/// noxis-rs ... --metrics net
/// noxis-rs ... --metrics none 
/// ```
/// 
#[derive(clap::ValueEnum, Debug, Clone)]
pub enum MetricsPrebootParams {
    Full, 
    System,
    Processes,
    Net,
    None,
}

/// # `std::fmt::Display` implementation for `MetricsPrebootParams`
/// ## to enable parsing object to String 
impl std::fmt::Display for MetricsPrebootParams { 
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 
        match self {
            MetricsPrebootParams::Full => write!(f, "full"),
            MetricsPrebootParams::System => write!(f, "system"),
            MetricsPrebootParams::Processes => write!(f, "processes"),
            MetricsPrebootParams::Net => write!(f, "net"),
            MetricsPrebootParams::None => write!(f, "none"),
        }
    } 
} 

/// # struct `PrebootParams`
/// ## to parse and set up all modes as preboot params from command prompt 
/// 
/// ### args :
/// 
/// `--no-hagent` - to disable hagent work module and set up work mode as autonomous
/// ### usage :
/// ``` bash
/// noxis-rs ... --no-hagent ...
/// ```
/// 
/// 
/// `--no-logs` - to disable logging at all  
/// ### usage :
/// ``` bash
/// noxis-rs ... --no-logs ...
/// ```
/// 
/// `--refresh-logs` - to truncate logs directory 
/// ### usage :
/// ``` bash
/// noxis-rs ... --refresh-logs ...
/// ```
/// 
/// `--no-remote-config` - to disable work with Redis as config producer 
/// ### usage :
/// ``` bash
/// noxis-rs ... --no-remote-config ...
/// ```
/// 
/// `--no-sub` - to disable Redis subscribtion mechanism  
/// ### usage :
/// ``` bash
/// noxis-rs ... --no-sub ...
/// ```
/// 
/// `--socket-path` - to set Unix Domain Socket file's directory
/// ### usage :
/// ``` bash
/// noxis-rs ... --socket-path /var/run/enode/hostagent.sock ...
/// ```
///
/// `--log-to` - to set directory for logs 
/// ### usage :
/// ``` bash
/// noxis-rs ... --log-to /dir/to/logs/ ...
/// ```
/// 
/// `--remote-server-url` - to set Redis Server
/// ### usage :
/// ``` bash
/// noxis-rs ... --remote-server-url 192.168.28.12 ...
/// ```
/// 
/// `--config` - to set Noxis' config full path
/// ### usage :
/// ``` bash
/// noxis-rs ... --config /etc/enode/settings.json ...
/// ```
/// 
/// `--metrics` - to set metrics mode 
/// ### usage :
/// ``` bash
/// noxis-rs ... --metrics full ...
/// ```
#[derive(Debug, Parser)]
pub struct PrebootParams {
    // actions
    #[arg(
        long = "no-hagent", 
        action, 
        conflicts_with="socket_path", 
        help="To disable work with host-agent"
    )]
    pub no_hostagent : bool,
    #[arg(
        long = "no-logs", 
        action, 
        conflicts_with="log_to", 
        help="To disable logs"
    )]
    pub no_logs: bool,
    #[arg(
        long = "refresh-logs", 
        action, 
        conflicts_with="no_logs", 
        help="To clear logs directory"
    )]
    pub refresh_logs : bool,
    #[arg(
        long = "no-remote-config", 
        action, 
        help="To disable work with remote config server", 
        conflicts_with="no_sub")]
    pub no_remote_config : bool,
    #[arg(
        long = "no-sub", 
        action, 
        help="To disable subscription mechanism", 
        conflicts_with="no_remote_config")]
    pub no_sub : bool,
    
    // params (socket_path, log_to, remote_server_url, config)
    #[arg(
        long = "socket-path", 
        default_value="/var/run/enode/hostagent.sock", 
        conflicts_with="no_hostagent", 
        help="To set .sock file's path used in communication with host-agent"
    )]
    pub socket_path : PathBuf,
    #[arg(
        long = "log-to", 
        default_value="./", 
        conflicts_with="no_logs", 
        help="To set a path to logs directory"
    )]
    pub log_to : PathBuf,
    #[arg(
        long = "remote-server-url", 
        default_value="localhost", 
        conflicts_with="no_remote_config", 
        help = "To set url of remote config server using in remote config pulling mechanism"
    )]
    pub remote_server_url : String,
    #[arg(
        long = "config", 
        short, 
        default_value="settings.json", 
        help="To set local config file path"
    )]
    pub config : PathBuf,

    // value enum params (metrics)
    #[arg(
        long = "metrics", 
        short, 
        default_value_t=MetricsPrebootParams::Full, 
        help="To set metrics grubbing mode"
    )]
    pub metrics: MetricsPrebootParams,
}

/// # implementation for `MetricsPrebootParams`
/// ## to enable validation mechanism
impl PrebootParams {
    pub fn validate(mut self) -> Result<Self> {
        dotenv().ok();
        if !self.socket_path.exists() && !self.no_hostagent {
            if self.socket_path.to_string_lossy() == SOCKET_PATH {
                self.no_hostagent = true;
                eprintln!("Warning: Socket-file wasn't found. Working without hostagent module...");
            } else {
                eprintln!("Warning: Socket-file wasn't found or Noxis can't read it. Socket-file was set to default");
                if !PathBuf::from(SOCKET_PATH).exists() {
                    self.no_hostagent = true;
                    eprintln!("Warning: Socket-file wasn't found. Working without hostagent module...");
                } else {
                    self.socket_path = PathBuf::from(SOCKET_PATH);
                }
            }
            // return Err(Error::msg("Socket-file not found or Noxis can't read it. Cannot start"));
        }
        // existing log dir
        if !self.log_to.exists() && !self.no_logs {
            eprintln!("Error: Log-Dir not found or Noxis can't read it. LogDir was set to default");
            self.log_to = PathBuf::from("./");
            // return Err(Error::msg("Log Directory Not Found or Noxis can't read it. Cannot start"));
        }
        // existing sock file
        if !self.config.exists() {
            eprintln!("Error: Invalid character in config file. Config path was set to default");
            let config = PathBuf::from("/etc/settings.json");
            if !config.exists() && self.no_remote_config {
                return Err(Error::msg("Noxis cannot run without config. Create local config or enable remote-config mechanism"));
            }
            self.config = PathBuf::from("settings.json");
            // return Err(Error::msg("Local Config Not Found or Noxis can't read it. Cannot start"));
        }
        // redis server check
        EnvVars::setup(&self);
        Ok(self)
    }
}


// unit tests of preboot params parsing mech
#[cfg(test)]
mod preboot_unitests{
    use super::*;

    #[test]
    fn parsing_zero_args() {
        assert!(PrebootParams::try_parse_from(vec!["runner-rs"]).is_ok())
    }
    #[test]
    fn parsing_hagent_valid_args() {
        assert!(PrebootParams::try_parse_from(vec![
            "runner-rs", 
            "--socket-path", "/path/to/socket"
        ]).is_ok())    
}
    #[test]
    fn parsing_hagent_invalid_args() {
        assert!(PrebootParams::try_parse_from(vec![
            "runner-rs", 
            "--socket-path", "/path/to/socket", 
            "--no-hagent"
        ]).is_err())    
    }
    #[test]
    fn parsing_log_valid_args() {
        assert!(PrebootParams::try_parse_from(vec![
            "runner-rs", 
            "--log-to", "/path/to/log/dir"
        ]).is_ok())    
    }
    #[test]
    fn parsing_log_invalid_args() {
        assert!(PrebootParams::try_parse_from(vec![
            "runner-rs", 
            "--log-to /path/to/log/dir", 
            "--no-logs"
        ]).is_err())
    }
    #[test]
    fn parsing_config_valid_args() {
        assert!(PrebootParams::try_parse_from(vec![
            "runner-rs", 
            "--no-sub", 
            "--remote-server-url", "redis://127.0.0.1"
        ]).is_ok())    
    }
    #[test]
    fn parsing_config_invalid_args_noremote_nosub() {
        assert!(PrebootParams::try_parse_from(vec![
            "runner-rs", 
            "--no-remote-config", "--no-sub"
        ]).is_err())    
    }
    #[test]
    fn parsing_config_invalid_args_noremote_remoteurl() {
        assert!(PrebootParams::try_parse_from(vec![
            "runner-rs", 
            "--no-remote-config", 
            "--remote-server-url", "redis://127.0.0.1"
        ]).is_err())    
    }
    #[test]
    fn parsing_metrics_args_using_value_enum() {
        assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "full"]).is_ok());
        assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "system"]).is_ok());
        assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "processes"]).is_ok());
        assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "net"]).is_ok());
        assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "none"]).is_ok());
        assert!(PrebootParams::try_parse_from(vec!["runner-rs", "--metrics", "unusual_value"]).is_err());
    }
}