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
//! Activity entity and business logic

use chrono::Local;
use core::fmt::Formatter;
use getset::{Getters, MutGetters, Setters};
use merge::Merge;
use serde_derive::{Deserialize, Serialize};
use std::fmt::Display;
use typed_builder::TypedBuilder;
use ulid::Ulid;

use crate::{
    calculate_duration,
    domain::{
        status::ActivityStatus,
        time::{duration_to_str, PaceDateTime, PaceDuration},
    },
    PaceResult,
};

#[derive(Debug, TypedBuilder, Getters, Setters, MutGetters, Clone, Eq, PartialEq, Default)]
#[getset(get = "pub", get_mut = "pub", set = "pub")]
pub struct ActivityItem {
    guid: ActivityGuid,
    activity: Activity,
}

impl ActivityItem {
    /// Create a new `ActivityItem`
    ///
    /// # Arguments
    ///
    /// * `guid` - The unique identifier of the activity
    /// * `activity` - The activity
    ///
    /// # Returns
    ///
    /// Returns a new `ActivityItem`
    pub fn new(guid: ActivityGuid, activity: Activity) -> Self {
        Self { guid, activity }
    }

    /// Consumes the `ActivityItem` and returns the inner `ActivityGuid` and `Activity`
    pub fn into_parts(self) -> (ActivityGuid, Activity) {
        (self.guid, self.activity)
    }
}

impl From<Activity> for ActivityItem {
    fn from(activity: Activity) -> Self {
        Self {
            guid: ActivityGuid::default(),
            activity,
        }
    }
}

impl From<(ActivityGuid, Activity)> for ActivityItem {
    fn from((guid, activity): (ActivityGuid, Activity)) -> Self {
        Self { guid, activity }
    }
}

/// The kind of activity a user can track
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, Hash, Copy)]
#[serde(rename_all = "kebab-case")]
// #[serde(untagged)]
pub enum ActivityKind {
    /// A generic activity
    #[default]
    Activity,

    /// A task
    Task,

    /// A break
    Intermission,

    /// A pomodoro work session
    PomodoroWork,

    /// A pomodoro break
    PomodoroIntermission,
}

impl ActivityKind {
    /// Returns `true` if the activity kind is [`Activity`].
    ///
    /// [`Activity`]: ActivityKind::Activity
    #[must_use]
    pub fn is_activity(&self) -> bool {
        matches!(self, Self::Activity)
    }

    /// Returns `true` if the activity kind is [`Task`].
    ///
    /// [`Task`]: ActivityKind::Task
    #[must_use]
    pub fn is_task(&self) -> bool {
        matches!(self, Self::Task)
    }

    /// Returns `true` if the activity kind is [`Intermission`].
    ///
    /// [`Intermission`]: ActivityKind::Intermission
    #[must_use]
    pub fn is_intermission(&self) -> bool {
        matches!(self, Self::Intermission)
    }

    /// Returns `true` if the activity kind is [`PomodoroWork`].
    ///
    /// [`PomodoroWork`]: ActivityKind::PomodoroWork
    #[must_use]
    pub fn is_pomodoro_work(&self) -> bool {
        matches!(self, Self::PomodoroWork)
    }

    /// Returns `true` if the activity kind is [`PomodoroIntermission`].
    ///
    /// [`PomodoroIntermission`]: ActivityKind::PomodoroIntermission
    #[must_use]
    pub fn is_pomodoro_intermission(&self) -> bool {
        matches!(self, Self::PomodoroIntermission)
    }

    pub fn to_symbol(&self) -> &'static str {
        match self {
            Self::Activity => "📆",
            Self::Task => "📋",
            Self::Intermission => "⏸️",
            Self::PomodoroWork => "🍅⏲️",
            Self::PomodoroIntermission => "🍅⏸️",
        }
    }
}

/// The cycle of pomodoro activity a user can track
// TODO!: Optional: Track Pomodoro work/break cycles
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
enum PomodoroCycle {
    /// A work session
    Work(usize), // usize could represent the work session number in a sequence

    // A break
    #[default]
    Intermission,
}

/// The activity entity
///
/// The activity entity is used to store and manage an activity
#[derive(
    Debug,
    Serialize,
    Deserialize,
    TypedBuilder,
    Getters,
    Setters,
    MutGetters,
    Clone,
    Eq,
    PartialEq,
    Default,
)]
#[getset(get = "pub", set = "pub", get_mut = "pub")]
#[derive(Merge)]
pub struct Activity {
    /// The category of the activity
    // TODO: We had it as a struct before with an ID, but it's questionable if we should go for this
    // TODO: Reconsider when we implement the project management part
    // category: Category,
    #[builder(default, setter(into))]
    #[getset(get = "pub", get_mut = "pub")]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[merge(strategy = crate::util::overwrite_left_with_right)]
    category: Option<String>,

    /// The description of the activity
    // This needs to be an Optional, because we use the whole activity struct
    // as well for intermissions, which don't have a description
    #[builder(setter(into))]
    #[merge(strategy = crate::util::overwrite_left_with_right)]
    description: String,

    /// The start date and time of the activity
    #[builder(default, setter(into))]
    #[getset(get = "pub")]
    // TODO: Should the begin time be updatable?
    #[merge(skip)]
    begin: PaceDateTime,

    #[builder(default)]
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    #[getset(get = "pub", get_mut = "pub")]
    #[merge(strategy = crate::util::overwrite_left_with_right)]
    activity_end_options: Option<ActivityEndOptions>,

    /// The kind of activity
    #[builder(default)]
    #[merge(skip)]
    kind: ActivityKind,

    /// Optional attributes for the activity kind
    #[builder(default, setter(into))]
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    #[merge(strategy = crate::util::overwrite_left_with_right)]
    activity_kind_options: Option<ActivityKindOptions>,

    // TODO: How to better support subcategories
    // subcategory: Option<Category>,

    // TODO: Was `Tag` before, but we want to check how to better support that
    // TODO: also, we should consider using a HashSet instead of a Vec
    // TODO: also, we might want to reconsider
    // #[builder(default, setter(strip_option))]
    // tags: Option<Vec<String>>,

    // Pomodoro-specific attributes
    /// The pomodoro cycle of the activity
    #[builder(default, setter(into))]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[merge(strategy = crate::util::overwrite_left_with_right)]
    pomodoro_cycle_options: Option<PomodoroCycle>,

    #[serde(default)]
    #[builder(default)]
    #[merge(strategy = crate::util::overwrite_left_with_right)]
    status: ActivityStatus,
}

#[derive(
    Debug, Serialize, Deserialize, TypedBuilder, Getters, Setters, MutGetters, Clone, Eq, PartialEq,
)]
#[getset(get = "pub")]
pub struct ActivityEndOptions {
    /// The end date and time of the activity
    #[builder(default)]
    #[getset(get = "pub")]
    end: PaceDateTime,

    /// The duration of the activity
    #[builder(default)]
    #[getset(get = "pub")]
    duration: PaceDuration,
}

impl ActivityEndOptions {
    pub fn new(end: PaceDateTime, duration: PaceDuration) -> Self {
        Self { end, duration }
    }
}

#[derive(
    Debug,
    Serialize,
    Deserialize,
    TypedBuilder,
    Getters,
    Setters,
    MutGetters,
    Clone,
    Eq,
    PartialEq,
    Default,
)]
#[getset(get = "pub", set = "pub", get_mut = "pub")]
#[derive(Merge)]
#[serde(rename_all = "kebab-case")]
pub struct ActivityKindOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[merge(skip)]
    parent_id: Option<ActivityGuid>,
}

impl ActivityKindOptions {
    pub fn with_parent_id(parent_id: ActivityGuid) -> Self {
        Self {
            parent_id: parent_id.into(),
        }
    }
}

/// The unique identifier of an activity
#[derive(Debug, Clone, Serialize, Deserialize, Ord, PartialEq, PartialOrd, Eq, Copy, Hash)]
pub struct ActivityGuid(Ulid);

impl Display for ActivityGuid {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl Default for ActivityGuid {
    fn default() -> Self {
        Self(Ulid::new())
    }
}

impl Display for Activity {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let rel_time = match self.begin.and_local_timezone(Local) {
            chrono::LocalResult::Single(time) => duration_to_str(time),
            chrono::LocalResult::None | chrono::LocalResult::Ambiguous(_, _) => {
                format!("at {}", self.begin)
            }
        };

        let nop_cat = "Uncategorized".to_string();

        write!(
            f,
            "{}  Activity: \"{}\" ({}) started {}",
            self.kind.to_symbol(),
            self.description(),
            self.category().as_ref().unwrap_or(&nop_cat),
            rel_time,
        )
    }
}

#[cfg(feature = "sqlite")]
impl rusqlite::types::FromSql for ActivityGuid {
    fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> {
        let bytes = <[u8; 16]>::column_result(value)?;
        Ok(Self(Ulid::from(u128::from_be_bytes(bytes))))
    }
}

#[cfg(feature = "sqlite")]
impl rusqlite::types::ToSql for ActivityGuid {
    fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
        Ok(rusqlite::types::ToSqlOutput::from(self.0.to_string()))
    }
}

impl Activity {
    /// Create a new activity from this activity to resume
    /// an already ended/archived/etc. activity
    pub fn new_from_self(&self) -> Self {
        Self::builder()
            .description(self.description.clone())
            .category(self.category.clone())
            .kind(self.kind)
            .activity_kind_options(self.activity_kind_options.clone())
            .pomodoro_cycle_options(self.pomodoro_cycle_options)
            .build()
    }

    /// If the activity is held
    pub fn is_held(&self) -> bool {
        self.status.is_held()
    }

    /// If the activity is active, so if it is currently being tracked
    /// Intermissions are not considered active activities, please use
    /// [`is_active_intermission`] for that
    #[must_use]
    pub fn is_active(&self) -> bool {
        self.activity_end_options().is_none()
            && (!self.kind.is_intermission() || !self.kind.is_pomodoro_intermission())
            && self.status.is_active()
    }

    /// Make the activity active
    pub fn make_active(&mut self) {
        self.status = ActivityStatus::Active;
    }

    /// Make the activity inactive
    pub fn make_inactive(&mut self) {
        self.status = ActivityStatus::Inactive;
    }

    /// Archive the activity
    /// This is only possible if the activity is not active and has ended
    pub fn archive(&mut self) {
        if !self.is_active() && self.has_ended() {
            self.status = ActivityStatus::Archived;
        }
    }

    /// Unarchive the activity
    /// This is only possible if the activity is archived
    pub fn unarchive(&mut self) {
        if self.is_archived() {
            self.status = ActivityStatus::Unarchived;
        }
    }

    /// If the activity is an active intermission
    #[must_use]
    pub fn is_active_intermission(&self) -> bool {
        self.activity_end_options().is_none()
            && (self.kind.is_intermission() || self.kind.is_pomodoro_intermission())
            && self.status.is_active()
    }

    /// If the activity is archived
    #[must_use]
    pub fn is_archived(&self) -> bool {
        self.status.is_archived()
    }

    /// If the activity is inactive
    #[must_use]
    pub fn is_inactive(&self) -> bool {
        self.status.is_inactive()
    }

    /// If the activity has ended and is not archived
    #[must_use]
    pub fn has_ended(&self) -> bool {
        self.activity_end_options().is_some()
            && (!self.kind.is_intermission() || !self.kind.is_pomodoro_intermission())
            && !self.is_archived()
            && self.status.is_ended()
    }

    /// End the activity
    ///
    /// # Arguments
    ///
    /// * `end` - The end date and time of the activity
    /// * `duration` - The [`PaceDuration`] of the activity
    pub fn end_activity(&mut self, end_opts: ActivityEndOptions) {
        self.activity_end_options = Some(end_opts);
        self.status = ActivityStatus::Ended;
    }

    /// End the activity with a given end date and time
    ///
    /// # Arguments
    ///
    /// * `begin` - The begin date and time of the activity (for calculating the duration)
    /// * `end` - The end date and time of the activity
    ///
    /// # Errors
    ///
    /// Returns an error if the duration cannot be calculated
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if the activity is ended successfully
    pub fn end_activity_with_duration_calc(
        &mut self,
        begin: PaceDateTime,
        end: PaceDateTime,
    ) -> PaceResult<()> {
        let end_opts = ActivityEndOptions::new(end, calculate_duration(&begin, end)?);

        self.end_activity(end_opts);

        Ok(())
    }

    /// Get `parent_id` if activity is intermission
    ///
    /// # Returns
    ///
    /// * `Some(ActivityGuid)` - The `parent_id` of the activity
    /// * `None` - If the activity is not an intermission
    #[must_use]
    pub fn parent_id(&self) -> Option<ActivityGuid> {
        self.activity_kind_options
            .as_ref()
            .and_then(|opts| opts.parent_id)
    }
}

#[cfg(test)]
mod tests {

    use std::str::FromStr;

    use chrono::NaiveDateTime;

    use super::*;

    #[test]
    fn test_parse_single_toml_activity_passes() {
        let toml = r#"
            category = "Work"
            description = "This is an example activity"
            end = "2021-08-01T12:00:00"
            begin = "2021-08-01T10:00:00"
            duration = 5
            kind = "activity"
        "#;

        let activity: Activity = toml::from_str(toml).unwrap();

        assert_eq!(activity.category.as_ref().unwrap(), "Work");

        assert_eq!(activity.description, "This is an example activity");

        let ActivityEndOptions { end, duration } = activity.activity_end_options().clone().unwrap();

        assert_eq!(
            end,
            PaceDateTime::from(
                NaiveDateTime::parse_from_str("2021-08-01T12:00:00", "%Y-%m-%dT%H:%M:%S").unwrap()
            )
        );

        assert_eq!(
            activity.begin,
            PaceDateTime::from(
                NaiveDateTime::parse_from_str("2021-08-01T10:00:00", "%Y-%m-%dT%H:%M:%S").unwrap()
            )
        );

        assert_eq!(duration, PaceDuration::from_str("5").unwrap());

        assert_eq!(activity.kind, ActivityKind::Activity);
    }

    #[test]
    fn test_parse_single_toml_intermission_passes() {
        let toml = r#"
            end = "2021-08-01T12:00:00"
            begin = "2021-08-01T10:00:00"
            description = "This is an example activity"
            duration = 50
            kind = "intermission"
            parent-id = "01F9Z4Z3Z3Z3Z4Z3Z3Z3Z3Z3Z4" 
        "#;

        let activity: Activity = toml::from_str(toml).unwrap();

        let ActivityEndOptions { end, duration } = activity.activity_end_options().clone().unwrap();

        assert_eq!(
            end,
            PaceDateTime::from(
                NaiveDateTime::parse_from_str("2021-08-01T12:00:00", "%Y-%m-%dT%H:%M:%S").unwrap()
            )
        );

        assert_eq!(duration, PaceDuration::from_str("50").unwrap());

        assert_eq!(
            activity.begin,
            PaceDateTime::from(
                NaiveDateTime::parse_from_str("2021-08-01T10:00:00", "%Y-%m-%dT%H:%M:%S").unwrap()
            )
        );

        assert_eq!(activity.kind, ActivityKind::Intermission);

        assert_eq!(
            activity
                .activity_kind_options
                .unwrap()
                .parent_id
                .unwrap()
                .to_string(),
            "01F9Z4Z3Z3Z3Z4Z3Z3Z3Z3Z3Z4"
        );
    }
}