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
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::time::Duration;

use anyhow::{Context as _, Result, anyhow};
use rand::seq::IndexedRandom;
use termusiclib::common::const_unknown::{UNKNOWN_ALBUM, UNKNOWN_ARTIST};
use termusiclib::config::SharedTuiSettings;
use termusiclib::config::v2::server::{LoopMode, ScanDepth};
use termusiclib::new_database::track_ops::TrackRead;
use termusiclib::new_database::{album_ops, track_ops};
use termusiclib::player::playlist_helpers::{
    PlaylistAddTrack, PlaylistPlaySpecific, PlaylistRemoveTrackIndexed, PlaylistSwapTrack,
    PlaylistTrackSource,
};
use termusiclib::player::{
    PlaylistAddTrackInfo, PlaylistLoopModeInfo, PlaylistRemoveTrackInfo, PlaylistShuffledInfo,
    PlaylistSwapInfo,
};
use termusiclib::track::Track;
use termusiclib::track::{DurationFmtShort, PodcastTrackData};
use termusiclib::utils::{filetype_supported, is_playlist, playlist_get_vec};
use tui_realm_stdlib::Table;
use tuirealm::props::{Alignment, BorderType, PropPayload, PropValue, TableBuilder, TextSpan};
use tuirealm::props::{Borders, Style};
use tuirealm::{
    AttrValue, Attribute, Component, Event, MockComponent, State, StateValue,
    event::{Key, KeyEvent},
};
use tuirealm::{
    command::{Cmd, CmdResult, Direction, Position},
    event::KeyModifiers,
};

use crate::ui::Model;
use crate::ui::components::orx_music_library::scanner::library_dir_tree;
use crate::ui::ids::Id;
use crate::ui::model::{TermusicLayout, UserEvent};
use crate::ui::msg::{GSMsg, Msg, PLMsg, SearchCriteria};
use crate::ui::tui_cmd::{PlaylistCmd, TuiCmd};

#[derive(MockComponent)]
pub struct Playlist {
    component: Table,
    config: SharedTuiSettings,
}

impl Playlist {
    pub fn new(config: SharedTuiSettings) -> Self {
        let component = {
            let config = config.read();
            Table::default()
                .borders(
                    Borders::default()
                        .modifiers(BorderType::Rounded)
                        .color(config.settings.theme.playlist_border()),
                )
                .background(config.settings.theme.playlist_background())
                .foreground(config.settings.theme.playlist_foreground())
                .inactive(Style::new().bg(config.settings.theme.playlist_background()))
                .title(" Playlist ", Alignment::Left)
                .scroll(true)
                .highlighted_color(config.settings.theme.playlist_highlight())
                .highlighted_str(&config.settings.theme.style.playlist.highlight_symbol)
                .rewind(false)
                .step(4)
                .row_height(1)
                .headers(["Duration", "Artist", "Title", "Album"])
                .column_spacing(2)
                .widths(&[12, 20, 25, 43])
                .table(
                    TableBuilder::default()
                        .add_col(TextSpan::from("Empty"))
                        .add_col(TextSpan::from("Empty Queue"))
                        .add_col(TextSpan::from("Empty"))
                        .build(),
                )
        };

        Self { component, config }
    }
}

impl Component<Msg, UserEvent> for Playlist {
    #[allow(clippy::too_many_lines)]
    fn on(&mut self, ev: Event<UserEvent>) -> Option<Msg> {
        let config = self.config.clone();
        let keys = &config.read().settings.keys;
        let cmd_result = match ev {
            Event::Keyboard(KeyEvent {
                code: Key::Down,
                modifiers: KeyModifiers::NONE,
            }) => self.perform(Cmd::Move(Direction::Down)),
            Event::Keyboard(KeyEvent {
                code: Key::Up,
                modifiers: KeyModifiers::NONE,
            }) => self.perform(Cmd::Move(Direction::Up)),
            Event::Keyboard(key) if key == keys.navigation_keys.down.get() => {
                self.perform(Cmd::Move(Direction::Down))
            }
            Event::Keyboard(key) if key == keys.navigation_keys.up.get() => {
                self.perform(Cmd::Move(Direction::Up))
            }
            Event::Keyboard(KeyEvent {
                code: Key::PageDown,
                modifiers: KeyModifiers::NONE,
            }) => self.perform(Cmd::Scroll(Direction::Down)),
            Event::Keyboard(KeyEvent {
                code: Key::PageUp,
                modifiers: KeyModifiers::NONE,
            }) => self.perform(Cmd::Scroll(Direction::Up)),
            Event::Keyboard(key) if key == keys.navigation_keys.goto_top.get() => {
                self.perform(Cmd::GoTo(Position::Begin))
            }
            Event::Keyboard(key) if key == keys.navigation_keys.goto_bottom.get() => {
                self.perform(Cmd::GoTo(Position::End))
            }
            Event::Keyboard(KeyEvent {
                code: Key::Home,
                modifiers: KeyModifiers::NONE,
            }) => self.perform(Cmd::GoTo(Position::Begin)),
            Event::Keyboard(KeyEvent {
                code: Key::End,
                modifiers: KeyModifiers::NONE,
            }) => self.perform(Cmd::GoTo(Position::End)),
            Event::Keyboard(KeyEvent {
                code: Key::Tab,
                modifiers: KeyModifiers::NONE,
            }) => return Some(Msg::Playlist(PLMsg::PlaylistTableBlurDown)),
            Event::Keyboard(KeyEvent {
                code: Key::BackTab,
                modifiers: KeyModifiers::SHIFT,
            }) => return Some(Msg::Playlist(PLMsg::PlaylistTableBlurUp)),
            Event::Keyboard(key) if key == keys.playlist_keys.delete.get() => {
                match self.component.state() {
                    State::One(StateValue::Usize(index_selected)) => {
                        return Some(Msg::Playlist(PLMsg::Delete(index_selected)));
                    }
                    _ => CmdResult::None,
                }
            }
            Event::Keyboard(key) if key == keys.playlist_keys.delete_all.get() => {
                return Some(Msg::Playlist(PLMsg::DeleteAll));
            }
            Event::Keyboard(key) if key == keys.playlist_keys.shuffle.get() => {
                return Some(Msg::Playlist(PLMsg::Shuffle));
            }
            Event::Keyboard(key) if key == keys.playlist_keys.cycle_loop_mode.get() => {
                return Some(Msg::Playlist(PLMsg::LoopModeCycle));
            }
            Event::Keyboard(key) if key == keys.playlist_keys.play_selected.get() => {
                if let State::One(StateValue::Usize(index)) = self.state() {
                    return Some(Msg::Playlist(PLMsg::PlaySelected(index)));
                }
                CmdResult::None
            }

            Event::Keyboard(KeyEvent {
                code: Key::Enter,
                modifiers: KeyModifiers::NONE,
            }) => {
                if let State::One(StateValue::Usize(index)) = self.state() {
                    return Some(Msg::Playlist(PLMsg::PlaySelected(index)));
                }
                CmdResult::None
            }
            Event::Keyboard(key) if key == keys.playlist_keys.search.get() => {
                return Some(Msg::GeneralSearch(GSMsg::PopupShowPlaylist));
            }
            Event::Keyboard(key) if key == keys.playlist_keys.swap_down.get() => {
                match self.component.state() {
                    State::One(StateValue::Usize(index_selected)) => {
                        self.perform(Cmd::Move(Direction::Down));
                        return Some(Msg::Playlist(PLMsg::SwapDown(index_selected)));
                    }
                    _ => CmdResult::None,
                }
            }
            Event::Keyboard(key) if key == keys.playlist_keys.swap_up.get() => {
                match self.component.state() {
                    State::One(StateValue::Usize(index_selected)) => {
                        self.perform(Cmd::Move(Direction::Up));
                        return Some(Msg::Playlist(PLMsg::SwapUp(index_selected)));
                    }
                    _ => CmdResult::None,
                }
            }
            Event::Keyboard(key) if key == keys.playlist_keys.add_random_album.get() => {
                return Some(Msg::Playlist(PLMsg::AddRandomAlbum));
            }
            Event::Keyboard(key) if key == keys.playlist_keys.add_random_songs.get() => {
                return Some(Msg::Playlist(PLMsg::AddRandomTracks));
            }
            _ => CmdResult::None,
        };
        match cmd_result {
            CmdResult::None => None,
            _ => Some(Msg::ForceRedraw),
        }
    }
}

impl Model {
    pub fn playlist_reload(&mut self) {
        assert!(
            self.app
                .remount(
                    Id::Playlist,
                    Box::new(Playlist::new(self.config_tui.clone())),
                    Vec::new()
                )
                .is_ok()
        );
        self.playlist_switch_layout();
        self.playlist_sync();
    }

    pub fn playlist_switch_layout(&mut self) {
        if self.layout == TermusicLayout::Podcast {
            let headers = &["Duration", "Episodes"];
            self.app
                .attr(
                    &Id::Playlist,
                    Attribute::Text,
                    AttrValue::Payload(PropPayload::Vec(
                        headers
                            .iter()
                            .map(|x| PropValue::Str((*x).to_string()))
                            // .map(|x| PropValue::Str(x.as_ref().to_string()))
                            .collect(),
                    )),
                )
                .ok();

            let widths = &[12, 88];
            self.app
                .attr(
                    &Id::Playlist,
                    Attribute::Width,
                    AttrValue::Payload(PropPayload::Vec(
                        widths.iter().map(|x| PropValue::U16(*x)).collect(),
                    )),
                )
                .ok();
            self.playlist_sync();
            return;
        }

        let headers = &["Duration", "Artist", "Title", "Album"];
        self.app
            .attr(
                &Id::Playlist,
                Attribute::Text,
                AttrValue::Payload(PropPayload::Vec(
                    headers
                        .iter()
                        .map(|x| PropValue::Str((*x).to_string()))
                        // .map(|x| PropValue::Str(x.as_ref().to_string()))
                        .collect(),
                )),
            )
            .ok();

        let widths = &[12, 20, 25, 43];
        self.app
            .attr(
                &Id::Playlist,
                Attribute::Width,
                AttrValue::Payload(PropPayload::Vec(
                    widths.iter().map(|x| PropValue::U16(*x)).collect(),
                )),
            )
            .ok();
        self.playlist_sync();
    }

    /// Add a playlist (like m3u) to the playlist.
    fn playlist_add_playlist(&mut self, playlist_path: &Path) -> Result<()> {
        let vec = playlist_get_vec(playlist_path)?;

        let sources = vec
            .into_iter()
            .map(|v| {
                if v.starts_with("http") {
                    PlaylistTrackSource::Url(v)
                } else {
                    PlaylistTrackSource::Path(v)
                }
            })
            .collect();

        self.command(TuiCmd::Playlist(PlaylistCmd::AddTrack(
            PlaylistAddTrack::new_vec(
                u64::try_from(self.playback.playlist.len()).unwrap(),
                sources,
            ),
        )));

        Ok(())
    }

    /// Add a podcast episode to the playlist.
    pub fn playlist_add_episode(&mut self, episode_index: usize) -> Result<()> {
        if self.podcast.podcasts.is_empty() {
            return Ok(());
        }
        let podcast_selected = self
            .podcast
            .podcasts
            .get(self.podcast.podcasts_index)
            .ok_or_else(|| anyhow!("get podcast selected failed."))?;
        let episode_selected = podcast_selected
            .episodes
            .get(episode_index)
            .ok_or_else(|| anyhow!("get episode selected failed."))?;

        let source = PlaylistTrackSource::PodcastUrl(episode_selected.url.clone());
        self.command(TuiCmd::Playlist(PlaylistCmd::AddTrack(
            PlaylistAddTrack::new_single(
                u64::try_from(self.playback.playlist.len()).unwrap(),
                source,
            ),
        )));

        Ok(())
    }

    fn playlist_get_dir_entries(path: &Path) -> Vec<String> {
        // use the same function as the tree order gets generated in, so that we add in a expected order
        let vec = library_dir_tree(path, ScanDepth::Limited(1));
        vec.children
            .into_iter()
            .map(|v| v.path.to_string_lossy().to_string())
            .collect()
    }

    /// Add the `current_node`, regardless if it is a Track, dir, playlist, etc.
    ///
    /// See [`Model::playlist_add_episode`] for podcast episode adding
    pub fn playlist_add(&mut self, path: &Path) -> Result<()> {
        if !path.exists() {
            return Ok(());
        }
        if path.is_dir() {
            let new_items_vec = Self::playlist_get_dir_entries(path);

            let sources = new_items_vec
                .into_iter()
                .map(PlaylistTrackSource::Path)
                .collect();

            self.command(TuiCmd::Playlist(PlaylistCmd::AddTrack(
                PlaylistAddTrack::new_vec(
                    u64::try_from(self.playback.playlist.len()).unwrap(),
                    sources,
                ),
            )));

            return Ok(());
        }
        self.playlist_add_item(path)?;
        self.playlist_sync();
        Ok(())
    }

    /// Add a Track or a Playlist to the playlist
    fn playlist_add_item(&mut self, path: &Path) -> Result<()> {
        if is_playlist(path) {
            self.playlist_add_playlist(path)?;
            return Ok(());
        }
        let source = if path.starts_with("http") {
            PlaylistTrackSource::Url(path.to_string_lossy().to_string())
        } else {
            PlaylistTrackSource::Path(path.to_string_lossy().to_string())
        };

        self.command(TuiCmd::Playlist(PlaylistCmd::AddTrack(
            PlaylistAddTrack::new_single(
                u64::try_from(self.playback.playlist.len()).unwrap(),
                source,
            ),
        )));

        Ok(())
    }

    /// Add [`TrackDB`] to the playlist
    pub fn playlist_add_all_from_db(&mut self, vec: &[TrackRead]) {
        let sources = vec
            .iter()
            .map(|f| PlaylistTrackSource::Path(f.as_pathbuf().to_string_lossy().to_string()))
            .collect();

        self.command(TuiCmd::Playlist(PlaylistCmd::AddTrack(
            PlaylistAddTrack::new_vec(
                u64::try_from(self.playback.playlist.len()).unwrap(),
                sources,
            ),
        )));
    }

    /// Add random album(s) from the database to the playlist
    pub fn playlist_add_random_album(&mut self) {
        let playlist_select_random_album_quantity = self
            .config_server
            .read()
            .settings
            .player
            .random_album_min_quantity
            .get();
        let vec = self.playlist_get_random_album_tracks(playlist_select_random_album_quantity);
        self.playlist_add_all_from_db(&vec);
    }

    /// Add random tracks from the database to the playlist
    pub fn playlist_add_random_tracks(&mut self) {
        let playlist_select_random_track_quantity = self
            .config_server
            .read()
            .settings
            .player
            .random_track_quantity
            .get();
        let vec = self.playlist_get_random_tracks(playlist_select_random_track_quantity);
        self.playlist_add_all_from_db(&vec);
    }

    /// Handle when a playlist has added a track
    pub fn handle_playlist_add(&mut self, items: PlaylistAddTrackInfo) -> Result<()> {
        // piggyback off-of the server side implementation for now by re-parsing everything.
        self.playback.playlist.add_tracks(
            PlaylistAddTrack {
                at_index: items.at_index,
                tracks: vec![items.trackid],
            },
            &self.podcast.db_podcast,
        )?;

        self.playlist_sync();

        Ok(())
    }

    /// Handle when a playlist has removed a track
    pub fn handle_playlist_remove(&mut self, items: &PlaylistRemoveTrackInfo) -> Result<()> {
        self.playback.playlist.handle_grpc_remove(items)?;

        self.playlist_sync();

        Ok(())
    }

    /// Handle when a playlist was cleared
    pub fn handle_playlist_clear(&mut self) {
        self.playback.playlist.clear();

        self.playlist_sync();
    }

    /// Handle when the playlist loop-mode was changed
    pub fn handle_playlist_loopmode(&mut self, loop_mode: &PlaylistLoopModeInfo) -> Result<()> {
        let as_u8 = u8::try_from(loop_mode.mode).context("Failed to convert u32 to u8")?;
        let loop_mode =
            LoopMode::tryfrom_discriminant(as_u8).context("Failed to get LoopMode from u8")?;
        self.playback.playlist.set_loop_mode(loop_mode);
        self.config_server.write().settings.player.loop_mode = loop_mode;
        self.playlist_update_title();
        // Force a redraw as stream updates are not part of the "tick" event and so cant send "Msg"
        // but need a redraw because ofthe title change
        self.force_redraw();

        Ok(())
    }

    /// Handle when the playlist had swapped some tracks
    pub fn handle_playlist_swap_tracks(&mut self, swapped_tracks: &PlaylistSwapInfo) -> Result<()> {
        let index_a = usize::try_from(swapped_tracks.index_a)
            .context("Failed to convert index_a to usize")?;
        let index_b = usize::try_from(swapped_tracks.index_b)
            .context("Failed to convert index_b to usize")?;

        self.playback.playlist.swap(index_a, index_b)?;

        self.playlist_sync();

        Ok(())
    }

    /// Handle when the playlist has been shuffled and so has new order of tracks
    pub fn handle_playlist_shuffled(&mut self, shuffled: PlaylistShuffledInfo) -> Result<()> {
        let playlist_comp_selected_index = self.playlist_get_selected_index();
        // this might be fragile if there are multiple of the same track in the playlist as there is no unique identifier currently
        let playlist_track_at_old_file = playlist_comp_selected_index
            .and_then(|idx| self.playback.playlist.tracks().get(idx))
            .map(Track::as_track_source);

        self.playback
            .load_from_grpc(shuffled.tracks, &self.podcast.db_podcast)?;
        self.playlist_sync();

        if let Some(old_id) = playlist_track_at_old_file {
            let found_new_index = self
                .playback
                .playlist
                .tracks()
                .iter()
                .enumerate()
                .find(|(_, track)| *track == old_id);
            if let Some((new_index, _)) = found_new_index {
                self.playlist_locate(new_index);
            }
        }

        Ok(())
    }

    /// Handle setting the current track index in the TUI playlist and selecting the proper list item
    ///
    /// Note: currently this function is called twice per track change, once for `UpdateEvents::TrackChanged` and once for `run_playback::GetProgress`
    pub fn handle_current_track_index(&mut self, current_track_index: usize, force_relocate: bool) {
        let tui_old_current_index = self.playback.playlist.current_track_index();
        info!(
            "index from player is: {current_track_index:?}, index in tui is: {tui_old_current_index:?}"
        );
        self.playback.clear_current_track();
        let _ = self
            .playback
            .playlist
            .set_current_track_index(current_track_index);
        self.playback.set_current_track_from_playlist();

        let playlist_comp_selected_index = self.playlist_get_selected_index();

        // only re-select the current-track if the old selection was the old-current-track
        if force_relocate
            || (tui_old_current_index.is_some()
                && playlist_comp_selected_index.is_none_or(|v| v == tui_old_current_index.unwrap()))
        {
            self.playlist_locate(current_track_index);
        }

        self.update_layout_for_current_track();
        self.playback.set_current_track_pos(Duration::ZERO);
        self.player_update_current_track_after();

        self.lyric_update_for_podcast_by_current_track();

        if let Err(e) = self.podcast_mark_current_track_played() {
            self.mount_error_popup(e.context("Marking podcast track as played"));
        }
    }

    fn playlist_sync_podcasts(&mut self) {
        let mut table: TableBuilder = TableBuilder::default();

        for (idx, track) in self.playback.playlist.tracks().iter().enumerate() {
            if idx > 0 {
                table.add_row();
            }

            let duration_str = if let Some(dur) = track.duration_str_short() {
                format!("[{dur:^7.7}]")
            } else {
                "[--:--]".to_string()
            };

            let mut title = track.title().unwrap_or("Unknown Title").to_string();
            if track
                .as_podcast()
                .is_some_and(PodcastTrackData::has_localfile)
            {
                title = format!("[D] {title}");
            }
            if Some(idx) == self.playback.playlist.current_track_index() {
                title = format!(
                    "{}{title}",
                    self.config_tui
                        .read()
                        .settings
                        .theme
                        .style
                        .playlist
                        .current_track_symbol
                );
            }
            table
                .add_col(TextSpan::new(duration_str.as_str()))
                .add_col(TextSpan::new(title).bold());
        }
        if self.playback.playlist.is_empty() {
            table.add_col(TextSpan::from("0"));
            table.add_col(TextSpan::from("empty playlist"));
        }

        let table = table.build();
        self.app
            .attr(
                &Id::Playlist,
                tuirealm::Attribute::Content,
                tuirealm::AttrValue::Table(table),
            )
            .ok();

        self.playlist_update_title();
    }

    pub fn playlist_sync(&mut self) {
        if self.layout == TermusicLayout::Podcast {
            self.playlist_sync_podcasts();
            return;
        }

        let mut table: TableBuilder = TableBuilder::default();
        let artist_color = self
            .config_tui
            .read_recursive()
            .settings
            .theme
            .library_highlight();

        for (idx, track) in self.playback.playlist.tracks().iter().enumerate() {
            if idx > 0 {
                table.add_row();
            }

            let duration_str = if let Some(dur) = track.duration_str_short() {
                format!("[{dur:^7.7}]")
            } else {
                "[--:--]".to_string()
            };

            let mut title: Cow<'_, str> = track.title().map_or_else(|| track.id_str(), Into::into);

            let artist = track.artist().unwrap_or(UNKNOWN_ARTIST);
            let album = track
                .as_track()
                .and_then(|v| v.album())
                .unwrap_or(UNKNOWN_ALBUM);

            // TODO: is there maybe a better option to do this on-demand instead of the whole playlist; like on draw-time?
            if Some(idx) == self.playback.playlist.current_track_index() {
                title = format!(
                    "{}{title}",
                    self.config_tui
                        .read()
                        .settings
                        .theme
                        .style
                        .playlist
                        .current_track_symbol
                )
                .into();
            }

            table
                .add_col(TextSpan::new(duration_str.as_str()))
                .add_col(TextSpan::new(artist).fg(artist_color))
                .add_col(TextSpan::new(title).bold())
                .add_col(TextSpan::new(album));
        }
        if self.playback.playlist.is_empty() {
            table.add_col(TextSpan::from("0"));
            table.add_col(TextSpan::from("empty playlist"));
            table.add_col(TextSpan::from(""));
            table.add_col(TextSpan::from(""));
        }

        let table = table.build();
        self.app
            .attr(
                &Id::Playlist,
                tuirealm::Attribute::Content,
                tuirealm::AttrValue::Table(table),
            )
            .ok();

        self.playlist_update_title();
    }

    /// Delete a track at `index` from the playlist
    pub fn playlist_delete_item(&mut self, index: usize) {
        if self.playback.playlist.is_empty() || index >= self.playback.playlist.len() {
            return;
        }

        let Some(track) = self.playback.playlist.tracks().get(index) else {
            return;
        };

        let track_source = track.as_track_source();

        self.command(TuiCmd::Playlist(PlaylistCmd::RemoveTrack(
            PlaylistRemoveTrackIndexed::new_single(u64::try_from(index).unwrap(), track_source),
        )));
    }

    /// Clear a entire playlist
    pub fn playlist_clear(&mut self) {
        if self.playback.playlist.is_empty() {
            return;
        }

        self.command(TuiCmd::Playlist(PlaylistCmd::Clear));
    }

    /// Shuffle the whole playlist
    pub fn playlist_shuffle(&mut self) {
        self.command(TuiCmd::Playlist(PlaylistCmd::Shuffle));
    }

    /// Send command to swap 2 indexes. Does nothing if either index is out-of-bounds.
    ///
    /// # Panics
    ///
    /// if `usize` cannot be converted to `u64`
    fn playlist_swap(&mut self, index_a: usize, index_b: usize) {
        let len = self.playback.playlist.tracks().len();
        if index_a.max(index_b) >= len {
            error!(
                "Index out-of-bounds, not executing swap: {}",
                index_a.max(index_b)
            );
            return;
        }

        self.command(TuiCmd::Playlist(PlaylistCmd::SwapTrack(
            PlaylistSwapTrack {
                index_a: u64::try_from(index_a).unwrap(),
                index_b: u64::try_from(index_b).unwrap(),
            },
        )));
    }

    /// Swap the given index upwards, does nothing if out-of-bounds or would result in itself.
    pub fn playlist_swap_up(&mut self, index: usize) {
        if index == 0 {
            return;
        }

        // always guranteed to be above 0, no saturated necessary
        self.playlist_swap(index, index - 1);
    }

    /// Swap the given index downwards, does nothing if out-of-bounds.
    pub fn playlist_swap_down(&mut self, index: usize) {
        if index >= self.playback.playlist.len().saturating_sub(1) {
            return;
        }

        self.playlist_swap(index, index.saturating_add(1));
    }

    pub fn playlist_update_library_delete(&mut self) {
        self.command(TuiCmd::Playlist(PlaylistCmd::RemoveDeletedItems));
    }

    pub fn playlist_update_title(&mut self) {
        let duration = self
            .playback
            .playlist
            .tracks()
            .iter()
            .filter_map(Track::duration)
            .sum();
        let display_symbol = self
            .config_tui
            .read()
            .settings
            .theme
            .style
            .playlist
            .use_loop_mode_symbol;
        let loop_mode = self.config_server.read().settings.player.loop_mode;
        let title = format!(
            "\u{2500} Playlist \u{2500}\u{2500}\u{2524} Total {} tracks | {} | Mode: {} \u{251c}\u{2500}",
            self.playback.playlist.len(),
            DurationFmtShort(duration),
            loop_mode.display(display_symbol),
        );
        self.app
            .attr(
                &Id::Playlist,
                tuirealm::Attribute::Title,
                tuirealm::AttrValue::Title((title, Alignment::Left)),
            )
            .ok();
    }

    /// Play the currently selected item in the playlist list
    pub fn playlist_play_selected(&mut self, index: usize) {
        let Some(track) = self.playback.playlist.tracks().get(index) else {
            error!("Track {index} not in playlist!");
            return;
        };

        let track_source = track.as_track_source();

        self.command(TuiCmd::Playlist(PlaylistCmd::PlaySpecific(
            PlaylistPlaySpecific {
                track_index: u64::try_from(index).unwrap(),
                id: track_source,
            },
        )));
    }

    pub fn playlist_update_search(&mut self, input: &str) {
        let filtered_music = Model::update_search(self.playback.playlist.tracks(), input);
        self.general_search_update_show(Model::build_table(filtered_music, &self.config_tui));
    }

    /// Select the given index in the playlist list component
    pub fn playlist_locate(&mut self, index: usize) {
        assert!(
            self.app
                .attr(
                    &Id::Playlist,
                    Attribute::Value,
                    AttrValue::Payload(PropPayload::One(PropValue::Usize(index))),
                )
                .is_ok()
        );
    }

    /// Get the current selected index in the playlist list component
    pub fn playlist_get_selected_index(&self) -> Option<usize> {
        // the index on a "Table" can be set via "AttrValue::Payload(PropPayload::One(PropValue::Usize(val)))", but reading that is stale
        // as that value is only read in the "Table", not removed or updated, but "state" is
        let Ok(State::One(StateValue::Usize(val))) = self.app.state(&Id::Playlist) else {
            return None;
        };

        Some(val)
    }

    pub fn playlist_get_random_tracks(&mut self, quantity: u32) -> Vec<TrackRead> {
        let mut result = Vec::with_capacity(usize::try_from(quantity).unwrap_or_default());
        let all_tracks =
            track_ops::get_all_tracks(&self.db.get_connection(), track_ops::RowOrdering::IdAsc);
        if let Ok(vec) = all_tracks {
            let mut i = 0;
            loop {
                if let Some(record) = vec.choose(&mut rand::rng()) {
                    let path = record.as_pathbuf();
                    if filetype_supported(&path) {
                        result.push(record.clone());
                        i += 1;
                        if i > quantity - 1 {
                            break;
                        }
                    }
                }
            }
        }
        result
    }

    pub fn playlist_get_random_album_tracks(&mut self, quantity: u32) -> Vec<TrackRead> {
        let mut result = Vec::with_capacity(usize::try_from(quantity).unwrap_or_default());
        let all_albums =
            album_ops::get_all_albums(&self.db.get_connection(), album_ops::RowOrdering::IdAsc);
        if let Ok(vec) = all_albums {
            loop {
                if let Some(v) = vec.choose(&mut rand::rng()) {
                    let all_tracks_in_album = track_ops::get_tracks_from_album(
                        &self.db.get_connection(),
                        &v.title,
                        &v.artist_display,
                        track_ops::RowOrdering::IdAsc,
                    );
                    if let Ok(vec2) = all_tracks_in_album {
                        if vec2.len() < quantity as usize {
                            continue;
                        }

                        result.extend(vec2);

                        break;
                    }
                }
            }
        }
        result
    }

    /// Save the current playlist as m3u to the given path
    pub fn playlist_save_m3u_before(&mut self, path: PathBuf) -> Result<()> {
        if path.exists() {
            self.mount_save_playlist_confirm(path)
                .expect("Expected SavePlaylistConfirm to mount correctly");
            return Ok(());
        }

        self.playlist_save_m3u(path)
    }

    /// Save the current playlist as m3u in the given full path.
    pub fn playlist_save_m3u(&mut self, path: PathBuf) -> Result<()> {
        // TODO: move this to server?
        self.playback.playlist.save_m3u(&path)?;

        self.new_library_reload_and_focus(path);

        // only reload database results, if the criteria is for playlists
        if self.dw.criteria == SearchCriteria::Playlist {
            self.database_update_search_results();
        }

        Ok(())
    }
}