use clap::Parser;
use std::path::PathBuf;

#[derive(Parser, Debug)]
#[command(name = "hoardom", version = "0.0.1", about = "Domain hoarding made less painful")]
#[command(disable_help_flag = true, disable_version_flag = true)]
pub struct Args {
    /// One or more domain names to search for
    #[arg(value_name = "DOMAIN")]
    pub domains: Vec<String>,

    // -- Mode --
    /// Default non interactive mode
    #[arg(long = "cli", default_value_t = false)]
    pub cli_mode: bool,

    /// Easy to use Terminal based Graphical user interface
    #[arg(long = "tui", default_value_t = false)]
    pub tui_mode: bool,

    // -- Basics --
    /// Define where environement file should be saved
    #[arg(short = 'e', long = "environement")]
    pub env_path: Option<PathBuf>,

    /// Show all in list even when unavailable
    #[arg(short = 'a', long = "all", default_value_t = false)]
    pub show_all: bool,

    // -- Advanced --
    /// Out in CSV, Path is optional. If path isnt given will be printed to terminal with no logs
    #[arg(short = 'c', long = "csv")]
    pub csv: Option<Option<PathBuf>>,

    /// Built in TLD list to use (from Lists.toml)
    #[arg(short = 'l', long = "list")]
    pub tld_list: Option<String>,

    /// Import a custom toml list for this session
    #[arg(short = 'i', long = "import-filter")]
    pub import_filter: Option<PathBuf>,

    /// Set certain TLDs to show up as first result (comma separated)
    #[arg(short = 't', long = "top", value_delimiter = ',')]
    pub top_tlds: Option<Vec<String>>,

    /// Only search these TLDs (comma separated)
    #[arg(short = 'o', long = "onlytop", value_delimiter = ',')]
    pub only_top: Option<Vec<String>>,

    /// How many suggestions to look up and try to show (defaults to 0 aka disabled)
    #[arg(short = 's', long = "suggestions")]
    pub suggestions: Option<usize>,

    // -- Various --
    /// Number of concurrent lookup requests (default: 1)
    #[arg(short = 'j', long = "jobs")]
    pub jobs: Option<u8>,

    /// Set the global delay in seconds between lookup requests
    #[arg(short = 'D', long = "delay")]
    pub delay: Option<f64>,

    /// Retry NUMBER amount of times if domain lookup errors out
    #[arg(short = 'R', long = "retry")]
    pub retry: Option<u32>,

    /// Verbose output for debugging
    #[arg(short = 'V', long = "verbose", default_value_t = false)]
    pub verbose: bool,

    /// Search for names/domains in text file, one domain per new line
    #[arg(short = 'A', long = "autosearch")]
    pub autosearch: Option<PathBuf>,

    /// Use a monochrome color scheme
    #[arg(short = 'C', long = "no-color", default_value_t = false)]
    pub no_color: bool,

    /// Do not use unicode only plain ASCII
    #[arg(short = 'U', long = "no-unicode", default_value_t = false)]
    pub no_unicode: bool,

    /// Disable the mouse integration for TUI
    #[arg(short = 'M', long = "no-mouse", default_value_t = false)]
    pub no_mouse: bool,

    /// Force refresh the RDAP bootstrap cache
    #[arg(short = 'r', long = "refresh-cache", default_value_t = false)]
    pub refresh_cache: bool,

    /// Basic Help
    #[arg(short = 'h', long = "help", default_value_t = false)]
    pub help: bool,

    /// Show full help
    #[arg(short = 'H', long = "fullhelp", default_value_t = false)]
    pub fullhelp: bool,
}

impl Args {
    pub fn is_tui(&self) -> bool {
        self.tui_mode
    }

    pub fn effective_list(&self) -> String {
        self.tld_list
            .clone()
            .map(|s| s.to_lowercase())
            .unwrap_or_else(|| crate::tlds::default_list_name().to_string())
    }

    pub fn effective_suggestions(&self) -> usize {
        self.suggestions.unwrap_or(0)
    }

    pub fn effective_retry(&self) -> u32 {
        self.retry.unwrap_or(1)
    }

    pub fn effective_delay(&self) -> f64 {
        self.delay.unwrap_or(0.0)
    }

    pub fn effective_jobs(&self) -> u8 {
        self.jobs.unwrap_or(1).max(1)
    }
}

pub fn print_help() {
    println!(
        "hoardom {} - Domain hoarding made less painful
Mode :
--cli                            Default none interactive mode
--tui                            Easy to use Terminal based Graphical user interface

Basics :
-e --environement=PATH           Define where .hoardom folder should be
                                 Defaults to /home/USER/.hoardom/
                                 Stores settings, imported lists, favs, cache etc.
-a --all                         Show all in list even when unavailable
                                 (Unless changed after launch in TUI mode)

-H --fullhelp                    Show full help",
        env!("CARGO_PKG_VERSION")
    );
}

pub fn print_fullhelp() {
    println!(
        "hoardom {} - they are inside your walls! 
        
## if you see this send a fax to +41 43 543 04 47 that mentions hoardom to know your fate ##

Mode :
--cli                            Default none interactive mode
--tui                            Easy to use Terminal based Graphical user interface

Basics :
-e --environement=PATH           Define where .hoardom folder should be
                                 Defaults to /home/USER/.hoardom/
                                 Stores settings, imported lists, favs, cache etc.
-a --all                         Show all in list even when unavailable
                                 (Unless changed after launch in TUI mode)

Advanced :
-c --csv=PATH                    Out in CSV,Path is optional
                                 if path isnt given will be printed to terminal with no logs
-l --list=LIST                   Built in TLD Lists are : {}
                                 Selects which list is applied
                                 (Unless changed after launch in TUI mode)
-i --import-filter=PATH          Import a custom toml list for this session
-t --top=TLD,TLD                 Set certain TLDs to show up as first result
                                 for when you need a domain in your country or for searching
                                 a specific one.
                                 (Unless changed after launch in TUI mode)
-o --onlytop=TLD,TLD             Only search these TLDs
-s --suggestions=NUMBER          How many suggestions to look up and try to show from list
                                 Number of available alternativ domains to try and find when
                                 Searching for full domain name directly.
                                 Defaults to 0 (aka disabled)

Various :
-j --jobs=NUMBER                 Number of concurrent lookup requests
                                 How many TLDs to look up at the same time (default: 1)
-D --delay=DELAY                 Set the global delay in Seconds between lookup requests
-R --retry=NUMBER                Retry NUMBER amount of times if domain lookup errors out
-V --verbose                     Verbose output for debugging
-A --autosearch=FILE             Search for names/domains in text file one domain per new line,
                                 lines starting with invalid character for a domain are ignored
                                 (allows for commenting)
-C --no-color                    Use a monochrome color scheme
-U --no-unicode                  Do not use unicode only plain ASCII
-M --no-mouse                    Disable the mouse integration for TUI
-r --refresh-cache               Force refresh the RDAP bootstrap cache

Help :
-h --help                        Basic Help
-H --fullhelp                    You just did this",
        env!("CARGO_PKG_VERSION"),
        crate::tlds::list_names().join(", ")
    );
}
