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
use errors::{MelodyErrors, MelodyErrorsKind};
use rand::{thread_rng, Rng};
use rodio;
use song::{Playlist, Song};
use std::fmt;
use std::fs::File;
use std::io::{BufReader, Write};
use tabwriter::TabWriter;
use utils::{fmt_duration, supported_song};

#[derive(Clone)]
pub enum MusicPlayerStatus {
    Stopped(Option<Song>),
    NowPlaying(Song),
    Paused(Song),
}

impl fmt::Display for MusicPlayerStatus {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use MusicPlayerStatus::*;
        match self.clone() {
            Paused(song) => write!(f, "[Paused] : {}", song),
            NowPlaying(song) => write!(f, "[Now Playing] : {}", song),
            Stopped(s) => match s {
                Some(song) => write!(f, "[Stopped] : Last Played - {}", song),
                None => write!(f, "[Stopped]"),
            },
        }
    }
}

pub struct MusicPlayer {
    playlist: Vec<Song>,
    sink: rodio::Sink,
    current: Option<Song>,
    previous: Option<Song>,
    playing_time: (::std::time::Instant, ::std::time::Duration),
}

impl MusicPlayer {
    pub fn new(playlist: Playlist) -> Self {
        // TODO: USe a non-depricated method
        #[allow(deprecated)]
        let endpoint =
            rodio::default_output_device().expect("Failed to find default music endpoint");

        MusicPlayer {
            // Remove all unsuported songs
            playlist: c![song, for song in playlist.tracks, if supported_song(song.file())],
            sink: rodio::Sink::new(&endpoint),
            current: None,
            previous: None,
            playing_time: (
                ::std::time::Instant::now(),
                ::std::time::Duration::from_secs(0),
            ),
        }
    }

    pub fn shuffle(&mut self) {
        thread_rng().shuffle(&mut self.playlist);
    }

    pub fn start(&mut self) -> Result<(), MelodyErrors> {
        if self.playlist.is_empty() {
            Err(MelodyErrors::new(
                MelodyErrorsKind::EmptyQueue,
                "Playlist is empty",
                None,
            ))
        } else {
            if self.sink.empty() {
                self.playing_time.0 = ::std::time::Instant::now();
                let current = self.playlist.remove(0);
                let file =
                    File::open(&current).expect(&format!("Failed to read {:?}", current.file));
                let source = rodio::Decoder::new(BufReader::new(file))
                    .expect(&format!("Failed to decode {:?}", current.file));
                self.sink.append(source);

                self.current = Some(current);
            };
            Ok(())
        }
    }
    pub fn resume(&mut self) {
        self.sink.play();
        self.playing_time.0 = ::std::time::Instant::now();
    }
    pub fn pause(&mut self) {
        self.sink.pause();
        self.playing_time.1 = self.playing_time.0.elapsed();
    }
    pub fn stop(&mut self) {
        self.sink.stop();
        self.previous = self.current.clone().and_then(|mut s| {
            s.elapsed = self.playing_time.0.elapsed() + self.playing_time.1;
            Some(s)
        });
        self.current = None;
    }
    pub fn play_next(&mut self) {
        self.start().unwrap_or(());
    }
    pub fn volume(&self) -> f32 {
        self.sink.volume()
    }
    pub fn set_volume(&mut self, volume: f32) {
        self.sink.set_volume(volume)
    }
    pub fn lock(&self) {
        self.sink.sleep_until_end();
    }
    pub fn queue(&self) -> &Vec<Song> {
        &self.playlist
    }
    pub fn status(&self) -> MusicPlayerStatus {
        if self.sink.empty() {
            MusicPlayerStatus::Stopped(self.previous.clone())
        } else if let Some(mut song) = self.current.clone() {
            if self.sink.is_paused() {
                song.elapsed = self.playing_time.1;
                MusicPlayerStatus::Paused(song)
            } else {
                song.elapsed = self.playing_time.0.elapsed() + self.playing_time.1;
                MusicPlayerStatus::NowPlaying(song)
            }
        } else {
            MusicPlayerStatus::Stopped(self.previous.clone())
        }
    }
}

impl fmt::Display for MusicPlayer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let now_playing = match self.current {
            Some(ref song) => {
                let status: &str = if self.sink.is_paused() {
                    "[paused] "
                } else {
                    ""
                };
                format!("{}{}\n", status, song)
            }
            None => String::new(),
        };
        let mut tw = TabWriter::new(Vec::new());
        let mut lines: Vec<String> = vec![String::from(
            "|\tArtist\t|\tAlbum\t|\tTitle\t|\tDuration\t|",
        )];
        for track in &self.playlist {
            let duration = fmt_duration(&track.duration);
            lines.push(format!(
                "|\t{}\t|\t{}\t|\t{}\t|\t{}\t|",
                track.artist().unwrap_or("Unkown Artist"),
                track.album().unwrap_or("Unkown Album"),
                track.title().unwrap_or("Unkown Title"),
                duration
            ))
        }
        write!(tw, "{}", lines.join("\n")).unwrap();
        tw.flush().unwrap();
        write!(
            f,
            "{}{}",
            now_playing,
            String::from_utf8(tw.into_inner().unwrap()).unwrap()
        )
    }
}