use ratatui::style::Color;
use serde::Deserialize;
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct Config {
    pub colors: ColorConfig,
    pub auto_refresh_interval: u64,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct ColorConfig {
    pub background: String,
    pub foreground: String,
    pub header: String,
    pub selected_bg: String,
    pub selected_fg: String,
    pub marked: String,
    pub snapshot: String,
    pub size: String,
    pub tree: String,
    pub search_match: String,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            colors: ColorConfig::default(),
            auto_refresh_interval: 30,
        }
    }
}

impl Default for ColorConfig {
    fn default() -> Self {
        Self {
            background: "blue".to_string(),
            foreground: "white".to_string(),
            header: "yellow".to_string(),
            selected_bg: "cyan".to_string(),
            selected_fg: "black".to_string(),
            marked: "yellow".to_string(),
            snapshot: "lightcyan".to_string(),
            size: "lightgreen".to_string(),
            tree: "darkgray".to_string(),
            search_match: "lightred".to_string(),
        }
    }
}

impl Config {
    pub fn load() -> Self {
        // Try to load from ~/.config/zfsdu/config.toml
        if let Some(config_path) = Self::config_path() {
            if config_path.exists() {
                if let Ok(contents) = fs::read_to_string(&config_path) {
                    if let Ok(config) = toml::from_str(&contents) {
                        return config;
                    }
                }
            }
        }
        Self::default()
    }

    fn config_path() -> Option<PathBuf> {
        dirs::config_dir().map(|p| p.join("zfsdu").join("config.toml"))
    }

    pub fn example_config() -> String {
        r#"# zfsdu configuration file
# Place this file at ~/.config/zfsdu/config.toml

# Auto-refresh interval in seconds (0 to disable)
auto_refresh_interval = 30

[colors]
# Available colors: black, red, green, yellow, blue, magenta, cyan, white
# Light variants: lightred, lightgreen, lightyellow, lightblue, lightmagenta, lightcyan, gray
# Also: darkgray

background = "blue"
foreground = "white"
header = "yellow"
selected_bg = "cyan"
selected_fg = "black"
marked = "yellow"
snapshot = "lightcyan"
size = "lightgreen"
tree = "darkgray"
search_match = "lightred"
"#.to_string()
    }
}

pub fn parse_color(name: &str) -> Color {
    match name.to_lowercase().as_str() {
        "black" => Color::Black,
        "red" => Color::Red,
        "green" => Color::Green,
        "yellow" => Color::Yellow,
        "blue" => Color::Blue,
        "magenta" => Color::Magenta,
        "cyan" => Color::Cyan,
        "white" => Color::White,
        "gray" | "grey" => Color::Gray,
        "darkgray" | "darkgrey" => Color::DarkGray,
        "lightred" => Color::LightRed,
        "lightgreen" => Color::LightGreen,
        "lightyellow" => Color::LightYellow,
        "lightblue" => Color::LightBlue,
        "lightmagenta" => Color::LightMagenta,
        "lightcyan" => Color::LightCyan,
        _ => Color::White,
    }
}
