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

#[derive(Parser, Debug)]
#[command(
    name = "hoardom",
    version = "0.0.1",
    about = "Domain hoarding made less painful"
)] // static version infos ???? whoops
#[command(disable_help_flag = true, disable_version_flag = true)]
pub struct Args {
    /// ffs why were a million comments added at some point !?!? basically all of these are self explanatory. removed dumb ass redundant ones.

    #[arg(value_name = "DOMAIN")]
    pub domains: Vec<String>,
    #[arg(long = "cli", default_value_t = false)]
    pub cli_mode: bool,
    #[arg(long = "tui", default_value_t = false)]
    pub tui_mode: bool,
    #[arg(short = 'e', long = "environement")]
    pub env_path: Option<PathBuf>,
    #[arg(short = 'a', long = "all", default_value_t = false)]
    pub show_all: bool,
    #[arg(short = 'c', long = "csv")]
    pub csv: Option<Option<PathBuf>>,
    #[arg(short = 'l', long = "list")]
    pub tld_list: Option<String>,
    #[arg(short = 'i', long = "import-list", alias = "import-filter")]
    pub import_list: Option<PathBuf>,
    #[arg(short = 't', long = "top", value_delimiter = ',')]
    pub top_tlds: Option<Vec<String>>,
    #[arg(short = 'o', long = "onlytop", value_delimiter = ',')]
    pub only_top: Option<Vec<String>>,
    #[arg(short = 's', long = "suggestions")]
    pub suggestions: Option<usize>,
    #[arg(short = 'j', long = "jobs")]
    pub jobs: Option<u8>,
    #[arg(short = 'D', long = "delay")]
    pub delay: Option<f64>,
    #[arg(short = 'R', long = "retry")]
    pub retry: Option<u32>,
    #[arg(short = 'V', long = "verbose", default_value_t = false)]
    pub verbose: bool,
    /// search for names/domains in a text file line by 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,
    #[arg(short = 'M', long = "no-mouse", default_value_t = false)]
    pub no_mouse: bool,
    #[arg(short = 'r', long = "refresh-cache", default_value_t = false)]
    pub refresh_cache: bool,
    #[arg(short = 'h', long = "help", default_value_t = false)]
    pub help: bool,
    #[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 :
-a --all                         Show all in list even when unavailable
-H --fullhelp                    Show full help

Example usage :
hoardom --tui                    Launch Terminal UI.
hoardom idea.com                 See if idea.com is available
hoardom -a idea1 idea2           See Table of available domains starting with that


",
        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 :
-a --all                         Show all in list even when unavailable
-c --csv=PATH                    Out in CSV, Path is optional
-l --list=LIST                   Built in TLD Lists are : {}

Advanced :
-e --environement=PATH           Define where .hoardom folder should be
                                 Defaults to /home/USER/.hoardom/
                                 Stores settings, imported lists, favs, cache etc.
-i --import-list=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: 32)
-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
-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(", ")
    );
}
