/// Integration tests for 24-bit Color (RFC 54).
use lv_tui::prelude::*;
use lv_tui::style::Color;
use lv_tui::text::Span;

// ── Color construction ─────────────────────────────────────────

#[test]
fn color_rgb_constructs() {
    let c = Color::Rgb(255, 128, 0);
    match c {
        Color::Rgb(r, g, b) => {
            assert_eq!(r, 255);
            assert_eq!(g, 128);
            assert_eq!(b, 0);
        }
        _ => panic!("expected Rgb"),
    }
}

#[test]
fn color_indexed_constructs() {
    let c = Color::Indexed(196);
    match c {
        Color::Indexed(i) => assert_eq!(i, 196),
        _ => panic!("expected Indexed"),
    }
}

// ── Color::hex parsing ─────────────────────────────────────────

#[test]
fn color_hex_full() {
    let c = Color::hex("#ff0000").unwrap();
    assert_eq!(c, Color::Rgb(255, 0, 0));
}

#[test]
fn color_hex_no_hash() {
    let c = Color::hex("00ff00").unwrap();
    assert_eq!(c, Color::Rgb(0, 255, 0));
}

#[test]
fn color_hex_short() {
    let c = Color::hex("#f00").unwrap();
    assert_eq!(c, Color::Rgb(255, 0, 0));
}

#[test]
fn color_hex_white() {
    let c = Color::hex("#ffffff").unwrap();
    assert_eq!(c, Color::Rgb(255, 255, 255));
}

#[test]
fn color_hex_black() {
    let c = Color::hex("#000").unwrap();
    assert_eq!(c, Color::Rgb(0, 0, 0));
}

#[test]
fn color_hex_invalid() {
    assert!(Color::hex("not-a-color").is_none());
    assert!(Color::hex("#gggggg").is_none());
    assert!(Color::hex("").is_none());
}

// ── Color rgb() constructor ────────────────────────────────────

#[test]
fn color_rgb_method() {
    assert_eq!(Color::rgb(10, 20, 30), Color::Rgb(10, 20, 30));
}

#[test]
fn color_indexed_method() {
    assert_eq!(Color::indexed(42), Color::Indexed(42));
}

// ── Named colors still work ────────────────────────────────────

#[test]
fn named_colors_still_exist() {
    let colors = [
        Color::Black, Color::Red, Color::Green, Color::Blue,
        Color::Yellow, Color::Magenta, Color::Cyan, Color::White, Color::Gray,
    ];
    // Just verify they compile and are distinct from Rgb
    for c in &colors {
        match c {
            Color::Rgb(..) => panic!("named color should not be Rgb"),
            Color::Indexed(_) => panic!("named color should not be Indexed"),
            _ => {}
        }
    }
}

// ── Stylize with rgb and hex ───────────────────────────────────

#[test]
fn stylize_rgb_on_string() {
    let span: Span = "hello".rgb(255, 0, 0);
    assert_eq!(span.style.fg, Some(Color::Rgb(255, 0, 0)));
}

#[test]
fn stylize_on_rgb_on_string() {
    let span: Span = "world".on_rgb(0, 0, 255);
    assert_eq!(span.style.bg, Some(Color::Rgb(0, 0, 255)));
}

#[test]
fn stylize_hex_on_string() {
    let span: Span = "test".hex("#ff6600");
    assert_eq!(span.style.fg, Some(Color::Rgb(255, 102, 0)));
}

#[test]
fn stylize_on_hex_on_string() {
    let span: Span = "bg".on_hex("#333333");
    assert_eq!(span.style.bg, Some(Color::Rgb(51, 51, 51)));
}

// ── CellStyle with new colors ──────────────────────────────────

#[test]
fn cellstyle_supports_rgb_colors() {
    let cs = lv_tui::buffer::CellStyle {
        fg: Some(Color::Rgb(128, 64, 32)),
        bg: Some(Color::Indexed(240)),
        bold: false,
        italic: false,
        underline: false,
    };
    assert_eq!(cs.fg, Some(Color::Rgb(128, 64, 32)));
    assert_eq!(cs.bg, Some(Color::Indexed(240)));
}

// ── Style with new colors ──────────────────────────────────────

#[test]
fn style_supports_rgb() {
    let style = Style::default()
        .fg(Color::Rgb(100, 200, 50))
        .bg(Color::hex("#123456").unwrap());
    assert_eq!(style.fg, Some(Color::Rgb(100, 200, 50)));
    assert_eq!(style.bg, Some(Color::Rgb(0x12, 0x34, 0x56)));
}

// ── Render with RGB color (end-to-end via Pilot) ───────────────

#[test]
fn label_renders_with_rgb_color() {
    let text = Text::from(Line::from("hello".rgb(0, 255, 0).bold()));
    let pilot = Pilot::new(
        lv_tui::widgets::Label::new(text),
        20,
        3,
    );
    assert!(pilot.frame().cells.iter().any(|c| c.symbol != " "),
        "label should render with rgb color");
}

// ── Equality and Debug ─────────────────────────────────────────

#[test]
fn color_equality() {
    assert_eq!(Color::Rgb(255, 0, 0), Color::Rgb(255, 0, 0));
    assert_ne!(Color::Rgb(255, 0, 0), Color::Rgb(0, 255, 0));
    assert_eq!(Color::hex("#ff0000").unwrap(), Color::Rgb(255, 0, 0));
    assert_eq!(Color::indexed(100), Color::Indexed(100));
}
