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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
use std::net::IpAddr;
/**
 * MIT License
 *
 * tuifeed - Copyright (c) 2021 Christian Visintin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
use std::num::{NonZeroU8, NonZeroU32};
use std::path::PathBuf;

use anyhow::{Result, bail};
use include_dir::DirEntry;
use termusiclib::THEME_DIR;
use termusiclib::config::v2::server::{
    Backend, ComProtocol, PositionYesNo, PositionYesNoLower, RememberLastPosition,
};
use termusiclib::config::v2::tui::Alignment as XywhAlign;
use termusiclib::config::v2::tui::theme::ThemeColors;
use termusiclib::utils::{get_app_config_path, get_pin_yin};
use tuirealm::props::{PropPayload, PropValue, Style, TableBuilder, TextSpan};
use tuirealm::ratatui::layout::{Constraint, Layout, Rect};
use tuirealm::ratatui::widgets::Clear;
use tuirealm::{AttrValue, Attribute, Frame, State, StateValue};

use crate::ui::Application;
use crate::ui::components::ConfigSavePopup;
use crate::ui::components::config_editor::update::THEMES_WITHOUT_FILES;
use crate::ui::components::raw::dynamic_height_grid::DynamicHeightGrid;
use crate::ui::components::raw::uniform_dynamic_grid::UniformDynamicGrid;
use crate::ui::ids::{Id, IdCEGeneral, IdCETheme, IdConfigEditor, IdKey, IdKeyGlobal, IdKeyOther};
use crate::ui::model::{Model, UserEvent};
use crate::ui::msg::{ConfigEditorLayout, KFGLOBAL_FOCUS_ORDER, KFOTHER_FOCUS_ORDER, Msg};
use crate::ui::utils::draw_area_in_absolute;

// NOTE: the macros either have to be in a different file OR be defined *before* they are used, otherwise they are not in scope

/// Chain many values together and saturated added value from it.
///
/// Equivalent to manually chaining `val1.saturating_add(val2.saturating_add(val3))`.
macro_rules! sat_add {
    ($first:expr$(,)?) => {
        $first
    };
    (
        $first:expr
        $(
            , $second:expr
        )* $(,)?
    ) => {
        $first.saturating_add(sat_add!($($second,)*))
    }
}

/// Convert to a `Box<[]>`, but allow spacing without formatting messing it up.
///
/// Equivalent to manually doing `Box::from([val1, val2])`.
macro_rules! to_boxed_slice {
    ($first:expr$(,)?) => {
        $first
    };
    (
        $first:expr
        $(
            , $second:expr
        )* $(,)?
    ) => {
        Box::from([$first, $($second,)*])
    }
}

/// Chain `app.views` together without having to repeat ourselfs (DRY).
///
/// A macro here uses way less lines than manually writing each `app.view` call out by hand.
///
/// Equivalent to manually writing out `app.view($id, $f, $cell)` for each id and cell.
macro_rules! app_view {
    (
        $app:expr, $f:expr,
        $($id:expr => $cell:expr$(,)?)*
    ) => {
        $($app.view($id, $f, $cell);)*
    }
}

impl Model {
    pub fn view_config_editor(&mut self) {
        self.terminal
            .raw_mut()
            .draw(|f| {
                // The common style to apply, even in areas that are unused.
                // To not look out-of-place, we need to get the preview theme style if on that layout, to properly preview.
                let common_style =
                    if self.config_editor.last_layout == ConfigEditorLayout::ThemeAndColor {
                        Style::new().bg(self.config_editor.theme.fallback_background())
                    } else {
                        Style::new().bg(self
                            .config_tui
                            .read_recursive()
                            .settings
                            .theme
                            .fallback_background())
                    };
                let chunk_main = Self::view_config_editor_common(&mut self.app, f, common_style);

                match self.config_editor.last_layout {
                    ConfigEditorLayout::General => {
                        Self::view_config_editor_general(&mut self.app, f, chunk_main);
                    }
                    ConfigEditorLayout::ThemeAndColor => {
                        Self::view_config_editor_color(&mut self.app, f, chunk_main);
                    }
                    ConfigEditorLayout::KeyGlobal => {
                        Self::view_config_editor_key1(&mut self.app, f, chunk_main);
                    }
                    ConfigEditorLayout::KeyOther => {
                        Self::view_config_editor_key2(&mut self.app, f, chunk_main);
                    }
                }

                Self::view_config_editor_popups(&mut self.app, f);
            })
            .expect("Expected to draw without error");
    }

    /// Split the frame area into header, main and footer,
    /// also Draw the Header and footer and return the main area.
    fn view_config_editor_common(
        app: &mut Application<Id, Msg, UserEvent>,
        f: &mut Frame<'_>,
        common_style: Style,
    ) -> Rect {
        let [header, chunk_main, footer] = Layout::vertical([
            Constraint::Length(2), // config header
            Constraint::Min(3),
            Constraint::Length(1), // config footer
        ])
        .areas(f.area());

        app.view(&Id::ConfigEditor(IdConfigEditor::Header), f, header);

        // apply basic style to whole area, otherwise the style will only be applied to the area used by components
        f.buffer_mut().set_style(chunk_main, common_style);

        // draw before main chunk, to easily tell if something is overdrawing
        app.view(&Id::ConfigEditor(IdConfigEditor::Footer), f, footer);

        chunk_main
    }

    /// Draw the keys for tab "General"
    fn view_config_editor_general(
        app: &mut Application<Id, Msg, UserEvent>,
        f: &mut Frame<'_>,
        chunk_main: Rect,
    ) {
        let focus_elem = app
            .focus()
            .and_then(|v| {
                if let Id::ConfigEditor(id) = *v {
                    Some(id)
                } else {
                    None
                }
            })
            .and_then(|v| {
                if let IdConfigEditor::General(v) = v {
                    Some(match v {
                        IdCEGeneral::MusicDir => 0,
                        IdCEGeneral::ExitConfirmation => 1,
                        IdCEGeneral::PlaylistDisplaySymbol => 2,
                        IdCEGeneral::PlaylistRandomTrack => 3,
                        IdCEGeneral::PlaylistRandomAlbum => 4,
                        IdCEGeneral::PodcastDir => 5,
                        IdCEGeneral::PodcastSimulDownload => 6,
                        IdCEGeneral::PodcastMaxRetries => 7,
                        IdCEGeneral::AlbumPhotoAlign => 8,
                        IdCEGeneral::SaveLastPosition => 9,
                        IdCEGeneral::SeekStep => 10,
                        IdCEGeneral::KillDamon => 11,
                        IdCEGeneral::PlayerUseMpris => 12,
                        IdCEGeneral::PlayerUseDiscord => 13,
                        IdCEGeneral::PlayerPort => 14,
                        IdCEGeneral::PlayerAddress => 15,
                        IdCEGeneral::PlayerProtocol => 16,
                        IdCEGeneral::PlayerUDSPath => 17,
                        IdCEGeneral::PlayerBackend => 18,
                        IdCEGeneral::ExtraYtdlpArgs => 19,
                    })
                } else {
                    None
                }
            });

        let cells = UniformDynamicGrid::new(20, 3, 56 + 2)
            .draw_row_low_space()
            .distribute_row_space()
            .focus_node(focus_elem)
            .split(chunk_main);

        app_view! {
            app, f,

            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::MusicDir)) => cells[0],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::ExitConfirmation)) => cells[1],

            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlaylistDisplaySymbol)) => cells[2],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlaylistRandomTrack)) => cells[3],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlaylistRandomAlbum)) => cells[4],

            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PodcastDir)) => cells[5],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PodcastSimulDownload)) => cells[6],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PodcastMaxRetries)) => cells[7],

            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::AlbumPhotoAlign)) => cells[8],

            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::SaveLastPosition)) => cells[9],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::SeekStep)) => cells[10],

            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::KillDamon)) => cells[11],

            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlayerUseMpris)) => cells[12],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlayerUseDiscord)) => cells[13],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlayerPort)) => cells[14],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlayerAddress)) => cells[15],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlayerProtocol)) => cells[16],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlayerUDSPath)) => cells[17],
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlayerBackend)) => cells[18],

            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::ExtraYtdlpArgs)) => cells[19],
        }
    }

    /// Draw common Popups while in the config editor
    fn view_config_editor_popups(app: &mut Application<Id, Msg, UserEvent>, f: &mut Frame<'_>) {
        // -- popups
        if app.mounted(&Id::ConfigEditor(IdConfigEditor::ConfigSavePopup)) {
            let popup = draw_area_in_absolute(f.area(), 50, 3);
            f.render_widget(Clear, popup);
            app.view(&Id::ConfigEditor(IdConfigEditor::ConfigSavePopup), f, popup);
        }
        if app.mounted(&Id::ErrorPopup) {
            let popup = draw_area_in_absolute(f.area(), 50, 4);
            f.render_widget(Clear, popup);
            app.view(&Id::ErrorPopup, f, popup);
        }
    }

    /// Draw the keys for tab "Themes and Colors"
    #[allow(clippy::too_many_lines)]
    fn view_config_editor_color(
        app: &mut Application<Id, Msg, UserEvent>,
        f: &mut Frame<'_>,
        chunk_main: Rect,
    ) {
        /// Gets the state of `Id::ConfigEditor(id)` and if it has a `State::One`, returns `yes`, otherwise `no`.
        ///
        /// Macro to apply "DRY"(Dont-Repeat-Yourself) to reduce function length.
        macro_rules! is_expanded {
            ($id:expr, $yes:expr, $no:expr) => {
                match app.state(&Id::ConfigEditor($id)) {
                    Ok(State::One(_)) => $no,
                    _ => $yes,
                }
            };
        }

        let library_foreground_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::LibraryForeground), 8, 3);
        let library_background_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::LibraryBackground), 8, 3);
        let library_border_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::LibraryBorder), 8, 3);
        let library_highlight_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::LibraryHighlight), 8, 3);

        let playlist_foreground_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::PlaylistForeground), 8, 3);
        let playlist_background_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::PlaylistBackground), 8, 3);
        let playlist_border_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::PlaylistBorder), 8, 3);
        let playlist_highlight_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::PlaylistHighlight), 8, 3);

        let progress_foreground_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::ProgressForeground), 8, 3);
        let progress_background_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::ProgressBackground), 8, 3);
        let progress_border_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::ProgressBorder), 8, 3);

        let lyric_foreground_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::LyricForeground), 8, 3);
        let lyric_background_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::LyricBackground), 8, 3);
        let lyric_border_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::LyricBorder), 8, 3);

        let important_popup_foreground_len: u16 = is_expanded!(
            IdConfigEditor::Theme(IdCETheme::ImportantPopupForeground),
            8,
            3
        );
        let important_popup_background_len: u16 = is_expanded!(
            IdConfigEditor::Theme(IdCETheme::ImportantPopupBackground),
            8,
            3
        );
        let important_popup_border_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::ImportantPopupBorder), 8, 3);

        let fallback_foreground_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::FallbackForeground), 8, 3);
        let fallback_background_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::FallbackBackground), 8, 3);
        let fallback_border_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::FallbackBorder), 8, 3);
        let fallback_highlight_len: u16 =
            is_expanded!(IdConfigEditor::Theme(IdCETheme::FallbackHighlight), 8, 3);

        let [left, right] = Layout::horizontal([Constraint::Ratio(1, 4), Constraint::Ratio(3, 4)])
            .areas(chunk_main);

        let library_height = sat_add! {
            1u16, // label
            library_foreground_len,
            library_background_len,
            library_border_len,
            library_highlight_len,
            3u16, // highlight symbol
        };
        let playlist_height = sat_add! {
            1u16, // label
            playlist_foreground_len,
            playlist_background_len,
            playlist_border_len,
            playlist_highlight_len,
            3u16, // highlight symbol
            3u16, // current track symbol,
        };
        let progress_height = sat_add! {
            1u16, // label
            progress_foreground_len,
            progress_background_len,
            progress_border_len
        };
        let lyric_height = sat_add! {
            1u16, // label
            lyric_foreground_len,
            lyric_background_len,
            lyric_border_len
        };
        let important_popup_height = sat_add! {
            1u16, // label
            important_popup_foreground_len,
            important_popup_background_len,
            important_popup_border_len
        };
        let fallback_height = sat_add! {
            1u16, // label
            fallback_foreground_len,
            fallback_background_len,
            fallback_border_len,
            fallback_highlight_len
        };

        // NOTE: the elements below have to be in the order they are draw and blurred(focused) in:
        let elem_height = to_boxed_slice! {
            library_height,
            playlist_height,
            progress_height,
            lyric_height,
            important_popup_height,
            fallback_height,
        };

        let focus_elem = app
            .focus()
            .and_then(|v| {
                if let Id::ConfigEditor(id) = *v {
                    Some(id)
                } else {
                    None
                }
            })
            .and_then(|v| {
                if let IdConfigEditor::Theme(v) = v {
                    Some(match v {
                        IdCETheme::LibraryLabel
                        | IdCETheme::LibraryForeground
                        | IdCETheme::LibraryBackground
                        | IdCETheme::LibraryBorder
                        | IdCETheme::LibraryHighlight
                        | IdCETheme::LibraryHighlightSymbol => 0,
                        IdCETheme::PlaylistLabel
                        | IdCETheme::PlaylistForeground
                        | IdCETheme::PlaylistBackground
                        | IdCETheme::PlaylistBorder
                        | IdCETheme::PlaylistHighlight
                        | IdCETheme::PlaylistHighlightSymbol
                        | IdCETheme::CurrentlyPlayingTrackSymbol => 1,
                        IdCETheme::ProgressLabel
                        | IdCETheme::ProgressForeground
                        | IdCETheme::ProgressBackground
                        | IdCETheme::ProgressBorder => 2,
                        IdCETheme::LyricLabel
                        | IdCETheme::LyricForeground
                        | IdCETheme::LyricBackground
                        | IdCETheme::LyricBorder => 3,
                        IdCETheme::ImportantPopupLabel
                        | IdCETheme::ImportantPopupForeground
                        | IdCETheme::ImportantPopupBackground
                        | IdCETheme::ImportantPopupBorder => 4,
                        IdCETheme::FallbackLabel
                        | IdCETheme::FallbackForeground
                        | IdCETheme::FallbackBackground
                        | IdCETheme::FallbackBorder
                        | IdCETheme::FallbackHighlight => 5,
                        IdCETheme::ThemeSelectTable => return None,
                    })
                } else {
                    None
                }
            });

        let cells = DynamicHeightGrid::new(elem_height, 16 + 2)
            .with_row_spacing(1)
            .draw_row_low_space()
            .distribute_row_space()
            .focus_node(focus_elem)
            .split(right);

        let chunks_library = Layout::vertical([
            Constraint::Length(1),
            Constraint::Length(library_foreground_len),
            Constraint::Length(library_background_len),
            Constraint::Length(library_border_len),
            Constraint::Length(library_highlight_len),
            Constraint::Length(3),
            Constraint::Min(0),
        ])
        .split(cells[0]);

        let chunks_playlist = Layout::vertical([
            Constraint::Length(1),
            Constraint::Length(playlist_foreground_len),
            Constraint::Length(playlist_background_len),
            Constraint::Length(playlist_border_len),
            Constraint::Length(playlist_highlight_len),
            Constraint::Length(3),
            Constraint::Length(3),
            Constraint::Min(0),
        ])
        .split(cells[1]);

        let chunks_progress = Layout::vertical([
            Constraint::Length(1),
            Constraint::Length(progress_foreground_len),
            Constraint::Length(progress_background_len),
            Constraint::Length(progress_border_len),
            Constraint::Min(0),
        ])
        .split(cells[2]);

        let chunks_lyric = Layout::vertical([
            Constraint::Length(1),
            Constraint::Length(lyric_foreground_len),
            Constraint::Length(lyric_background_len),
            Constraint::Length(lyric_border_len),
            Constraint::Min(0),
        ])
        .split(cells[3]);

        let chunks_important_popup = Layout::vertical([
            Constraint::Length(1),
            Constraint::Length(important_popup_foreground_len),
            Constraint::Length(important_popup_background_len),
            Constraint::Length(important_popup_border_len),
            Constraint::Min(0),
        ])
        .split(cells[4]);

        let chunks_fallback = Layout::vertical([
            Constraint::Length(1),
            Constraint::Length(fallback_foreground_len),
            Constraint::Length(fallback_background_len),
            Constraint::Length(fallback_border_len),
            Constraint::Length(fallback_highlight_len),
            Constraint::Min(0),
        ])
        .split(cells[5]);

        app_view! {
            app, f,

            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::ThemeSelectTable)) => left,

            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::LibraryLabel)) => chunks_library[0],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::LibraryForeground)) => chunks_library[1],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::LibraryBackground)) => chunks_library[2],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::LibraryBorder)) => chunks_library[3],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::LibraryHighlight)) => chunks_library[4],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::LibraryHighlightSymbol)) => chunks_library[5],

            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::PlaylistLabel)) => chunks_playlist[0],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::PlaylistForeground)) => chunks_playlist[1],

            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::PlaylistBackground)) => chunks_playlist[2],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::PlaylistBorder)) => chunks_playlist[3],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::PlaylistHighlight)) => chunks_playlist[4],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::PlaylistHighlightSymbol)) => chunks_playlist[5],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::CurrentlyPlayingTrackSymbol)) => chunks_playlist[6],

            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::ProgressLabel)) => chunks_progress[0],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::ProgressForeground)) => chunks_progress[1],

            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::ProgressBackground)) => chunks_progress[2],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::ProgressBorder)) => chunks_progress[3],

            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::LyricLabel)) => chunks_lyric[0],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::LyricForeground)) => chunks_lyric[1],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::LyricBackground)) => chunks_lyric[2],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::LyricBorder)) => chunks_lyric[3],

            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::ImportantPopupLabel)) => chunks_important_popup[0],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::ImportantPopupForeground)) => chunks_important_popup[1],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::ImportantPopupBackground)) => chunks_important_popup[2],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::ImportantPopupBorder)) => chunks_important_popup[3],

            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::FallbackLabel)) => chunks_fallback[0],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::FallbackForeground)) => chunks_fallback[1],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::FallbackBackground)) => chunks_fallback[2],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::FallbackBorder)) => chunks_fallback[3],
            &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::FallbackHighlight)) => chunks_fallback[4],
        }
    }

    /// Draw the keys for tab "Key Global"
    fn view_config_editor_key1(
        app: &mut Application<Id, Msg, UserEvent>,
        f: &mut Frame<'_>,
        chunk_main: Rect,
    ) {
        KeyDisplay::new(KFGLOBAL_FOCUS_ORDER, 23 + 2).view(app, chunk_main, f);
    }

    /// Draw the keys for tab "Key Other"
    fn view_config_editor_key2(
        app: &mut Application<Id, Msg, UserEvent>,
        f: &mut Frame<'_>,
        chunk_main: Rect,
    ) {
        KeyDisplay::new(KFOTHER_FOCUS_ORDER, 25 + 2).view(app, chunk_main, f);
    }

    /// Mount the Config Editor.
    pub fn mount_config_editor(&mut self) {
        self.mount_config_editor_components()
            .expect("Expected Config Editor Components to mount correctly");

        if let Err(e) = self.theme_select_load_themes() {
            self.mount_error_popup(e.context("load themes"));
        }
        self.theme_select_sync(None);
        if let Err(e) = self.update_photo() {
            self.mount_error_popup(e.context("update_photo"));
        }
    }

    /// Mount all the Config Editor Components.
    fn mount_config_editor_components(&mut self) -> Result<()> {
        // when opening the config editor, always be on the default / first page, instead of the one it was closed on
        self.config_editor.last_layout = ConfigEditorLayout::default();

        self.remount_config_general()?;

        self.remount_config_color(&self.config_tui.clone(), None)?;

        self.remount_config_keys()?;

        // Set default focus component for the config editor
        self.app.active(&Id::ConfigEditor(IdConfigEditor::General(
            IdCEGeneral::MusicDir,
        )))?;

        // remount last, as it is sensitive to the focused component
        self.remount_config_header_footer()?;

        Ok(())
    }

    /// Unmount the Config Editor and trigger reload of necessary data.
    pub fn umount_config_editor(&mut self) {
        self.playlist_reload();
        self.database_reload();
        self.progress_reload();
        self.mount_label_help();
        self.lyric_reload();

        self.umount_config_editor_components()
            .expect("Expected Config Editor Components to unmount correctly");

        self.remount_global_listener()
            .expect("Expected Global Hotkey Listener to remount correctly");

        if let Err(e) = self.update_photo() {
            self.mount_error_popup(e.context("update_photo"));
        }
    }

    /// Unmount all the config editor components.
    fn umount_config_editor_components(&mut self) -> Result<()> {
        self.umount_config_header_footer()?;

        self.umount_config_general()?;

        self.umount_config_color()?;

        self.umount_config_keys()?;

        Ok(())
    }

    /// Mount quit popup
    pub fn mount_config_save_popup(&mut self) {
        assert!(
            self.app
                .remount(
                    Id::ConfigEditor(IdConfigEditor::ConfigSavePopup),
                    Box::new(ConfigSavePopup::new(self.config_tui.clone())),
                    vec![]
                )
                .is_ok()
        );
        assert!(
            self.app
                .active(&Id::ConfigEditor(IdConfigEditor::ConfigSavePopup))
                .is_ok()
        );
    }

    #[allow(clippy::too_many_lines)]
    pub fn collect_config_data(&mut self) -> Result<()> {
        let mut config_tui = self.config_tui.write();
        match self.config_editor.key_config.check_keys() {
            Ok(()) => config_tui.settings.keys = self.config_editor.key_config.clone(),
            Err(err) => bail!(err),
        }
        config_tui.settings.theme = self.config_editor.theme.clone();

        let mut config_server = self.config_server.write();

        if let Ok(State::One(StateValue::String(music_dir))) = self.app.state(&Id::ConfigEditor(
            IdConfigEditor::General(IdCEGeneral::MusicDir),
        )) {
            let vec = music_dir
                .split(';')
                .map(PathBuf::from)
                .filter(|p| {
                    let absolute_dir = shellexpand::path::tilde(p);
                    absolute_dir.exists()
                })
                .collect();
            config_server.settings.player.music_dirs = vec;
        }

        if let Ok(State::One(StateValue::Usize(exit_confirmation))) = self.app.state(
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::ExitConfirmation)),
        ) {
            config_tui.settings.behavior.confirm_quit = matches!(exit_confirmation, 0);
        }

        if let Ok(State::One(StateValue::Usize(display_symbol))) = self.app.state(
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlaylistDisplaySymbol)),
        ) {
            config_tui
                .settings
                .theme
                .style
                .playlist
                .use_loop_mode_symbol = matches!(display_symbol, 0);
        }

        if let Ok(State::One(StateValue::String(random_track_quantity_str))) = self.app.state(
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlaylistRandomTrack)),
        ) && let Ok(quantity) = random_track_quantity_str.parse::<NonZeroU32>()
        {
            config_server.settings.player.random_track_quantity = quantity;
        }

        if let Ok(State::One(StateValue::String(random_album_quantity_str))) = self.app.state(
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlaylistRandomAlbum)),
        ) && let Ok(quantity) = random_album_quantity_str.parse::<NonZeroU32>()
        {
            config_server.settings.player.random_album_min_quantity = quantity;
        }

        if let Ok(State::One(StateValue::String(podcast_dir))) = self.app.state(&Id::ConfigEditor(
            IdConfigEditor::General(IdCEGeneral::PodcastDir),
        )) {
            let absolute_dir = shellexpand::path::tilde(&podcast_dir);
            if absolute_dir.exists() {
                config_server.settings.podcast.download_dir = absolute_dir.into_owned();
            }
        }
        if let Ok(State::One(StateValue::String(podcast_simul_download))) = self.app.state(
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PodcastSimulDownload)),
        ) && let Ok(quantity) = podcast_simul_download.parse::<NonZeroU8>()
        {
            config_server.settings.podcast.concurrent_downloads_max = quantity;
        }
        if let Ok(State::One(StateValue::String(podcast_max_retries))) = self.app.state(
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PodcastMaxRetries)),
        ) && let Ok(quantity) = podcast_max_retries.parse::<u8>()
        {
            if (1..11).contains(&quantity) {
                config_server.settings.podcast.max_download_retries = quantity;
            } else {
                bail!(" It's not recommended to set max retries to more than 10. ");
            }
        }
        if let Ok(State::One(StateValue::Usize(align))) = self.app.state(&Id::ConfigEditor(
            IdConfigEditor::General(IdCEGeneral::AlbumPhotoAlign),
        )) {
            let align = match align {
                0 => XywhAlign::BottomRight,
                1 => XywhAlign::BottomLeft,
                2 => XywhAlign::TopRight,
                _ => XywhAlign::TopLeft,
            };
            config_tui.settings.coverart.align = align;
        }

        if let Ok(State::One(StateValue::Usize(save_last_position))) = self.app.state(
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::SaveLastPosition)),
        ) {
            // NOTE: value "0" means to not save the value
            if save_last_position != 0 {
                let new_val = match save_last_position {
                    1 => RememberLastPosition::All(PositionYesNo::Simple(PositionYesNoLower::No)),
                    2 => RememberLastPosition::All(PositionYesNo::Simple(PositionYesNoLower::Yes)),
                    // only 0,1,2 exist here
                    _ => unreachable!(),
                };

                config_server.settings.player.remember_position = new_val;
            }

            // let save_last_position = match save_last_position {
            //     0 => LastPosition::Auto,
            //     1 => LastPosition::No,
            //     2 => LastPosition::Yes,
            //     _ => bail!(" Save last position must be set to auto, yes or no."),
            // };
            // config_server.settings.player.remember_position = save_last_position;
        }

        if let Ok(State::One(StateValue::Usize(seek_step))) = self.app.state(&Id::ConfigEditor(
            IdConfigEditor::General(IdCEGeneral::SeekStep),
        )) {
            // NOTE: seek_step is currently unsupported to be set
            let _ = seek_step;

            // let seek_step = match seek_step {
            //     0 => SeekStep::Auto,
            //     1 => SeekStep::Short,
            //     2 => SeekStep::Long,
            //     _ => bail!(" Unknown player step length provided."),
            // };
            // config_server.settings.player.seek_step = seek_step;
        }

        if let Ok(State::One(StateValue::Usize(kill_daemon))) = self.app.state(&Id::ConfigEditor(
            IdConfigEditor::General(IdCEGeneral::KillDamon),
        )) {
            config_tui.settings.behavior.quit_server_on_exit = matches!(kill_daemon, 0);
        }

        if let Ok(State::One(StateValue::Usize(player_use_mpris))) = self.app.state(
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlayerUseMpris)),
        ) {
            config_server.settings.player.use_mediacontrols = matches!(player_use_mpris, 0);
        }

        if let Ok(State::One(StateValue::Usize(player_use_discord))) = self.app.state(
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::PlayerUseDiscord)),
        ) {
            config_server.settings.player.set_discord_status = matches!(player_use_discord, 0);
        }

        if let Ok(State::One(StateValue::String(player_port))) = self.app.state(&Id::ConfigEditor(
            IdConfigEditor::General(IdCEGeneral::PlayerPort),
        )) && let Ok(port) = player_port.parse::<u16>()
        {
            if (1024..u16::MAX).contains(&port) {
                config_server.settings.com.port = port;
            } else {
                bail!(" It's not recommended to use ports below 1024 for the player. ");
            }
        }

        if let Ok(State::One(StateValue::String(player_port))) = self.app.state(&Id::ConfigEditor(
            IdConfigEditor::General(IdCEGeneral::PlayerAddress),
        )) && let Ok(addr) = player_port.parse::<IpAddr>()
        {
            config_server.settings.com.address = addr;
        }

        if let Ok(State::One(StateValue::Usize(align))) = self.app.state(&Id::ConfigEditor(
            IdConfigEditor::General(IdCEGeneral::PlayerProtocol),
        )) {
            let protocol = match align {
                0 => ComProtocol::HTTP,
                1 => {
                    // the config will support either value on any system, but will fail to actually start on non-unix systems
                    if cfg!(not(unix)) {
                        bail!("UDS Protocol is only supported on unix systems");
                    }
                    ComProtocol::UDS
                }
                // numbers are specified in "PlayerProtocol"
                _ => unreachable!(),
            };
            config_server.settings.com.protocol = protocol;
        }

        if let Ok(State::One(StateValue::String(podcast_dir))) = self.app.state(&Id::ConfigEditor(
            IdConfigEditor::General(IdCEGeneral::PlayerUDSPath),
        )) {
            let abs_path = shellexpand::path::tilde(&podcast_dir);

            if !abs_path.has_root()
                || abs_path.file_name().is_none()
                || abs_path.extension().is_none_or(|v| v != "socket")
            {
                bail!(
                    "Invalid UDS socket Path.\nPath need to be absolute and end with \".socket\"."
                );
            }

            config_server.settings.com.socket_path = abs_path.into_owned();
        }

        if let Ok(State::One(StateValue::Usize(align))) = self.app.state(&Id::ConfigEditor(
            IdConfigEditor::General(IdCEGeneral::PlayerBackend),
        )) {
            let backend = match align {
                0 => Backend::Rusty,
                1 => Backend::Mpv,
                2 => Backend::Gstreamer,
                // numbers are specified in "PlayerBackend"
                _ => unreachable!(),
            };
            config_server.settings.player.backend = backend;
        }

        if let Ok(State::One(StateValue::String(extra_ytdlp_args))) = self.app.state(
            &Id::ConfigEditor(IdConfigEditor::General(IdCEGeneral::ExtraYtdlpArgs)),
        ) {
            config_tui.settings.ytdlp.extra_args = extra_ytdlp_args;
        }
        Ok(())
    }

    /// Extract all Themes to actual locations that can be loaded
    pub fn theme_extract_all() -> Result<()> {
        let mut path = get_app_config_path()?;
        path.push("themes");
        if !path.exists() {
            std::fs::create_dir_all(&path)?;
        }

        let base_path = &path;
        for entry in THEME_DIR.entries() {
            let path = base_path.join(entry.path());

            match entry {
                DirEntry::Dir(d) => {
                    std::fs::create_dir_all(&path)?;
                    d.extract(base_path)?;
                }
                DirEntry::File(f) => {
                    if !path.exists() {
                        std::fs::write(path, f.contents())?;
                    }
                }
            }
        }

        Ok(())
    }

    /// Find all themes in the `config/themes` directory and add them to be selected for preview
    pub fn theme_select_load_themes(&mut self) -> Result<()> {
        let mut path = get_app_config_path()?;
        path.push("themes");

        if let Ok(paths) = std::fs::read_dir(path) {
            self.config_editor.themes.clear();
            let mut paths: Vec<_> = paths.filter_map(std::result::Result::ok).collect();

            paths.sort_by_cached_key(|k| get_pin_yin(&k.file_name().to_string_lossy()));
            for entry in paths {
                let path = entry.path();
                let Some(stem) = path.file_stem() else {
                    warn!("Theme {:#?} does not have a filestem!", path.display());

                    continue;
                };

                self.config_editor
                    .themes
                    .push(stem.to_string_lossy().to_string());
            }
        }

        Ok(())
    }

    /// Build the theme UI table and select the current theme
    pub fn theme_select_sync(&mut self, previous_index: Option<usize>) {
        let mut table: TableBuilder = TableBuilder::default();

        table
            .add_col(TextSpan::new(0.to_string()))
            .add_col(TextSpan::new("Termusic Default"));
        table.add_row();
        table
            .add_col(TextSpan::new(1.to_string()))
            .add_col(TextSpan::new("Native"));

        for (idx, record) in self.config_editor.themes.iter().enumerate() {
            table.add_row();

            // idx + X as 0 until X entries are termusic default, always existing themes
            table
                .add_col(TextSpan::new((idx + THEMES_WITHOUT_FILES).to_string()))
                .add_col(TextSpan::new(record));
        }

        let table = table.build();
        self.app
            .attr(
                &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::ThemeSelectTable)),
                Attribute::Content,
                AttrValue::Table(table),
            )
            .ok();

        // select theme currently used
        let index = if let Some(index) = previous_index {
            index
        } else {
            let mut index = None;
            if let Some(current_file_name) = self.config_editor.theme.theme.file_name.as_ref() {
                for (idx, name) in self.config_editor.themes.iter().enumerate() {
                    if name == current_file_name {
                        // idx + X as 0 until X entries are termusic default, always existing themes
                        index = Some(idx + THEMES_WITHOUT_FILES);
                        break;
                    }
                }
            } else if self.config_editor.theme.theme.name == ThemeColors::full_default().name {
                index = Some(0);
            } else if self.config_editor.theme.theme.name == ThemeColors::full_native().name {
                index = Some(1);
            }

            index.unwrap_or(0)
        };
        assert!(
            self.app
                .attr(
                    &Id::ConfigEditor(IdConfigEditor::Theme(IdCETheme::ThemeSelectTable)),
                    Attribute::Value,
                    AttrValue::Payload(PropPayload::One(PropValue::Usize(index))),
                )
                .is_ok()
        );
    }
}

/// Enum which determines what [`KeyDisplay`] will and look for focus.
#[derive(Debug, Clone, Copy, PartialEq)]
enum KeyDisplayType {
    Global,
    Other,
}

/// Helper type to draw "Key" tabs.
#[derive(Debug)]
struct KeyDisplay<'a> {
    elems: &'a [IdKey],
    discriminant: KeyDisplayType,
    width: u16,
}

impl<'a> KeyDisplay<'a> {
    /// Create a new instance, where the elements `elems` are drawn in that order with at least a width of `width`.
    ///
    /// NOTE: all given [`IdKey`]s need to be of the same discriminant!
    pub fn new(elems: &'a [IdKey], width: u16) -> Self {
        // figure out which kind of tab is draw by looking at the first element
        let discriminant = std::mem::discriminant(&IdConfigEditor::from(&elems[0]));

        let discriminant = if discriminant
            == std::mem::discriminant(&IdConfigEditor::KeyGlobal(IdKeyGlobal::Config))
        {
            KeyDisplayType::Global
        } else if discriminant
            == std::mem::discriminant(&IdConfigEditor::KeyOther(IdKeyOther::DatabaseAddAll))
        {
            KeyDisplayType::Other
        } else {
            unimplemented!("Invalid Discriminant: {:#?}", discriminant)
        };

        Self {
            elems,
            discriminant,
            width,
        }
    }

    /// Actually draw the elements in the current instance.
    pub fn view(&self, model: &mut Application<Id, Msg, UserEvent>, area: Rect, f: &mut Frame<'_>) {
        /// Gets the state of `Id::ConfigEditor(id)` and if it has a `State::One`, returns `yes`, otherwise `no`.
        ///
        /// Macro to apply "DRY"(Dont-Repeat-Yourself) to reduce function length.
        macro_rules! is_expanded {
            ($id:expr, $yes:expr, $no:expr) => {
                match model.state(&Id::ConfigEditor($id)) {
                    Ok(State::One(_)) => $no,
                    _ => $yes,
                }
            };
        }

        // determine what heights each element should have
        let mut elems_heights = Vec::with_capacity(self.elems.len());

        for id in self.elems {
            elems_heights.push(is_expanded!(IdConfigEditor::from(id), 8, 3));
        }

        // find the focused element, if any
        let focus_elem = model
            .focus()
            .and_then(|v| match self.discriminant {
                KeyDisplayType::Global => {
                    if let Id::ConfigEditor(IdConfigEditor::KeyGlobal(key)) = *v {
                        Some(IdKey::Global(key))
                    } else {
                        None
                    }
                }
                KeyDisplayType::Other => {
                    if let Id::ConfigEditor(IdConfigEditor::KeyOther(key)) = *v {
                        Some(IdKey::Other(key))
                    } else {
                        None
                    }
                }
            })
            .and_then(|focus| {
                self.elems
                    .iter()
                    .enumerate()
                    .find(|(_, v)| **v == focus)
                    .map(|(idx, _)| idx)
            });

        let cells = DynamicHeightGrid::new(elems_heights, self.width)
            .draw_row_low_space()
            .distribute_row_space()
            .focus_node(focus_elem)
            .split(area);

        // actually draw each element
        for (id, cell) in self.elems.iter().zip(cells.iter()) {
            model.view(&Id::ConfigEditor(id.into()), f, *cell);
        }
    }
}