/// RFC 06 验收 example — 边界情况压力测试
///
/// 验证：
/// - 超长文本截断
/// - 空字符串渲染不 panic
/// - 各种按键处理（已知/未知）
/// - Column 空 children 不 panic
/// - 嵌套 Column 布局

use lv_tui::prelude::*;
use lv_tui::Component;
use lv_tui::widgets::{Column, Label};

#[derive(Component)]
struct EdgeCase {
    #[reactive(paint)]
    count: i32,

    /// 超长文本用于测试 clip 截断
    long_text: String,
}

impl EdgeCase {
fn new() -> Self {
        Self {
            count: 0,
            long_text: "A".repeat(500),
        }
    }
}

impl Component for EdgeCase {
fn render(&self, cx: &mut RenderCx) {
        // 空字符串渲染 — 不应 panic
        cx.line("");

        // 正常文本
        cx.line(format!("count: {} (press + to increment)", self.count()));

        // 超长文本 — 应在 clip 边界截断
        cx.line(&self.long_text);

        // 空字符串再次测试
        cx.text("");

        cx.line("press arrows/enter/esc/tab to test keys, q to quit");
    }
fn event(&mut self, event: &Event, cx: &mut EventCx) {
        if cx.phase() != lv_tui::event::EventPhase::Target { return; }
        match event {
            Event::Key(key_event) => match &key_event.key {
                Key::Char('+') => self.update_count(cx, |n| *n += 1),
                Key::Char('q') => cx.quit(),
                // 测试各种已知 key 不 panic
                Key::Up
                | Key::Down
                | Key::Left
                | Key::Right
                | Key::Enter
                | Key::Esc
                | Key::Tab
                | Key::Backspace
                | Key::Delete
                | Key::Home
                | Key::End
                | Key::PageUp
                | Key::PageDown => {
                    // 接受但忽略
                }
                Key::Char(_) => {
                    // 未知 char key 也忽略
                }
            },
            Event::Resize(_) => {
                // resize 由 Runtime 处理
            }
            _ => {} // mouse / tick 忽略
        }
    }
}

fn main() -> lv_tui::Result<()> {
    App::new(
        Column::new()
            .padding(1)
            .gap(1)
            .child(Label::new("=== Edge Case Test ==="))
            .child(EdgeCase::new()),
    )
    .run()
}
