orgflow-tui 0.2.0

A terminal user interface for orgflow - manage notes and tasks with a smooth workflow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
use orgflow::{Configuration, Note, OrgDocument, Task, TagSuggestions, Tag, TagCollection};
use std::io;
use std::io::Result as IoResult;
use std::str::FromStr;

mod session;
use session::{SessionManager, SessionState};

mod autocompletion;
use autocompletion::AutocompletionWidget;

use ratatui::crossterm::event::{KeyCode, KeyEventKind, KeyModifiers};
use ratatui::layout::{Direction, Rect};
use ratatui::prelude::Color;
use ratatui::style::Style;
use ratatui::{
    DefaultTerminal, Frame,
    layout::{Constraint, Layout},
    prelude::Line,
    style::Stylize,
    widgets::{Block, Borders, Widget},
};
use tui_textarea::TextArea;

fn main() -> io::Result<()> {
    // Initialise terminal and move to raw mode
    let mut terminal = ratatui::init();

    // Create app and run for infinite loop
    let mut app = App::new()?;
    let app_result = app.run(&mut terminal);

    // Disable raw mode
    ratatui::restore();

    // Return application exit code
    app_result
}

#[derive(Debug)]
struct App {
    document: OrgDocument,
    exit: bool,
    note: TextArea<'static>,
    title: TextArea<'static>,
    note_focus: NoteFocus,
    scratchpad: TextArea<'static>,
    scratchpad_visible: bool,
    current_tab: AppTab,
    current_note_index: usize,
    current_task_index: usize,
    session_manager: SessionManager,
    document_path: String,
    has_unsaved_changes: bool,
    tag_suggestions: TagSuggestions,
    autocompletion: AutocompletionWidget,          // For scratchpad
    title_autocompletion: AutocompletionWidget,    // For note titles
}

#[derive(Debug)]
enum AppTab {
    Editor,
    Viewer,
    Tasks,
}

#[derive(Debug, PartialEq)]
enum NoteFocus {
    Title,
    Content,
}

impl<'a> App {
    fn new() -> IoResult<Self> {
        let basefolder = Configuration::basefolder();

        // Ensure base folder exists with better error handling
        if let Err(e) = std::fs::create_dir_all(&basefolder) {
            eprintln!("Failed to create base folder '{}': {}", basefolder, e);
            eprintln!("Try setting ORGFLOW_BASEFOLDER to a writable directory:");
            eprintln!("  export ORGFLOW_BASEFOLDER=/tmp/orgflow");
            return Err(e);
        }

        let refile_path = std::path::Path::new(&basefolder).join("refile.org");
        let document_path = refile_path.to_str().unwrap().to_string();

        // Load document or create empty one if file doesn't exist
        let document = match OrgDocument::from(&document_path) {
            Ok(doc) => doc,
            Err(_) => OrgDocument::default(), // Create empty document if file doesn't exist
        };

        // Initialize session manager
        let session_file_path = std::path::Path::new(&basefolder).join("session.json");
        let mut session_manager =
            SessionManager::new(session_file_path.to_str().unwrap().to_string());

        // Load existing session or create default
        let session_state = match session_manager.load_session() {
            Ok(state) => state,
            Err(e) => {
                eprintln!("Warning: Failed to load session, starting fresh: {}", e);
                SessionState::default()
            }
        };

        // Restore UI state from session
        let current_tab = session_state.current_tab;
        // Ensure indices are within bounds for current document
        let current_note_index = if session_state.current_note_index < document.notes.len() {
            session_state.current_note_index
        } else {
            0
        };
        let current_task_index = if session_state.current_task_index < document.tasks.len() {
            session_state.current_task_index
        } else {
            0
        };
        let note_focus = session_state.note_focus;
        let scratchpad_visible = session_state.scratchpad_visible;

        // Restore draft content from session with cursor positions
        let title = SessionManager::restore_textarea_with_cursor(
            &session_state.title_content,
            session_state.title_cursor_pos,
        );
        let note = SessionManager::restore_textarea_with_cursor(
            &session_state.note_content,
            session_state.note_cursor_pos,
        );
        let scratchpad = SessionManager::restore_textarea_with_cursor(
            &session_state.scratchpad_content,
            session_state.scratchpad_cursor_pos,
        );

        // Extract tag suggestions from document
        let tag_suggestions = document.collect_unique_tags();
        let autocompletion = AutocompletionWidget::new();
        let title_autocompletion = AutocompletionWidget::new();

        let app = App {
            document,
            exit: false,
            note,
            title,
            note_focus,
            scratchpad,
            scratchpad_visible,
            current_tab,
            current_note_index,
            current_task_index,
            session_manager,
            document_path,
            has_unsaved_changes: session_state.has_unsaved_changes,
            tag_suggestions,
            autocompletion,
            title_autocompletion,
        };
        Ok(app)
    }
    /// Start the application
    fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
        // Infinite loop until variable set
        while !self.exit {
            // Iterate over frames and draw them one by one
            terminal.draw(|frame| self.draw(frame))?;

            // wait for key events and handle them locally in the application
            match ratatui::crossterm::event::read()? {
                ratatui::crossterm::event::Event::Key(key_event) => {
                    self.handle_key_event(key_event)?;

                    // Update session state after each keystroke
                    self.update_session_state();

                    // Check if we should save session (debounced)
                    if self.session_manager.should_save() {
                        let _ = self.session_manager.save_session();
                    }
                }
                _ => {}
            }
        }

        // Force save session on exit
        let _ = self.session_manager.force_save();
        Ok(())
    }
    /// Routine about how to draw each frame in application
    fn draw(&self, frame: &mut Frame) {
        frame.render_widget(self, frame.area());
    }

    /// Look for key presses and handle event
    fn handle_key_event(
        &mut self,
        key_event: ratatui::crossterm::event::KeyEvent,
    ) -> io::Result<()> {
        match (
            key_event.kind,
            key_event.code,
            &self.current_tab,
            &self.note_focus,
        ) {
            // Tab switching with Ctrl+Tab (cycles through tabs) - only when scratchpad is NOT visible
            (KeyEventKind::Press, KeyCode::Char('r'), _, _)
                if key_event.modifiers.contains(KeyModifiers::CONTROL)
                    && !self.scratchpad_visible =>
            {
                self.current_tab = match self.current_tab {
                    AppTab::Editor => {
                        // Reset note index if out of bounds when switching to Viewer
                        if self.current_note_index >= self.document.notes.len() {
                            self.current_note_index = 0;
                        }
                        AppTab::Viewer
                    }
                    AppTab::Viewer => {
                        // Reset task index if out of bounds when switching to Tasks
                        if self.current_task_index >= self.document.tasks.len() {
                            self.current_task_index = 0;
                        }
                        AppTab::Tasks
                    }
                    AppTab::Tasks => AppTab::Editor,
                };
            }
            // Arrow navigation in viewer tab
            (KeyEventKind::Press, KeyCode::Left, AppTab::Viewer, _) => {
                if self.current_note_index > 0 {
                    self.current_note_index -= 1;
                }
            }
            (KeyEventKind::Press, KeyCode::Right, AppTab::Viewer, _) => {
                if self.current_note_index < self.document.notes.len().saturating_sub(1) {
                    self.current_note_index += 1;
                }
            }
            // Arrow navigation in tasks tab
            (KeyEventKind::Press, KeyCode::Up, AppTab::Tasks, _) => {
                if self.current_task_index > 0 {
                    self.current_task_index -= 1;
                }
            }
            (KeyEventKind::Press, KeyCode::Down, AppTab::Tasks, _) => {
                if self.current_task_index < self.document.tasks.len().saturating_sub(1) {
                    self.current_task_index += 1;
                }
            }
            (KeyEventKind::Press, KeyCode::Char('t'), _, _)
                if key_event.modifiers.contains(KeyModifiers::CONTROL) =>
            {
                self.scratchpad_visible = !self.scratchpad_visible;
            }
            // Ctrl+S save - put this early to ensure it's not intercepted
            (KeyEventKind::Press, KeyCode::Char('s'), _, _)
                if key_event.modifiers.contains(KeyModifiers::CONTROL)
                    && !self.scratchpad_visible =>
            {
                self.save_note()?;
            }
            (KeyEventKind::Press, KeyCode::Esc, _, _) if self.scratchpad_visible && self.autocompletion.is_visible() => {
                // Hide autocompletion but don't close scratchpad
                self.autocompletion.hide();
            }
            (KeyEventKind::Press, KeyCode::Esc, AppTab::Editor, NoteFocus::Title) if self.title_autocompletion.is_visible() => {
                // Hide title autocompletion
                self.title_autocompletion.hide();
            }
            (KeyEventKind::Press, KeyCode::Esc, _, _) => {
                if self.scratchpad_visible {
                    // First ESC closes the scratchpad
                    self.scratchpad_visible = false;
                } else {
                    // Second ESC (or first ESC when scratchpad isn't visible) exits the app
                    self.exit = true;
                }
            }
            (KeyEventKind::Press, KeyCode::Enter, _, _) if self.scratchpad_visible => {
                let task = self.scratchpad.lines().first().unwrap();
                let t = Task::with_today(task);
                self.document.push_task(t);

                // Save to file immediately
                let _ = self.document.to(&self.document_path);

                self.scratchpad = TextArea::default();
                self.has_unsaved_changes = false;
                
                // Update tag suggestions after adding new task
                self.tag_suggestions = self.document.collect_unique_tags();
            }
            // Autocompletion handling in scratchpad
            (KeyEventKind::Press, KeyCode::Up, _, _) if self.scratchpad_visible && self.autocompletion.is_visible() => {
                self.autocompletion.select_previous();
            }
            (KeyEventKind::Press, KeyCode::Down, _, _) if self.scratchpad_visible && self.autocompletion.is_visible() => {
                self.autocompletion.select_next();
            }
            (KeyEventKind::Press, KeyCode::Tab, _, _) if self.scratchpad_visible && self.autocompletion.is_visible() => {
                // Apply the selected suggestion
                if let Some((new_text, _cursor_pos)) = self.autocompletion.apply_selected(&self.scratchpad.lines().join(" ")) {
                    // Replace the text content
                    self.scratchpad = TextArea::from(vec![new_text]);
                    // Move cursor to the end of the inserted tag
                    self.scratchpad.move_cursor(tui_textarea::CursorMove::End);
                    self.autocompletion.hide();
                }
            }
            (_, _, _, _) if self.scratchpad_visible => {
                self.scratchpad.input(key_event);
                // Update autocompletion suggestions after input
                let current_text = self.scratchpad.lines().join(" ");
                self.autocompletion.update_suggestions(&current_text, &self.tag_suggestions);
            }
            // Editor tab specific key handling
            (KeyEventKind::Press, KeyCode::BackTab, AppTab::Editor, NoteFocus::Content) => {
                self.note_focus = NoteFocus::Title
            }
            (KeyEventKind::Press, KeyCode::BackTab, AppTab::Editor, NoteFocus::Title) => {
                self.note_focus = NoteFocus::Content
            }
            (KeyEventKind::Press, KeyCode::Enter, AppTab::Editor, NoteFocus::Title) => {
                self.note_focus = NoteFocus::Content
            }
            // Title autocompletion handling
            (KeyEventKind::Press, KeyCode::Up, AppTab::Editor, NoteFocus::Title) if self.title_autocompletion.is_visible() => {
                self.title_autocompletion.select_previous();
            }
            (KeyEventKind::Press, KeyCode::Down, AppTab::Editor, NoteFocus::Title) if self.title_autocompletion.is_visible() => {
                self.title_autocompletion.select_next();
            }
            (KeyEventKind::Press, KeyCode::Tab, AppTab::Editor, NoteFocus::Title) if self.title_autocompletion.is_visible() => {
                // Apply the selected suggestion
                if let Some((new_text, _cursor_pos)) = self.title_autocompletion.apply_selected(&self.title.lines().join(" ")) {
                    self.title = TextArea::from(vec![new_text]);
                    self.title.move_cursor(tui_textarea::CursorMove::End);
                    self.title_autocompletion.hide();
                }
            }
            (KeyEventKind::Press, KeyCode::Tab, AppTab::Editor, NoteFocus::Title) => {
                self.note_focus = NoteFocus::Content
            }
            (_, _, AppTab::Editor, NoteFocus::Content) => _ = self.note.input(key_event),
            (_, _, AppTab::Editor, NoteFocus::Title) => {
                self.title.input(key_event);
                // Update autocompletion suggestions after input
                let current_text = self.title.lines().join(" ");
                self.title_autocompletion.update_suggestions(&current_text, &self.tag_suggestions);
            }
            // Ignore other inputs in viewer mode
            (_, _, AppTab::Viewer, _) => {}
            // Ignore other inputs in tasks mode
            (_, _, AppTab::Tasks, _) => {}
        }
        Ok(())
    }

    /// Extract tags from text (title or content)
    fn extract_tags_from_text(&self, text: &str) -> Vec<Tag> {
        let mut tags = Vec::new();
        
        // Split text into words and look for tag patterns
        for word in text.split_whitespace() {
            if let Ok(tag) = Tag::from_str(word) {
                tags.push(tag);
            }
        }
        
        tags
    }

    /// Remove tags from text, returning the cleaned text
    fn remove_tags_from_text(&self, text: &str) -> String {
        text.split_whitespace()
            .filter(|word| Tag::from_str(word).is_err()) // Keep words that are NOT valid tags
            .collect::<Vec<&str>>()
            .join(" ")
    }

    fn save_note(&mut self) -> io::Result<()> {
        let title = self.title.lines().join(" ");
        let content: Vec<String> = self.note.lines().iter().map(|s| s.to_string()).collect();

        // Check if we have any meaningful content
        let has_title = !title.trim().is_empty();
        let has_content = content.iter().any(|line| !line.trim().is_empty());

        if has_title || has_content {
            // Extract tags from title and content
            let mut extracted_tags = Vec::new();
            extracted_tags.extend(self.extract_tags_from_text(&title));
            for line in &content {
                extracted_tags.extend(self.extract_tags_from_text(line));
            }

            // Remove tags from title to get clean title
            let clean_title = self.remove_tags_from_text(&title);
            let final_title = if clean_title.trim().is_empty() {
                "Untitled Note".to_string()
            } else {
                clean_title
            };

            // Remove tags from content to get clean content
            let clean_content: Vec<String> = content
                .iter()
                .map(|line| self.remove_tags_from_text(line))
                .filter(|line| !line.trim().is_empty()) // Remove empty lines
                .collect();

            // Create note with extracted tags
            let note = if !extracted_tags.is_empty() {
                let tag_collection = TagCollection::from_tags(extracted_tags);
                Note::with_tags(final_title, clean_content, tag_collection)
            } else {
                Note::with(final_title, clean_content)
            };
            
            self.document.push_note(note);

            // Save to file
            self.document.to(&self.document_path)?;

            // Clear the text areas
            self.title = TextArea::default();
            self.note = TextArea::default();
            self.note_focus = NoteFocus::Title;
            self.has_unsaved_changes = false;
            
            // Update tag suggestions after adding new note
            self.tag_suggestions = self.document.collect_unique_tags();
        }
        Ok(())
    }

    /// Update session state with current application state
    fn update_session_state(&mut self) {
        // Check if there are unsaved changes in text areas
        let has_draft_content = !self.title.lines().is_empty()
            || !self.note.lines().is_empty()
            || !self.scratchpad.lines().is_empty();

        let has_unsaved = self.has_unsaved_changes || has_draft_content;

        self.session_manager.update_state(
            &self.current_tab,
            self.current_note_index,
            self.current_task_index,
            &self.note_focus,
            self.scratchpad_visible,
            &self.title,
            &self.note,
            &self.scratchpad,
            &self.document_path,
            has_unsaved,
        );
    }
}

/// Give App itself the ability to be a Widget (if there is only one widget )
impl<'a> Widget for &App {
    fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer)
    where
        Self: Sized,
    {
        match self.current_tab {
            AppTab::Editor => render_note_editor(self, area, buf),
            AppTab::Viewer => render_note_viewer(self, area, buf),
            AppTab::Tasks => render_task_viewer(self, area, buf),
        }
    }
}

fn render_note_editor(app: &App, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
    // Create a vertical layout via length
    let vertical_layout = Layout::vertical([
        Constraint::Length(1),
        Constraint::Length(3),
        Constraint::Min(0),
    ]);

    // Split input area in above layout
    let [appname_area, title_area, content_area] = vertical_layout.areas(area);

    // Render title in the vertical area
    Line::from("Orgflow - Editor | Viewer | Tasks (Ctrl+R to switch)")
        .bold()
        .centered()
        .render(appname_area, buf);

    // Define title area and its content
    let mut title = TextArea::from(app.title.clone());
    let title_block = Block::default().borders(Borders::ALL).title("Title");
    let title_block = match app.note_focus {
        NoteFocus::Title if !app.scratchpad_visible => {
            title_block.style(Style::default().fg(Color::Yellow))
        }
        _ => title_block,
    };

    // Define content for the note inputs: content (text_area), title (instructions), border (block)
    let mut text_area = TextArea::from(app.note.clone());
    let note_instructions = Line::from(vec![
        " Quit ".into(),
        "<ESC> ".blue().bold(),
        "Switch ".into(),
        "<SHIFT>+<TAB> ".blue().bold(),
        "Save Note ".into(),
        "<CTRL>+<S> ".blue().bold(),
        "Enter Task ".into(),
        "<CTRL>+<T> ".blue().bold(),
        "Switch ".into(),
        "<CTRL>+<R> ".blue().bold(),
    ])
    .centered();
    let note_block = Block::default()
        .borders(Borders::ALL)
        .title("Content")
        .title_bottom(note_instructions);
    let note_block = match app.note_focus {
        NoteFocus::Content if !app.scratchpad_visible => {
            note_block.style(Style::default().fg(Color::Yellow))
        }
        _ => note_block,
    };

    let mut scratchpad = TextArea::from(app.scratchpad.clone());
    let scratchpad_block = Block::default()
        .borders(Borders::ALL)
        .title("Task")
        .style(Style::default().fg(Color::Yellow));

    let scratchpad_area = centered_rect(60, 10, area);

    if app.scratchpad_visible {
        scratchpad.set_block(scratchpad_block);
        scratchpad.render(scratchpad_area, buf);
        
        // Render autocompletion popup if visible
        if app.autocompletion.is_visible() {
            // Calculate cursor position within the scratchpad
            let cursor_line = scratchpad.cursor().0;
            let cursor_col = scratchpad.cursor().1;
            let cursor_pos = (
                scratchpad_area.x + 1 + cursor_col as u16, // +1 for border
                scratchpad_area.y + 1 + cursor_line as u16, // +1 for border
            );
            app.autocompletion.render(area, buf, cursor_pos);
        }
    }

    // Render each of the contents
    text_area.set_block(note_block);
    text_area.render(content_area, buf);

    title.set_block(title_block);
    title.render(title_area, buf);
    
    // Render title autocompletion popup if visible
    if app.title_autocompletion.is_visible() && app.note_focus == NoteFocus::Title && !app.scratchpad_visible {
        // Calculate cursor position within the title
        let cursor_line = title.cursor().0;
        let cursor_col = title.cursor().1;
        let cursor_pos = (
            title_area.x + 1 + cursor_col as u16, // +1 for border
            title_area.y + 1 + cursor_line as u16, // +1 for border
        );
        app.title_autocompletion.render(area, buf, cursor_pos);
    }
}

fn render_note_viewer(app: &App, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
    // Create a vertical layout
    let vertical_layout = Layout::vertical([
        Constraint::Length(1),
        Constraint::Length(3),
        Constraint::Min(0),
    ]);

    // Split input area in above layout
    let [appname_area, navigation_area, main_area] = vertical_layout.areas(area);

    // Render title in the vertical area
    Line::from("Orgflow - Editor | Viewer | Tasks (Ctrl+R to switch)")
        .bold()
        .centered()
        .render(appname_area, buf);

    // Show current note info and navigation
    let note_count = app.document.notes.len();
    let current_index = app.current_note_index;

    let navigation_content = if note_count == 0 {
        vec!["No notes available".to_string()]
    } else {
        vec![format!(
            "Note {} of {} (Use ←→ arrows to navigate)",
            current_index + 1,
            note_count
        )]
    };

    let navigation_block = Block::default()
        .borders(Borders::ALL)
        .title("Navigation")
        .style(Style::default().fg(Color::Yellow));

    let mut navigation_display = TextArea::from(navigation_content);
    navigation_display.set_block(navigation_block);
    navigation_display.render(navigation_area, buf);

    if note_count == 0 {
        // Show empty state
        let empty_block = Block::default()
            .borders(Borders::ALL)
            .title("No Notes")
            .title_bottom(
                Line::from(vec![
                    " Quit ".into(),
                    "<ESC> ".blue().bold(),
                    "Switch ".into(),
                    "<CTRL>+<TAB> ".blue().bold(),
                ])
                .centered(),
            );

        let mut empty_display = TextArea::from(vec!["No notes to display".to_string()]);
        empty_display.set_block(empty_block);
        empty_display.render(main_area, buf);
        return;
    }

    // Create horizontal layout for content and metadata
    let horizontal_layout =
        Layout::horizontal([Constraint::Percentage(70), Constraint::Percentage(30)]);

    let [content_area, metadata_area] = horizontal_layout.areas(main_area);

    // Create vertical layout for content area (title + content)
    let content_vertical = Layout::vertical([Constraint::Length(3), Constraint::Min(0)]);

    let [title_area, note_content_area] = content_vertical.areas(content_area);

    if let Some(note) = app.document.notes.get(current_index) {
        // Display note title
        let title_block = Block::default().borders(Borders::ALL).title("Title");

        let mut title_display = TextArea::from(vec![note.title().to_string()]);
        title_display.set_block(title_block);
        title_display.render(title_area, buf);

        // Display note content
        let content_block = Block::default()
            .borders(Borders::ALL)
            .title("Content")
            .title_bottom(
                Line::from(vec![
                    " Quit ".into(),
                    "<ESC> ".blue().bold(),
                    "Switch ".into(),
                    "<CTRL>+<TAB> ".blue().bold(),
                ])
                .centered(),
            );

        let content_lines: Vec<String> = note.content().iter().cloned().collect();
        let mut content_display = TextArea::from(content_lines);
        content_display.set_block(content_block);
        content_display.render(note_content_area, buf);

        // Display metadata
        let metadata_lines = vec![
            format!("Level: {}", note.level()),
            format!("Created: {}", note.creation_date()),
            format!("Modified: {}", note.modification_date()),
            format!("GUID: {}", note.guid()),
            format!("Tags: {}", note.tags()),
        ];

        let metadata_block = Block::default().borders(Borders::ALL).title("Metadata");

        let mut metadata_display = TextArea::from(metadata_lines);
        metadata_display.set_block(metadata_block);
        metadata_display.render(metadata_area, buf);
    }
}

fn render_task_viewer(app: &App, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
    // Create a vertical layout
    let vertical_layout = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]);

    // Split input area in above layout
    let [appname_area, main_area] = vertical_layout.areas(area);

    // Render title in the vertical area
    Line::from("Orgflow - Editor | Viewer | Tasks (Ctrl+R to switch)")
        .bold()
        .centered()
        .render(appname_area, buf);

    let task_count = app.document.tasks.len();
    let current_index = app.current_task_index;

    if task_count == 0 {
        // Show empty state
        let empty_block = Block::default()
            .borders(Borders::ALL)
            .title("No Tasks")
            .title_bottom(
                Line::from(vec![
                    " Quit ".into(),
                    "<ESC> ".blue().bold(),
                    "Switch ".into(),
                    "<CTRL>+<TAB> ".blue().bold(),
                ])
                .centered(),
            );

        let mut empty_display = TextArea::from(vec!["No tasks to display".to_string()]);
        empty_display.set_block(empty_block);
        empty_display.render(main_area, buf);
        return;
    }

    // Create horizontal layout for task list and metadata
    let horizontal_layout =
        Layout::horizontal([Constraint::Percentage(60), Constraint::Percentage(40)]);

    let [task_list_area, metadata_area] = horizontal_layout.areas(main_area);

    // Display task list with current selection highlighted
    let task_list_block = Block::default()
        .borders(Borders::ALL)
        .title(format!("Tasks ({} total)", task_count))
        .title_bottom(
            Line::from(vec![
                " Quit ".into(),
                "<ESC> ".blue().bold(),
                "Navigate ".into(),
                "<↑↓> ".blue().bold(),
                "Switch ".into(),
                "<CTRL>+<TAB> ".blue().bold(),
            ])
            .centered(),
        );

    // Create content area for the task list
    let inner_area = task_list_block.inner(task_list_area);
    task_list_block.render(task_list_area, buf);

    // Render each task line with appropriate styling
    for (i, task) in app.document.tasks.iter().enumerate() {
        if i >= inner_area.height as usize {
            break; // Don't render beyond the available space
        }

        let y = inner_area.y + i as u16;
        let prefix = if i == current_index { "" } else { "  " };
        let status = if task.is_completed() { "[x]" } else { "[ ]" };
        let text = format!("{}{} {}", prefix, status, task.description());

        let style = if i == current_index {
            Style::default().add_modifier(ratatui::style::Modifier::UNDERLINED)
        } else {
            Style::default()
        };

        Line::from(text).style(style).render(
            ratatui::layout::Rect {
                x: inner_area.x,
                y,
                width: inner_area.width,
                height: 1,
            },
            buf,
        );
    }

    // Display metadata for current task
    if let Some(task) = app.document.tasks.get(current_index) {
        let mut metadata_lines = vec![format!(
            "Status: {}",
            if task.is_completed() {
                "Completed"
            } else {
                "Pending"
            }
        )];

        if let Some(priority) = task.priority_level() {
            metadata_lines.push(format!("Priority: {}", priority));
        } else {
            metadata_lines.push("Priority: None".to_string());
        }

        if let Some(creation_date) = task.creation_date() {
            metadata_lines.push(format!("Created: {}", creation_date));
        } else {
            metadata_lines.push("Created: Unknown".to_string());
        }

        if let Some(completion_date) = task.completion_date() {
            metadata_lines.push(format!("Completed: {}", completion_date));
        } else {
            metadata_lines.push("Completed: N/A".to_string());
        }

        if let Some(tags) = task.tags() {
            metadata_lines.push(format!("Tags: {}", tags));
        } else {
            metadata_lines.push("Tags: None".to_string());
        }

        metadata_lines.push("".to_string());
        metadata_lines.push("Description:".to_string());
        metadata_lines.push(task.description().to_string());

        let metadata_block = Block::default().borders(Borders::ALL).title("Task Details");

        let mut metadata_display = TextArea::from(metadata_lines);
        metadata_display.set_block(metadata_block);
        metadata_display.render(metadata_area, buf);
    }
}

fn centered_rect(percent_x: u16, percent_y: u16, area: Rect) -> Rect {
    let popup_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage((100 - percent_y) / 2),
            Constraint::Length(3),
            Constraint::Percentage((100 - percent_y) / 2),
        ])
        .split(area);

    Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage((100 - percent_x) / 2),
            Constraint::Percentage(percent_x),
            Constraint::Percentage((100 - percent_x) / 2),
        ])
        .split(popup_layout[1])[1]
}