tui-canvas 0.8.12

Form/textarea/input for TUI
Documentation
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
// examples/validation_2.rs
//! Advanced TUI Example demonstrating complex pattern filtering edge cases
//!
//! This example showcases the full potential of the pattern validation system
//! with creative real-world scenarios and edge cases.
//!
//! Run with: cargo run --example validation_advanced_patterns --features "validation,gui,cursor-style"

// REQUIRE validation, gui and cursor-style features
#[cfg(not(all(feature = "validation", feature = "gui", feature = "cursor-style")))]
compile_error!(
    "This example requires the 'validation', 'gui' and 'cursor-style' features. \
     Run with: cargo run --example validation_advanced_patterns --features \"validation,gui,cursor-style\""
);

use crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use ratatui::{
    Frame, Terminal,
    backend::{Backend, CrosstermBackend},
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Wrap},
};
use std::io;
use std::sync::Arc;
use tui_canvas::ValidationResult;

use tui_canvas::{
    AppMode, CharacterFilter, CursorManager, DataProvider, PatternFilters, PositionFilter,
    PositionRange, TextFormState, ValidationConfig, ValidationConfigBuilder, render_canvas_default,
};

struct AdvancedPatternTextForm<D: DataProvider> {
    editor: TextFormState<D>,
    debug_message: String,
    command_buffer: String,
    validation_enabled: bool,
    field_switch_blocked: bool,
    block_reason: Option<String>,
}

impl<D: DataProvider> AdvancedPatternTextForm<D> {
    fn new(data_provider: D) -> Self {
        let mut editor = TextFormState::new(data_provider);
        editor.set_validation_enabled(true);

        Self {
            editor,
            debug_message:
                "๐Ÿš€ Advanced Pattern Validation - Showcasing edge cases and complex patterns!"
                    .to_string(),
            command_buffer: String::new(),
            validation_enabled: true,
            field_switch_blocked: false,
            block_reason: None,
        }
    }

    fn clear_command_buffer(&mut self) {
        self.command_buffer.clear();
    }
    fn add_to_command_buffer(&mut self, ch: char) {
        self.command_buffer.push(ch);
    }
    fn get_command_buffer(&self) -> &str {
        &self.command_buffer
    }
    fn has_pending_command(&self) -> bool {
        !self.command_buffer.is_empty()
    }

    fn toggle_validation(&mut self) {
        self.validation_enabled = !self.validation_enabled;
        self.editor.set_validation_enabled(self.validation_enabled);
        if self.validation_enabled {
            self.debug_message = "โœ… Advanced Pattern Validation ENABLED".to_string();
        } else {
            self.debug_message = "โŒ Advanced Pattern Validation DISABLED".to_string();
        }
    }

    fn move_left(&mut self) {
        self.editor.move_left();
        self.field_switch_blocked = false;
        self.block_reason = None;
    }
    fn move_right(&mut self) {
        self.editor.move_right();
        self.field_switch_blocked = false;
        self.block_reason = None;
    }

    fn move_up(&mut self) {
        if self.editor.move_up() {
            self.update_field_validation_status();
            self.field_switch_blocked = false;
            self.block_reason = None;
        } else if let Some(reason) = self.editor.field_switch_block_reason() {
            self.field_switch_blocked = true;
            self.block_reason = Some(reason.clone());
            self.debug_message = format!("๐Ÿšซ Field switch blocked: {reason}");
        }
    }

    fn move_down(&mut self) {
        if self.editor.move_down() {
            self.update_field_validation_status();
            self.field_switch_blocked = false;
            self.block_reason = None;
        } else if let Some(reason) = self.editor.field_switch_block_reason() {
            self.field_switch_blocked = true;
            self.block_reason = Some(reason.clone());
            self.debug_message = format!("๐Ÿšซ Field switch blocked: {reason}");
        }
    }

    fn move_line_start(&mut self) {
        self.editor.move_line_start();
    }
    fn move_line_end(&mut self) {
        self.editor.move_line_end();
    }

    fn enter_edit_mode(&mut self) {
        self.editor.enter_edit_mode();
        self.debug_message =
            "โœ๏ธ  INSERT MODE - Cursor: Steady Bar | - Testing advanced pattern validation"
                .to_string();
    }

    fn enter_append_mode(&mut self) {
        self.editor.enter_append_mode();
        self.debug_message =
            "โœ๏ธ  INSERT (append) - Cursor: Steady Bar | - Advanced patterns active".to_string();
    }

    fn exit_edit_mode(&mut self) {
        self.editor.exit_edit_mode();
        self.debug_message = "๐Ÿ”’ NORMAL MODE - Cursor: Steady Block โ–ˆ".to_string();
        self.update_field_validation_status();
    }

    fn insert_char(&mut self, ch: char) -> anyhow::Result<()> {
        let result = self.editor.insert_char(ch);
        if result.is_ok() {
            let validation_result = self.editor.validate_current_field();
            match validation_result {
                ValidationResult::Valid => {
                    self.debug_message = "โœ… Character accepted".to_string();
                }
                ValidationResult::Warning { message } => {
                    self.debug_message = format!("โš ๏ธ  Warning: {message}");
                }
                ValidationResult::Error { message } => {
                    self.debug_message = format!("โŒ Pattern violation: {message}");
                }
            }
        }
        result
    }

    fn delete_backward(&mut self) -> anyhow::Result<()> {
        let result = self.editor.delete_backward();
        if result.is_ok() {
            self.debug_message = "โŒซ Character deleted".to_string();
        }
        result
    }

    fn delete_forward(&mut self) -> anyhow::Result<()> {
        let result = self.editor.delete_forward();
        if result.is_ok() {
            self.debug_message = "โŒฆ Character deleted".to_string();
        }
        result
    }

    // Delegate methods
    fn current_field(&self) -> usize {
        self.editor.current_field()
    }
    fn cursor_position(&self) -> usize {
        self.editor.cursor_position()
    }
    fn mode(&self) -> AppMode {
        self.editor.mode()
    }
    fn current_text(&self) -> &str {
        let field_index = self.editor.current_field();
        self.editor.data_provider().field_value(field_index)
    }
    fn data_provider(&self) -> &D {
        self.editor.data_provider()
    }
    fn ui_state(&self) -> &tui_canvas::EditorState {
        self.editor.ui_state()
    }
    fn set_mode(&mut self, mode: AppMode) {
        self.editor.set_mode(mode);
    }

    fn next_field(&mut self) {
        if self.editor.next_field() {
            self.update_field_validation_status();
            self.field_switch_blocked = false;
            self.block_reason = None;
        } else if let Some(reason) = self.editor.field_switch_block_reason() {
            self.field_switch_blocked = true;
            self.block_reason = Some(reason.clone());
            self.debug_message = format!("๐Ÿšซ Cannot move to next field: {reason}");
        }
    }

    fn prev_field(&mut self) {
        if self.editor.prev_field() {
            self.update_field_validation_status();
            self.field_switch_blocked = false;
            self.block_reason = None;
        } else if let Some(reason) = self.editor.field_switch_block_reason() {
            self.field_switch_blocked = true;
            self.block_reason = Some(reason.clone());
            self.debug_message = format!("๐Ÿšซ Cannot move to previous field: {reason}");
        }
    }

    fn set_debug_message(&mut self, msg: String) {
        self.debug_message = msg;
    }
    fn debug_message(&self) -> &str {
        &self.debug_message
    }

    fn update_field_validation_status(&mut self) {
        if !self.validation_enabled {
            return;
        }
        let result = self.editor.validate_current_field();
        match result {
            ValidationResult::Valid => {
                self.debug_message = format!(
                    "Field {}: โœ… Pattern valid",
                    self.editor.current_field() + 1
                );
            }
            ValidationResult::Warning { message } => {
                self.debug_message =
                    format!("Field {}: โš ๏ธ  {}", self.editor.current_field() + 1, message);
            }
            ValidationResult::Error { message } => {
                self.debug_message =
                    format!("Field {}: โŒ {}", self.editor.current_field() + 1, message);
            }
        }
    }

    fn get_validation_status(&self) -> String {
        if !self.validation_enabled {
            return "โŒ DISABLED".to_string();
        }
        if self.field_switch_blocked {
            return "๐Ÿšซ SWITCH BLOCKED".to_string();
        }
        let summary = self.editor.validation_summary();
        if summary.has_errors() {
            format!("โŒ {} ERRORS", summary.error_fields)
        } else if summary.has_warnings() {
            format!("โš ๏ธ  {} WARNINGS", summary.warning_fields)
        } else if summary.validated_fields > 0 {
            format!("โœ… {} VALID", summary.valid_fields)
        } else {
            "๐Ÿ” READY".to_string()
        }
    }
}

// Advanced demo form with creative and edge-case-heavy validation patterns
struct AdvancedPatternData {
    fields: Vec<(String, String)>,
}

impl AdvancedPatternData {
    fn new() -> Self {
        Self {
            fields: vec![
                ("๐Ÿ• Time (HH:MM) - 24hr format".to_string(), "".to_string()),
                (
                    "๐ŸŽจ Hex Color (#RRGGBB) - Web colors".to_string(),
                    "".to_string(),
                ),
                (
                    "๐ŸŒ IPv4 (XXX.XXX.XXX.XXX) - Network address".to_string(),
                    "".to_string(),
                ),
                (
                    "๐Ÿท๏ธ  Product Code (ABC-123-XYZ) - Mixed format".to_string(),
                    "".to_string(),
                ),
                (
                    "๐Ÿ“… Date Code (2024W15) - Year + Week".to_string(),
                    "".to_string(),
                ),
                (
                    "๐Ÿ”ข Binary (101010) - Only 0s and 1s".to_string(),
                    "".to_string(),
                ),
                (
                    "๐ŸŽฏ Complex ID (A1-B2C-3D4E) - Multi-rule".to_string(),
                    "".to_string(),
                ),
                (
                    "๐Ÿš€ Custom Pattern - Advanced logic".to_string(),
                    "".to_string(),
                ),
            ],
        }
    }
}

impl DataProvider for AdvancedPatternData {
    fn field_count(&self) -> usize {
        self.fields.len()
    }
    fn field_name(&self, index: usize) -> &str {
        &self.fields[index].0
    }
    fn field_value(&self, index: usize) -> &str {
        &self.fields[index].1
    }
    fn set_field_value(&mut self, index: usize, value: String) {
        self.fields[index].1 = value;
    }

    fn validation_config(&self, field_index: usize) -> Option<ValidationConfig> {
        match field_index {
            0 => {
                // ๐Ÿ• Time (HH:MM) - Hours 00-23, Minutes 00-59
                // This showcases: Multiple position ranges, exact character matching, custom validation
                let time_pattern = PatternFilters::new()
                    .add_filter(PositionFilter::new(
                        PositionRange::Multiple(vec![0, 1, 3, 4]), // Hours and minutes positions
                        CharacterFilter::Numeric,
                    ))
                    .add_filter(PositionFilter::new(
                        PositionRange::Single(2), // Colon separator
                        CharacterFilter::Exact(':'),
                    ));

                Some(
                    ValidationConfigBuilder::new()
                        .with_pattern_filters(time_pattern)
                        .with_max_length(5) // HH:MM = 5 characters
                        .build(),
                )
            }
            1 => {
                // ๐ŸŽจ Hex Color (#RRGGBB) - Web color format
                // This showcases: OneOf filter with hex digits, exact character at start
                let hex_digits = vec![
                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
                    'a', 'b', 'c', 'd', 'e', 'f',
                ];
                let hex_color_pattern = PatternFilters::new()
                    .add_filter(PositionFilter::new(
                        PositionRange::Single(0), // Hash symbol
                        CharacterFilter::Exact('#'),
                    ))
                    .add_filter(PositionFilter::new(
                        PositionRange::Range(1, 6), // 6 hex digits for RGB
                        CharacterFilter::OneOf(hex_digits),
                    ));

                Some(
                    ValidationConfigBuilder::new()
                        .with_pattern_filters(hex_color_pattern)
                        .with_max_length(7) // #RRGGBB = 7 characters
                        .build(),
                )
            }
            2 => {
                // ๐ŸŒ IPv4 Address (XXX.XXX.XXX.XXX) - Network address
                // This showcases: Complex pattern with dots at specific positions
                let ipv4_pattern = PatternFilters::new()
                    .add_filter(PositionFilter::new(
                        PositionRange::Multiple(vec![3, 7, 11]), // Dots at specific positions
                        CharacterFilter::Exact('.'),
                    ))
                    .add_filter(PositionFilter::new(
                        PositionRange::Multiple(vec![0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14]), // Number positions
                        CharacterFilter::Numeric,
                    ));

                Some(
                    ValidationConfigBuilder::new()
                        .with_pattern_filters(ipv4_pattern)
                        .with_max_length(15) // XXX.XXX.XXX.XXX = up to 15 chars
                        .build(),
                )
            }
            3 => {
                // ๐Ÿท๏ธ Product Code (ABC-123-XYZ) - Mixed format sections
                // This showcases: Different rules for different sections
                let product_code_pattern = PatternFilters::new()
                    .add_filter(PositionFilter::new(
                        PositionRange::Range(0, 2), // First 3 positions: letters
                        CharacterFilter::Alphabetic,
                    ))
                    .add_filter(PositionFilter::new(
                        PositionRange::Multiple(vec![3, 7]), // Dashes
                        CharacterFilter::Exact('-'),
                    ))
                    .add_filter(PositionFilter::new(
                        PositionRange::Range(4, 6), // Middle 3 positions: numbers
                        CharacterFilter::Numeric,
                    ))
                    .add_filter(PositionFilter::new(
                        PositionRange::Range(8, 10), // Last 3 positions: letters
                        CharacterFilter::Alphabetic,
                    ));

                Some(
                    ValidationConfigBuilder::new()
                        .with_pattern_filters(product_code_pattern)
                        .with_max_length(11) // ABC-123-XYZ = 11 characters
                        .build(),
                )
            }
            4 => {
                // ๐Ÿ“… Date Code (2024W15) - Year + Week format
                // This showcases: From position filtering and mixed patterns
                let date_code_pattern = PatternFilters::new()
                    .add_filter(PositionFilter::new(
                        PositionRange::Range(0, 3), // Year: 4 digits
                        CharacterFilter::Numeric,
                    ))
                    .add_filter(PositionFilter::new(
                        PositionRange::Single(4), // Week indicator
                        CharacterFilter::Exact('W'),
                    ))
                    .add_filter(PositionFilter::new(
                        PositionRange::From(5), // Week number: rest are digits
                        CharacterFilter::Numeric,
                    ));

                Some(
                    ValidationConfigBuilder::new()
                        .with_pattern_filters(date_code_pattern)
                        .with_max_length(7) // 2024W15 = 7 characters
                        .build(),
                )
            }
            5 => {
                // ๐Ÿ”ข Binary (101010) - Only 0s and 1s
                // This showcases: OneOf filter with limited character set
                let binary_pattern = PatternFilters::new().add_filter(PositionFilter::new(
                    PositionRange::From(0), // All positions
                    CharacterFilter::OneOf(vec!['0', '1']),
                ));

                Some(
                    ValidationConfigBuilder::new()
                        .with_pattern_filters(binary_pattern)
                        .with_max_length(16) // Allow up to 16 binary digits
                        .build(),
                )
            }
            6 => {
                // ๐ŸŽฏ Complex ID (A1-B2C-3D4E) - Multiple overlapping rules
                // This showcases: Complex overlapping patterns and edge cases
                let complex_id_pattern = PatternFilters::new()
                    .add_filter(PositionFilter::new(
                        PositionRange::Multiple(vec![0, 3, 6, 8]), // Letter positions
                        CharacterFilter::Alphabetic,
                    ))
                    .add_filter(PositionFilter::new(
                        PositionRange::Multiple(vec![1, 4, 7, 9]), // Number positions
                        CharacterFilter::Numeric,
                    ))
                    .add_filter(PositionFilter::new(
                        PositionRange::Multiple(vec![2, 5]), // Dashes
                        CharacterFilter::Exact('-'),
                    ))
                    .add_filter(PositionFilter::new(
                        PositionRange::Single(5),    // Special case: override dash with letter C
                        CharacterFilter::Alphabetic, // This creates an interesting edge case
                    ));

                Some(
                    ValidationConfigBuilder::new()
                        .with_pattern_filters(complex_id_pattern)
                        .with_max_length(10) // A1-B2C-3D4E = 10 characters
                        .build(),
                )
            }
            7 => {
                // ๐Ÿš€ Custom Pattern - Advanced logic with custom function
                // This showcases: Custom validation function for complex rules
                let custom_pattern = PatternFilters::new().add_filter(PositionFilter::new(
                    PositionRange::From(0),
                    CharacterFilter::Custom(Arc::new(|c| {
                        // Advanced rule: Alternating vowels and consonants!
                        // Even positions (0,2,4...): vowels (a,e,i,o,u)
                        // Odd positions (1,3,5...): consonants
                        let vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];

                        // For demo purposes, we'll just accept alphabetic characters
                        // In real usage, you'd implement the alternating logic based on position
                        c.is_alphabetic()
                    })),
                ));

                Some(
                    ValidationConfigBuilder::new()
                        .with_pattern_filters(custom_pattern)
                        .with_max_length(12) // Allow up to 12 characters
                        .build(),
                )
            }
            _ => None,
        }
    }
}

// Key handling (same structure as before)
fn handle_key_press(
    key: KeyCode,
    modifiers: KeyModifiers,
    editor: &mut AdvancedPatternTextForm<AdvancedPatternData>,
) -> anyhow::Result<bool> {
    let mode = editor.mode();

    // Quit handling
    if (key == KeyCode::Char('q') && modifiers.contains(KeyModifiers::CONTROL))
        || (key == KeyCode::Char('c') && modifiers.contains(KeyModifiers::CONTROL))
        || key == KeyCode::F(10)
    {
        return Ok(false);
    }

    match (mode, key, modifiers) {
        // Mode transitions
        (AppMode::Nor, KeyCode::Char('i'), _) => {
            editor.enter_edit_mode();
            editor.clear_command_buffer();
        }
        (AppMode::Nor, KeyCode::Char('a'), _) => {
            editor.enter_append_mode();
            editor.clear_command_buffer();
        }
        (AppMode::Nor, KeyCode::Char('A'), _) => {
            editor.move_line_end();
            editor.enter_edit_mode();
            editor.clear_command_buffer();
        }
        (_, KeyCode::Esc, _) => {
            if mode == AppMode::Ins {
                editor.exit_edit_mode();
            } else {
                editor.clear_command_buffer();
            }
        }

        // Validation commands
        (AppMode::Nor, KeyCode::F(1), _) => {
            editor.toggle_validation();
        }

        // Movement in ReadOnly mode
        (AppMode::Nor, KeyCode::Char('h'), _) | (AppMode::Nor, KeyCode::Left, _) => {
            editor.move_left();
            editor.clear_command_buffer();
        }
        (AppMode::Nor, KeyCode::Char('l'), _) | (AppMode::Nor, KeyCode::Right, _) => {
            editor.move_right();
            editor.clear_command_buffer();
        }
        (AppMode::Nor, KeyCode::Char('j'), _) | (AppMode::Nor, KeyCode::Down, _) => {
            editor.move_down();
            editor.clear_command_buffer();
        }
        (AppMode::Nor, KeyCode::Char('k'), _) | (AppMode::Nor, KeyCode::Up, _) => {
            editor.move_up();
            editor.clear_command_buffer();
        }

        // Movement in Edit mode
        (AppMode::Ins, KeyCode::Left, _) => {
            editor.move_left();
        }
        (AppMode::Ins, KeyCode::Right, _) => {
            editor.move_right();
        }
        (AppMode::Ins, KeyCode::Up, _) => {
            editor.move_up();
        }
        (AppMode::Ins, KeyCode::Down, _) => {
            editor.move_down();
        }

        // Delete operations
        (AppMode::Ins, KeyCode::Backspace, _) => {
            editor.delete_backward()?;
        }
        (AppMode::Ins, KeyCode::Delete, _) => {
            editor.delete_forward()?;
        }

        // Tab navigation
        (_, KeyCode::Tab, _) => {
            editor.next_field();
        }
        (_, KeyCode::BackTab, _) => {
            editor.prev_field();
        }

        // Character input
        (AppMode::Ins, KeyCode::Char(c), m) if !m.contains(KeyModifiers::CONTROL) => {
            editor.insert_char(c)?;
        }

        // Debug info
        (AppMode::Nor, KeyCode::Char('?'), _) => {
            let summary = editor.editor.validation_summary();
            editor.set_debug_message(format!(
                "Field {}/{}, Pos {}, Mode: {:?}, Advanced patterns: {} configured",
                editor.current_field() + 1,
                editor.data_provider().field_count(),
                editor.cursor_position(),
                editor.mode(),
                summary.total_fields
            ));
        }

        _ => {
            if editor.has_pending_command() {
                editor.clear_command_buffer();
                editor.set_debug_message("Invalid command sequence".to_string());
            }
        }
    }

    Ok(true)
}

fn run_app<B: Backend<Error = io::Error>>(
    terminal: &mut Terminal<B>,
    mut editor: AdvancedPatternTextForm<AdvancedPatternData>,
) -> io::Result<()> {
    loop {
        terminal.draw(|f| ui(f, &editor))?;

        if let Event::Key(key) = event::read()? {
            match handle_key_press(key.code, key.modifiers, &mut editor) {
                Ok(should_continue) => {
                    if !should_continue {
                        break;
                    }
                }
                Err(e) => {
                    editor.set_debug_message(format!("Error: {e}"));
                }
            }
        }
    }

    Ok(())
}

fn ui(f: &mut Frame, editor: &AdvancedPatternTextForm<AdvancedPatternData>) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(8), Constraint::Length(15)])
        .split(f.area());

    render_canvas_default(f, chunks[0], &editor.editor);
    render_advanced_validation_status(f, chunks[1], editor);
}

fn render_advanced_validation_status(
    f: &mut Frame,
    area: Rect,
    editor: &AdvancedPatternTextForm<AdvancedPatternData>,
) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Status bar
            Constraint::Length(5), // Validation summary
            Constraint::Length(7), // Help
        ])
        .split(area);

    // Status bar
    let mode_text = match editor.mode() {
        AppMode::Ins => "INSERT | (bar cursor)",
        AppMode::Nor => "NORMAL โ–ˆ (block cursor)",
        _ => "NORMAL โ–ˆ (block cursor)",
    };

    let validation_status = editor.get_validation_status();
    let status_text = format!(
        "-- {} -- {} | Advanced Patterns: {}",
        mode_text,
        editor.debug_message(),
        validation_status
    );

    let status = Paragraph::new(Line::from(Span::raw(status_text))).block(
        Block::default()
            .borders(Borders::ALL)
            .title("๐Ÿš€ Advanced Pattern Validation"),
    );

    f.render_widget(status, chunks[0]);

    // Validation summary
    let summary = editor.editor.validation_summary();
    let field_info = match editor.current_field() {
        0 => "Time format (HH:MM) - Tests exact chars + numeric ranges",
        1 => "Hex color (#RRGGBB) - Tests OneOf filter with case insensitive",
        2 => "IPv4 address - Tests complex dot positioning",
        3 => "Product code (ABC-123-XYZ) - Tests section-based patterns",
        4 => "Date code (2024W15) - Tests From position filtering",
        5 => "Binary input - Tests limited character set (0,1 only)",
        6 => "Complex ID - Tests overlapping/conflicting rules",
        7 => "Custom pattern - Tests advanced custom validation logic",
        _ => "Unknown field",
    };

    let summary_text = if editor.validation_enabled {
        format!(
            "๐Ÿ“Š Advanced Pattern Summary: {} fields with complex rules\n\
             Current Field: {}\n\
             โœ… Valid: {}  โš ๏ธ  Warnings: {}  โŒ Errors: {}  ๐Ÿ“ˆ Progress: {:.0}%\n\
             ๐ŸŽฏ Pattern Focus: {}",
            summary.total_fields,
            editor.current_field() + 1,
            summary.valid_fields,
            summary.warning_fields,
            summary.error_fields,
            summary.completion_percentage() * 100.0,
            field_info
        )
    } else {
        "โŒ Advanced pattern validation is DISABLED\nPress F1 to enable and see the magic!"
            .to_string()
    };

    let summary_style = if summary.has_errors() {
        Style::default().fg(Color::Red)
    } else if summary.has_warnings() {
        Style::default().fg(Color::Yellow)
    } else {
        Style::default().fg(Color::Green)
    };

    let validation_summary = Paragraph::new(summary_text)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("๐ŸŽฏ Advanced Pattern Analysis"),
        )
        .style(summary_style)
        .wrap(Wrap { trim: true });

    f.render_widget(validation_summary, chunks[1]);

    // Help text
    let help_text = match editor.mode() {
        AppMode::Nor => {
            "๐Ÿš€ ADVANCED PATTERN SHOWCASE - Each field demonstrates different edge cases!\n\
             ๐Ÿ• Time: Numeric+exact chars  ๐ŸŽจ Hex: OneOf with case-insensitive  ๐ŸŒ IPv4: Complex positioning\n\
             ๐Ÿท๏ธ  Product: Multi-section rules  ๐Ÿ“… Date: From-position filtering  ๐Ÿ”ข Binary: Limited charset\n\
             ๐ŸŽฏ Complex: Overlapping rules  ๐Ÿš€ Custom: Advanced logic functions\n\
             \n\
             Movement: hjkl/arrows=move, Tab/Shift+Tab=fields, i/a=insert, F1=toggle, ?=info"
        }
        AppMode::Ins => {
            "โœ๏ธ  INSERT MODE - Testing advanced pattern validation!\n\
             Each character is validated against complex rules in real-time\n\
             Try entering invalid characters to see detailed error messages\n\
             arrows=move, Backspace/Del=delete, Esc=normal, Tab=next field"
        }
        _ => "๐Ÿš€ Advanced Pattern Validation Active!",
    };

    let help = Paragraph::new(help_text)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("๐ŸŽฏ Advanced Pattern Commands & Info"),
        )
        .style(Style::default().fg(Color::Gray))
        .wrap(Wrap { trim: true });

    f.render_widget(help, chunks[2]);
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("๐Ÿš€ Canvas Advanced Pattern Validation Demo");
    println!("โœ… validation feature: ENABLED");
    println!("โœ… gui feature: ENABLED");
    println!("โœ… cursor-style feature: ENABLED");
    println!("๐ŸŽฏ Advanced pattern filtering: ACTIVE");
    println!("๐Ÿงช Edge cases and complex patterns: READY");
    println!("๐Ÿ’ก Each field showcases different validation capabilities!");
    println!();

    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let data = AdvancedPatternData::new();
    let mut editor = AdvancedPatternTextForm::new(data);

    // Initialize with normal mode - library automatically sets block cursor
    editor.set_mode(AppMode::Nor);

    CursorManager::update_for_mode(AppMode::Nor)?;

    let res = run_app(&mut terminal, editor);

    CursorManager::reset()?;

    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture
    )?;
    terminal.show_cursor()?;

    if let Err(err) = res {
        println!("{err:?}");
    }

    println!("๐Ÿš€ Advanced pattern validation demo completed!");
    println!("๐ŸŽฏ Hope you enjoyed seeing all the edge cases in action!");
    Ok(())
}