trackWork 0.15.0

A terminal-based time tracking application for managing work sessions
use ratatui::{
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph},
    Frame,
};

use crate::app::{App, InputMode};
use crate::cursor::render_with_cursor;
use crate::triggers::EVENT_LABELS;

fn label_style(is_selected: bool) -> Style {
    if is_selected {
        Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(Color::Gray)
    }
}

fn value_style(is_selected: bool) -> Style {
    if is_selected {
        Style::default().bg(Color::DarkGray).fg(Color::White)
    } else {
        Style::default()
    }
}

pub fn draw_triggers(f: &mut Frame, app: &App) {
    let InputMode::Triggers {
        enabled,
        urls,
        bodies,
        current_field,
        cursor_pos,
        ..
    } = &app.input_mode
    else {
        return;
    };

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Header
            Constraint::Min(10),   // Form
            Constraint::Length(3), // Footer
        ])
        .split(f.area());

    let header = Paragraph::new(vec![Line::from(vec![Span::styled(
        "🔔 Event Triggers (webhooks)",
        Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD),
    )])])
    .block(Block::default().borders(Borders::ALL));
    f.render_widget(header, chunks[0]);

    let mut lines: Vec<Line> = Vec::new();
    for event in 0..3 {
        lines.push(Line::from(Span::styled(
            format!("  {}", EVENT_LABELS[event]),
            Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD),
        )));

        // Enabled toggle (sub 0)
        let f_enabled = event * 3;
        let on_enabled = *current_field == f_enabled;
        lines.push(Line::from(vec![
            Span::styled("    Enabled: ", label_style(on_enabled)),
            Span::styled(
                if enabled[event] { "Yes" } else { "No" },
                value_style(on_enabled),
            ),
            Span::styled(" (←/→ or Space to toggle)", Style::default().fg(Color::DarkGray)),
        ]));

        // URL (sub 1)
        let f_url = event * 3 + 1;
        let on_url = *current_field == f_url;
        lines.push(Line::from(vec![
            Span::styled("    URL: ", label_style(on_url)),
            Span::styled(render_with_cursor(&urls[event], *cursor_pos, on_url), value_style(on_url)),
        ]));

        // Body (sub 2)
        let f_body = event * 3 + 2;
        let on_body = *current_field == f_body;
        lines.push(Line::from(vec![Span::styled("    Body (JSON): ", label_style(on_body))]));
        lines.push(Line::from(vec![
            Span::raw("      "),
            Span::styled(render_with_cursor(&bodies[event], *cursor_pos, on_body), value_style(on_body)),
        ]));
        lines.push(Line::from(""));
    }

    lines.push(Line::from(Span::styled(
        "  Variables: [[event]] [[date]] [[time]] [[datetime]] [[description]]  (Ctrl++ to insert)",
        Style::default().fg(Color::DarkGray),
    )));

    let form = Paragraph::new(lines)
        .block(Block::default().borders(Borders::ALL).title("Triggers"));
    f.render_widget(form, chunks[1]);

    let footer = Paragraph::new(vec![Line::from(vec![
        Span::styled("↑/↓/Tab", Style::default().fg(Color::Cyan)),
        Span::raw(": Navigate  "),
        Span::styled("←/→/Space", Style::default().fg(Color::Cyan)),
        Span::raw(": Toggle  "),
        Span::styled("Enter", Style::default().fg(Color::Green)),
        Span::raw(": Save  "),
        Span::styled("Esc", Style::default().fg(Color::Red)),
        Span::raw(": Cancel"),
    ])])
    .block(Block::default().borders(Borders::ALL));
    f.render_widget(footer, chunks[2]);
}