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
use crate::timer::{Timer, TimerEvent, TimerType};
use flume;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
use tokio::task;

#[derive(Clone)]
pub struct Config {
    pub work_duration: Duration,
    pub break_duration: Duration,
    pub long_break_duration: Duration,
    pub long_break_interval: u64,
    pub auto: bool,
}

pub struct Controller {
    timer: Arc<Mutex<Timer>>,
    tx: flume::Sender<String>,
    rx: flume::Receiver<String>,
    config: Config,
    event_handlers: HashMap<TimerEvent, Vec<Arc<dyn Fn(&Timer) + Send + Sync>>>,
    num_finished_timers: u64,
}

impl Controller {
    pub fn new(config: Config) -> Arc<Mutex<Controller>> {
        let (tx, rx) = flume::unbounded();

        let Config { work_duration, .. } = config;

        let timer = Controller::create_timer(tx.clone(), TimerType::Work, work_duration);

        Arc::new(Mutex::new(Controller {
            config,
            timer,
            tx,
            rx,
            event_handlers: HashMap::new(),
            num_finished_timers: 0,
        }))
    }

    fn create_timer(
        tx: flume::Sender<String>,
        timer_type: TimerType,
        duration: Duration,
    ) -> Arc<Mutex<Timer>> {
        // create a new timer
        let timer = Timer::new(timer_type, &duration.clone());

        let mut timer_guard = timer.lock().expect("Failed to lock timer");

        // add Finish event handler (only used for auto mode to start next timer)
        timer_guard.on(
            TimerEvent::Finish,
            Arc::new(move |_: &Timer| {
                tx.send("timer_finished".to_string())
                    .expect("Failed to send timer finished message");
            }),
        );

        drop(timer_guard);

        timer
    }

    fn attach_timer_handlers(&self) {
        let mut timer = self.timer.lock().expect("Failed to lock timer");

        // attach saved event handlers to timer
        for (event, handlers) in &self.event_handlers {
            for handler in handlers {
                timer.on(*event, handler.clone());
            }
        }
    }

    pub fn start(controller: &Arc<Mutex<Self>>) {
        let controller = Arc::clone(controller);

        let mut controller_guard_1 = controller.lock().expect("Failed to lock controller");
        let rx = controller_guard_1.rx.clone();

        controller_guard_1.start_current_timer();
        drop(controller_guard_1);

        task::spawn(async move {
            loop {
                let msg = rx
                    .recv_async()
                    .await
                    .expect("Failed to listen to socket messages");

                let mut controller_guard_2 = controller.lock().expect("Failed to lock controller");

                match msg.as_str() {
                    "timer_finished" => {
                        controller_guard_2.on_timer_finished();
                    }
                    "skip" => {
                        controller_guard_2.start_next_timer();
                    }
                    _ => {}
                }

                drop(controller_guard_2);
            }
        });
    }

    // TODO: These methods look like they can be refactored into a single method
    pub fn next(controller: &Arc<Mutex<Self>>) {
        let mut controller = controller.lock().expect("Failed to lock controller");
        controller.start_next_timer();
    }

    pub fn stop(controller: &Arc<Mutex<Self>>) {
        let mut controller = controller.lock().expect("Failed to lock controller");
        controller.stop_current_timer();
    }

    pub fn pause(controller: &Arc<Mutex<Self>>) {
        let mut controller = controller.lock().expect("Failed to lock controller");
        controller.pause_current_timer();
    }

    fn start_current_timer(&mut self) {
        Timer::start(&self.timer);
    }

    fn stop_current_timer(&mut self) {
        let mut timer = self.timer.lock().expect("Failed to lock timer");
        timer.stop();
    }

    fn pause_current_timer(&mut self) {
        let mut timer = self.timer.lock().expect("Failed to lock timer");
        timer.pause();
    }

    fn start_next_timer(&mut self) {
        self.num_finished_timers += 1;

        self.stop_current_timer();

        let current_timer = self.timer.lock().expect("Failed to lock timer");

        let new_timer = match current_timer.timer_type() {
            TimerType::Work => {
                let mut duration = self.config.break_duration.clone();

                let num_finished_break_timers = self.num_finished_timers / 2;

                if num_finished_break_timers != 0
                    && num_finished_break_timers % (self.config.long_break_interval - 1) == 0
                {
                    duration = self.config.long_break_duration.clone();
                }

                Controller::create_timer(self.tx.clone(), TimerType::Break, duration)
            }
            TimerType::Break => Controller::create_timer(
                self.tx.clone(),
                TimerType::Work,
                self.config.work_duration.clone(),
            ),
        };

        drop(current_timer);

        self.timer = new_timer;

        self.attach_timer_handlers();

        self.start_current_timer();
    }

    fn on_timer_finished(&mut self) {
        // if Controller is in auto mode, start next timer
        if self.config.auto {
            self.start_next_timer();
            return;
        }
    }

    pub fn get_current_timer(controller: &Arc<Mutex<Self>>) -> Arc<Mutex<Timer>> {
        let controller = Arc::clone(controller);

        let controller = controller.lock().expect("Failed to lock controller");
        let timer = Arc::clone(&controller.timer);
        // drop(controller);
        timer
    }

    pub fn on(
        controller: &Arc<Mutex<Self>>,
        event: TimerEvent,
        callback: Arc<dyn Fn(&Timer) + Send + Sync>,
    ) {
        let mut controller = controller.lock().expect("Failed to lock controller");

        // save the handler for future timers
        controller
            .event_handlers
            .entry(event)
            .or_default()
            .push(callback.clone());

        // attach event listener to current timer
        let mut timer = controller.timer.lock().expect("Failed to lock timer");

        // attach the listener to the current timer
        timer.on(event, callback);
    }
}