use ratatui::style::{Color, Modifier, Style};
use ratatui::widgets::{Block, Borders};

// Catppuccin-inspired palette — high contrast on dark terminals
pub const BG: Color = Color::Rgb(14, 15, 20);
pub const SURFACE: Color = Color::Rgb(32, 33, 39);
pub const SURFACE_ALT: Color = Color::Rgb(22, 24, 32);
pub const ACCENT: Color = Color::Rgb(137, 220, 235); // #89DCEB sky
pub const SAPPHIRE: Color = Color::Rgb(116, 199, 236); // #74C7EC secondary accent
pub const GREEN: Color = Color::Rgb(166, 227, 161); // #A6E3A1
pub const RED: Color = Color::Rgb(243, 139, 168); // #F38BA8
pub const YELLOW: Color = Color::Rgb(249, 226, 175); // #F9E2AF
pub const TEXT_HIGH: Color = Color::Rgb(244, 244, 246);
pub const TEXT_MED: Color = Color::Rgb(150, 159, 175);
pub const TEXT_DIM: Color = Color::Rgb(130, 135, 155);
pub const BORDER_COLOR: Color = Color::Rgb(55, 57, 65);
pub const BORDER_FOCUS: Color = Color::Rgb(116, 199, 236); // #74C7EC

pub fn title() -> Style {
    Style::default().fg(ACCENT).add_modifier(Modifier::BOLD)
}

pub fn bid() -> Style {
    Style::default().fg(GREEN).add_modifier(Modifier::BOLD)
}

pub fn ask() -> Style {
    Style::default().fg(RED).add_modifier(Modifier::BOLD)
}

pub fn header() -> Style {
    Style::default().fg(TEXT_DIM)
}

pub fn normal() -> Style {
    Style::default().fg(TEXT_HIGH)
}

pub fn dim() -> Style {
    Style::default().fg(TEXT_DIM)
}

pub fn secondary() -> Style {
    Style::default().fg(TEXT_MED)
}

pub fn highlight() -> Style {
    Style::default().fg(ACCENT).add_modifier(Modifier::BOLD)
}

pub fn warning() -> Style {
    Style::default().fg(YELLOW)
}

pub fn border(active: bool) -> Style {
    if active {
        Style::default().fg(BORDER_FOCUS)
    } else {
        Style::default().fg(BORDER_COLOR)
    }
}

pub fn status_ok() -> Style {
    Style::default().fg(GREEN)
}

pub fn status_err() -> Style {
    Style::default().fg(RED)
}

pub fn status_warn() -> Style {
    Style::default().fg(YELLOW)
}

/// Extract RGB components from a Color::Rgb for gradient calculations.
pub fn rgb_components(color: Color) -> (u8, u8, u8) {
    match color {
        Color::Rgb(r, g, b) => (r, g, b),
        _ => (255, 255, 255),
    }
}

/// Active panel subtle background tint
pub const BG_ACTIVE: Color = Color::Rgb(20, 22, 30);

/// Reusable panel block builder — top border only, focused panel gets bright title
pub fn panel_block(title: &str, active: bool) -> Block<'_> {
    let title_style = if active {
        Style::default().fg(ACCENT).add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(TEXT_MED)
    };
    let border_style = if active {
        Style::default().fg(SAPPHIRE)
    } else {
        Style::default().fg(BORDER_COLOR)
    };
    let bg = if active { BG_ACTIVE } else { BG };
    Block::default()
        .title(format!(" {title} "))
        .title_style(title_style)
        .borders(Borders::TOP)
        .border_style(border_style)
        .style(Style::default().bg(bg))
}

/// Panel block with left border — for right-column and bottom-strip panels to create visual dividers
pub fn panel_block_divided(title: &str, active: bool) -> Block<'_> {
    let title_style = if active {
        Style::default().fg(ACCENT).add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(TEXT_MED)
    };
    let border_style = if active {
        Style::default().fg(SAPPHIRE)
    } else {
        Style::default().fg(BORDER_COLOR)
    };
    let bg = if active { BG_ACTIVE } else { BG };
    Block::default()
        .title(format!(" {title} "))
        .title_style(title_style)
        .borders(Borders::TOP | Borders::LEFT)
        .border_style(border_style)
        .style(Style::default().bg(bg))
}

/// Panel block with left border and info suffix
pub fn panel_block_divided_with_info<'a>(title: &'a str, info: &'a str, active: bool) -> Block<'a> {
    use ratatui::text::{Line, Span};
    let title_style = if active {
        Style::default().fg(ACCENT).add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(TEXT_MED)
    };
    let border_style = if active {
        Style::default().fg(SAPPHIRE)
    } else {
        Style::default().fg(BORDER_COLOR)
    };
    let bg = if active { BG_ACTIVE } else { BG };
    Block::default()
        .title(Line::from(vec![
            Span::styled(format!(" {title} "), title_style),
            Span::styled(info.to_string(), Style::default().fg(TEXT_DIM)),
        ]))
        .borders(Borders::TOP | Borders::LEFT)
        .border_style(border_style)
        .style(Style::default().bg(bg))
}

/// Panel block with info suffix (e.g., count, scroll position)
pub fn panel_block_with_info<'a>(title: &'a str, info: &'a str, active: bool) -> Block<'a> {
    use ratatui::text::{Line, Span};
    let title_style = if active {
        Style::default().fg(ACCENT).add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(TEXT_MED)
    };
    let border_style = if active {
        Style::default().fg(SAPPHIRE)
    } else {
        Style::default().fg(BORDER_COLOR)
    };
    let bg = if active { BG_ACTIVE } else { BG };
    Block::default()
        .title(Line::from(vec![
            Span::styled(format!(" {title} "), title_style),
            Span::styled(info.to_string(), Style::default().fg(TEXT_DIM)),
        ]))
        .borders(Borders::TOP)
        .border_style(border_style)
        .style(Style::default().bg(bg))
}
