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::views::View;
use oo_ide::input::{MouseEvent, MouseEventKind, Modifiers};
use oo_ide::operation::Operation;

#[test]
fn ctrl_highlight_only_while_held() {
    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(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![],
        scroll_offset: 0,
        scrollback_len: 100,
        exited: false,
        scrollback_lines: vec![],
    };

    tv.tabs.push(tab);
    tv.active = 0;

    // Mouse move with Ctrl pressed should enable link highlighting
    let mouse_ctrl = MouseEvent { kind: MouseEventKind::Moved, column: 0, row: 0, modifiers: Modifiers::CTRL };
    let ops = tv.handle_mouse(mouse_ctrl);
    assert!(ops.iter().any(|op| matches!(op, Operation::TerminalLocal(oo_ide::operation::TerminalOp::SetLinkHighlight { enabled: true }))));

    // Mouse move with no modifiers should disable link highlighting
    let mouse_none = MouseEvent { kind: MouseEventKind::Moved, column: 0, row: 0, modifiers: Modifiers::empty() };
    let ops2 = tv.handle_mouse(mouse_none);
    assert!(ops2.iter().any(|op| matches!(op, Operation::TerminalLocal(oo_ide::operation::TerminalOp::SetLinkHighlight { enabled: false }))));
}
