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
mod component;
pub mod component_factory;

pub use component::{NotificationComponent, NotificationComponentProps};
pub use component_factory::NotificationFactory;
use time::{Duration, OffsetDateTime};
use uuid::Uuid;
use yew::{classes, Classes};

use crate::Notifiable;

/// Standard notification type.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum NotificationType {
    /// Represents some informative message for the user.
    #[default]
    Info,

    /// Represents some warning.
    Warn,

    /// Represents some error message.
    Error,

    /// Custom notification type.
    ///
    /// You can use this option when you want to set the custom style of your notification
    /// but don't want to write an entire custom component from scratch.
    Custom(Classes),
}

impl From<&str> for NotificationType {
    fn from(data: &str) -> Self {
        match data {
            "info" => Self::Info,
            "warn" => Self::Warn,
            "error" => Self::Error,
            data => Self::Custom(data.to_owned().into()),
        }
    }
}

impl From<&NotificationType> for Classes {
    fn from(notification_type: &NotificationType) -> Self {
        match notification_type {
            NotificationType::Info => classes!("info"),
            NotificationType::Warn => classes!("warn"),
            NotificationType::Error => classes!("error"),
            NotificationType::Custom(classes) => classes.clone(),
        }
    }
}

/// Standard notification.
#[derive(Debug, Clone, PartialEq)]
pub struct Notification {
    pub(crate) id: Uuid,
    pub(crate) notification_type: NotificationType,
    pub(crate) title: Option<String>,
    pub(crate) text: String,

    pub(crate) spawn_time: OffsetDateTime,
    pub(crate) lifetime: Duration,
    pub(crate) full_lifetime: Duration,
    pub(crate) paused: bool,
}

impl Notification {
    pub const NOTIFICATION_LIFETIME: Duration = Duration::seconds(3);

    /// Creates a new standard notification from notification type, title, text, and lifetime duration.
    pub fn new(
        notification_type: NotificationType,
        title: impl Into<String>,
        text: impl Into<String>,
        lifetime: Duration,
    ) -> Self {
        Self {
            id: Uuid::new_v4(),
            notification_type,
            title: Some(title.into()),
            text: text.into(),

            spawn_time: OffsetDateTime::now_local().expect("Can not acquire local current time"),
            lifetime,
            full_lifetime: lifetime,
            paused: false,
        }
    }

    /// Creates a new standard notification from notification type and text.
    ///
    /// Title will be omitted. Notification lifetime is equal to the [`Self::NOTIFICATION_LIFETIME`].
    pub fn from_description_and_type(notification_type: NotificationType, text: impl Into<String>) -> Self {
        Self {
            id: Uuid::new_v4(),
            notification_type,
            title: None,
            text: text.into(),

            spawn_time: OffsetDateTime::now_local().expect("Can not acquire local current time"),
            lifetime: Self::NOTIFICATION_LIFETIME,
            full_lifetime: Self::NOTIFICATION_LIFETIME,
            paused: false,
        }
    }

    /// Set the title for the notification.
    pub fn with_title(self, new_title: impl Into<String>) -> Self {
        let Notification {
            id,
            notification_type,
            title: _,
            text: description,

            spawn_time,
            lifetime,
            full_lifetime,
            paused,
        } = self;

        Self {
            id,
            notification_type,
            title: Some(new_title.into()),
            text: description,

            spawn_time,
            lifetime,
            full_lifetime,
            paused,
        }
    }

    /// Set the type for the notification.
    pub fn with_type(self, new_notification_type: NotificationType) -> Self {
        let Notification {
            id,
            notification_type: _,
            title,
            text: description,

            spawn_time,
            lifetime,
            full_lifetime,
            paused,
        } = self;

        Self {
            id,
            notification_type: new_notification_type,
            title,
            text: description,

            spawn_time,
            lifetime,
            full_lifetime,
            paused,
        }
    }

    /// Set the text for the notification.
    pub fn with_text(self, new_text: impl Into<String>) -> Self {
        let Notification {
            id,
            notification_type,
            title,
            text: _,

            spawn_time,
            lifetime,
            full_lifetime,
            paused,
        } = self;

        Self {
            id,
            notification_type,
            title,
            text: new_text.into(),

            spawn_time,
            lifetime,
            full_lifetime,
            paused,
        }
    }

    /// Resets notification lifetime.
    ///
    /// It means that after this method invocation, the lifetime of the notification will be equal to the start value.
    pub fn reset_lifetime(self) -> Self {
        let Notification {
            id,
            notification_type,
            title,
            text,

            spawn_time,
            lifetime: _,
            full_lifetime,
            paused,
        } = self;

        Self {
            id,
            notification_type,
            title,
            text,

            spawn_time,
            lifetime: full_lifetime,
            full_lifetime,
            paused,
        }
    }
}

impl Notifiable for Notification {
    fn id(&self) -> Uuid {
        self.id
    }

    fn apply_tick(&mut self, time: Duration) {
        self.lifetime = self.lifetime.checked_sub(time).unwrap_or_default();
    }

    fn is_alive(&self) -> bool {
        self.lifetime != Duration::default()
    }

    fn mouse_in(&mut self) {
        self.paused = true;
    }

    fn mouse_out(&mut self) {
        self.paused = false;
        self.lifetime = self.full_lifetime;
    }

    fn is_paused(&self) -> bool {
        self.paused
    }
}