use std::cell::RefCell;
use tempfile::tempdir;
use portable_pty::{native_pty_system, PtySize};
use vt100::Parser;

use oo_ide::views::terminal::{TerminalView, TerminalTab};
use oo_ide::widgets::terminal::{Link, LinkKind};
use oo_ide::input::{MouseEvent, MouseEventKind, MouseButton, Modifiers};
use oo_ide::operation::Operation;
use oo_ide::views::View;

#[test]
fn click_wrapped_url_emits_openurl_op() {
    let mut tv = TerminalView::new();
    let dir = tempdir().unwrap();
    let pty = native_pty_system();
    let pair = pty.openpty(PtySize { rows: 10, cols: 80, pixel_width: 0, pixel_height: 0 }).unwrap();
    let writer = pair.master.take_writer().unwrap();
    let parser = RefCell::new(Parser::new(2, 18, 100));
    parser.borrow_mut().process(b"http://example.com\n");

    let tab = TerminalTab {
        id: 1,
        title: "t".into(),
        command: "cmd".into(),
        cwd: dir.path().to_path_buf(),
        master: pair.master,
        writer,
        parser,
        links: vec![Link {
            kind: LinkKind::Url("http://example.com".to_string()),
            row: 0,
            start_col: 0,
            end_col: 18,
            text: "http://example.com".to_string(),
        }],
        scroll_offset: 0,
        scrollback_len: 100,
        exited: false,
        scrollback_lines: vec![],
    };
    tv.tabs.push(tab);
    tv.active = 0;

    let mouse = MouseEvent { kind: MouseEventKind::Down(MouseButton::Left), column: 1, row: 0, modifiers: Modifiers::empty() };
    let ops = tv.handle_mouse(mouse);
    assert!(ops.iter().any(|op| matches!(op, Operation::OpenUrl { url } if url == "http://example.com")));
}

#[test]
fn click_file_link_with_line_number_emits_openfile_and_jumpto() {
    let mut tv = TerminalView::new();
    let dir = tempdir().unwrap();
    let file = dir.path().join("main.rs");
    std::fs::write(&file, "fn main() {\n    println!(\"hello\");\n}\n").unwrap();
    
    let pty = native_pty_system();
    let pair = pty.openpty(PtySize { rows: 10, cols: 80, pixel_width: 0, pixel_height: 0 }).unwrap();
    let writer = pair.master.take_writer().unwrap();
    let parser = RefCell::new(Parser::new(10, 80, 100));
    
    let tab = TerminalTab {
        id: 1,
        title: "t".into(),
        command: "cmd".into(),
        cwd: dir.path().to_path_buf(),
        master: pair.master,
        writer,
        parser,
        links: vec![Link {
            kind: LinkKind::File {
                path: file.clone(),
                line: Some(2),
                column: Some(5),
            },
            row: 0,
            start_col: 0,
            end_col: 13,
            text: "main.rs:2:5".to_string(),
        }],
        scroll_offset: 0,
        scrollback_len: 100,
        exited: false,
        scrollback_lines: vec![],
    };
    
    tv.tabs.push(tab);
    tv.active = 0;
    
    let mouse = MouseEvent { kind: MouseEventKind::Down(MouseButton::Left), column: 1, row: 0, modifiers: Modifiers::empty() };
    let ops = tv.handle_mouse(mouse);
    
    // Should emit OpenFile operation
    assert!(ops.iter().any(|op| matches!(op, Operation::OpenFile { path } if path == &file)));
    
    // Should emit GoToLineLocal with JumpTo for line 2 (1-based) = index 1 (0-based), col 5 (1-based) = index 4 (0-based)
    assert!(ops.iter().any(|op| {
        matches!(op, Operation::GoToLineLocal(oo_ide::operation::GoToLineOp::JumpTo { line, column }) if *line == 1 && *column == 4)
    }));
}
