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
//!
//! Chrome system notification API, allows creation of desktop / system notifications.
//!
//!  # Synopsis
//! ```rust
//! use nw_sys::notifications;
//!
//! // Create basic notification
//! let options = notifications::Options::new()
//!     .title("Title text")
//!     .icon_url("/resources/icons/tray-icon@2x.png")
//!     .set_type(notifications::TemplateType::Basic)
//!     .message("Message Text")
//!     .context_message("Context Message");
//!
//! notifications::create(None, &options, None);
//!
//! // Create notification with buttons
//! let button1 = notifications::Button::new()
//!     .title("Button A")
//!     .icon_url("/resources/icons/tray-icon@2x.png");
//!
//! let button2 = notifications::Button::new()
//!     .title("Button B")
//!     .icon_url("/resources/icons/tray-icon@2x.png");
//!
//! let options = notifications::Options::new()
//!     .title("Title text")
//!     .icon_url("/resources/icons/tray-icon@2x.png")
//!     .set_type(notifications::TemplateType::Basic)
//!     .message("Message Text")
//!     .buttons(vec![button1, button2]);
//!
//! notifications::create(None, &options, None);
//!
//! // Create image notification
//! let options = notifications::Options::new()
//!     .title("Title text")
//!     .icon_url("/resources/icons/tray-icon@2x.png")
//!     .set_type(notifications::TemplateType::Image)
//!     .message("Message Text")
//!     .image_url("/resources/setup/document.png");
//!
//! notifications::create(None, &options, None);
//!
//! // Create notification with items
//! let item1 = notifications::Item::new()
//!     .title("Item A")
//!     .message("Mesage A");
//! let item2 = notifications::Item::new()
//!     .title("Item B")
//!     .message("Mesage B");
//!
//! let options = notifications::Options::new()
//!     .title("Title text")
//!     .icon_url("/resources/icons/tray-icon@2x.png")
//!     .set_type(notifications::TemplateType::List)
//!     .message("Message Text")
//!     .items(vec![item1, item2]);
//!
//! notifications::create(None, &options, None);
//!
//! // Create notification with progress
//! let options = notifications::Options::new()
//!     .title("Title text")
//!     .icon_url("/resources/icons/tray-icon@2x.png")
//!     .set_type(notifications::TemplateType::Progress)
//!     .message("Mesage text")
//!     .progress(50);
//! notifications::create(None, &options, None);
//!  
//! ```
//!

use crate::options::OptionsTrait;
use js_sys::{Array, Function, Object};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace=["chrome", "notifications"], js_name="create")]
    fn create_impl(options: &Options);

    #[wasm_bindgen(js_namespace=["chrome", "notifications"], js_name="create")]
    fn create_with_id_impl(id: &str, options: &Options);

    #[wasm_bindgen(js_namespace=["chrome", "notifications"], js_name="create")]
    fn create_with_callback_impl(options: &Options, callback: &Function);

    #[wasm_bindgen(js_namespace=["chrome", "notifications"], js_name="create")]
    fn create_with_id_and_callback_impl(id: &str, options: &Options, callback: &Function);

    #[wasm_bindgen(js_namespace=["chrome", "notifications"], js_name="clear")]
    fn clear_impl(id: &str);

    #[wasm_bindgen(js_namespace=["chrome", "notifications"], js_name="clear")]
    fn clear_with_callback_impl(id: &str, callback: &Function);

    #[wasm_bindgen(js_namespace=["chrome", "notifications"], js_name="getAll")]
    /// Retrieves all the notifications of this app or extension.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#method-getAll)
    ///
    pub fn get_all(callback: &Function);

    #[wasm_bindgen(js_namespace=["chrome", "notifications"], js_name="getPermissionLevel")]
    /// Retrieves whether the user has enabled notifications from this app or extension.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#method-getPermissionLevel)
    ///
    pub fn get_permission_level(callback: &Function);

    #[wasm_bindgen(js_namespace=["chrome", "notifications"], js_name="update")]
    fn update_impl(id: &str, options: &Options);

    #[wasm_bindgen(js_namespace=["chrome", "notifications"], js_name="update")]
    fn update_with_callback_impl(id: &str, options: &Options, callback: &Function);

    #[wasm_bindgen(js_namespace=["chrome", "notifications", "onButtonClicked"], js_name="addListener")]
    /// The user pressed a button in the notification.
    ///
    /// The `callback` parameter looks like:
    /// (notification_id: String, button_index: u16) => ()
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#event-onButtonClicked)
    pub fn on_button_clicked(callback: &Function);

    #[wasm_bindgen(js_namespace=["chrome", "notifications", "onClicked"], js_name="addListener")]
    /// The user clicked in a non-button area of the notification.
    ///
    /// The `callback` parameter looks like:
    /// (notification_id: String) => ()
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#event-onClicked)
    pub fn on_clicked(callback: &Function);

    #[wasm_bindgen(js_namespace=["chrome", "notifications", "onClosed"], js_name="addListener")]
    /// The notification closed, either by the system or by user action.
    ///
    /// The `callback` parameter looks like:
    /// (notification_id: String, by_user:bool) => ()
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#event-onClosed)
    pub fn on_closed(callback: &Function);

    #[wasm_bindgen(js_namespace=["chrome", "notifications", "onPermissionLevelChanged"], js_name="addListener")]
    /// The user changes the permission level.
    /// As of Chrome 47, only ChromeOS has UI that dispatches this event.
    ///
    /// The `callback` parameter looks like:
    /// (level: JsValue ) => ()
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#event-onPermissionLevelChanged)
    pub fn on_permission_level_changed(callback: &Function);

    /// Notification Options
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#type-NotificationOptions)
    #[wasm_bindgen(extends = Object)]
    #[derive(Debug, Clone, PartialEq, Eq)]
    pub type Options;

    /// Notification Button
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#type-NotificationButton)
    #[wasm_bindgen(extends = Object)]
    #[derive(Debug, Clone, PartialEq, Eq)]
    pub type Button;

    /// Notification Item
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#type-NotificationItem)
    #[wasm_bindgen(extends = Object)]
    #[derive(Debug, Clone, PartialEq, Eq)]
    pub type Item;
}

impl OptionsTrait for Options {}
impl OptionsTrait for Button {}
impl OptionsTrait for Item {}

/// Notification template type
pub enum TemplateType {
    Basic,
    Image,
    List,
    Progress,
}

impl ToString for TemplateType {
    fn to_string(&self) -> String {
        match self {
            Self::Basic => "Basic".to_string(),
            Self::Image => "Image".to_string(),
            Self::List => "List".to_string(),
            Self::Progress => "Progress".to_string(),
        }
    }
}

impl From<TemplateType> for JsValue {
    fn from(value: TemplateType) -> Self {
        Self::from(value.to_string().to_lowercase())
    }
}

impl Button {
    /// Button icons not visible for Mac OS X users.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationButton-iconUrl)
    pub fn icon_url(self, url: &str) -> Self {
        self.set("iconUrl", JsValue::from(url))
    }

    /// Button text
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationButton-title)
    pub fn title(self, title: &str) -> Self {
        self.set("title", JsValue::from(title))
    }
}

impl Item {
    /// Additional details about this item.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationItem-message)
    pub fn message(self, message: &str) -> Self {
        self.set("message", JsValue::from(message))
    }

    /// Title of one item of a list notification.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationItem-title)
    pub fn title(self, title: &str) -> Self {
        self.set("title", JsValue::from(title))
    }
}

impl Options {
    ///
    /// *Note* : The app icon mask is not visible for Mac OS X users.
    ///
    /// A URL to the app icon mask.
    /// URLs have the same restrictions as iconUrl.
    /// The app icon mask should be in alpha channel, as only the alpha channel
    /// of the image will be considered.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-appIconMaskUrl)
    pub fn app_icon_mask_url(self, url: &str) -> Self {
        self.set("appIconMaskUrl", JsValue::from(url))
    }

    /// Text and icons for up to two notification action buttons.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-buttons)
    pub fn buttons(self, buttons: Vec<Button>) -> Self {
        let array = Array::new();
        for button in buttons {
            array.push(&JsValue::from(button));
        }
        self.set("buttons", JsValue::from(array))
    }

    /// Alternate notification content with a lower-weight font.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-contextMessage)
    pub fn context_message(self, context_message: &str) -> Self {
        self.set("contextMessage", JsValue::from(context_message))
    }

    /// A timestamp associated with the notification, in milliseconds past the epoch (e.g. Date.now() + n).
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-eventTime)
    pub fn event_time(self, event_time: u32) -> Self {
        self.set("eventTime", JsValue::from(event_time))
    }

    /// A URL to the sender's avatar, app icon, or a thumbnail for image notifications.
    ///
    /// URLs can be a data URL, a blob URL, or a URL relative to a
    /// resource within this extension's .crx file Required for [notifications.create()](self::create) method.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-iconUrl)
    pub fn icon_url(self, icon_url: &str) -> Self {
        self.set("iconUrl", JsValue::from(icon_url))
    }

    /// The image is not visible for Mac OS X users.
    ///
    /// A URL to the image thumbnail for image-type notifications.
    /// URLs have the same restrictions as [iconUrl](self::Options#method.icon_url).
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-imageUrl)
    pub fn image_url(self, image_url: &str) -> Self {
        self.set("imageUrl", JsValue::from(image_url))
    }

    /// Items for multi-item notifications.
    /// Users on Mac OS X only see the first item.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-items)
    pub fn items(self, items: Vec<Item>) -> Self {
        let array = Array::new();
        for item in items {
            array.push(&JsValue::from(item));
        }
        self.set("items", JsValue::from(array))
    }

    /// Main notification content.
    /// Required for [notifications.create](self::create) method.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-message)
    pub fn message(self, message: &str) -> Self {
        self.set("message", JsValue::from(message))
    }

    /// Priority ranges from -2 to 2. -2 is lowest priority. 2 is highest.
    /// Zero is default.
    /// On platforms that don't support a notification center
    /// (Windows, Linux & Mac), -2 and -1 result in an error as notifications with
    /// those priorities will not be shown at all.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-priority)
    pub fn priority(self, priority: i8) -> Self {
        self.set("priority", JsValue::from(priority))
    }

    /// Current progress ranges from 0 to 100.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-progress)
    pub fn progress(self, progress: u8) -> Self {
        self.set("progress", JsValue::from(progress))
    }

    /// Indicates that the notification should remain visible on screen until
    /// the user activates or dismisses the notification. This defaults to false.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-requireInteraction)
    pub fn require_interaction(self, require_interaction: bool) -> Self {
        self.set("requireInteraction", JsValue::from(require_interaction))
    }

    /// Indicates that no sounds or vibrations should be made when the
    /// notification is being shown. This defaults to false.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-silent)
    pub fn silent(self, silent: bool) -> Self {
        self.set("silent", JsValue::from(silent))
    }

    /// Title of the notification (e.g. sender name for email).
    /// Required for [notifications.create](self::create) method.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-title)
    pub fn title(self, title: &str) -> Self {
        self.set("title", JsValue::from(title))
    }

    /// Which type of notification to display.
    ///
    /// Required for [notifications.create](self::create) method.
    ///
    /// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#property-NotificationOptions-type)
    pub fn set_type(self, template_type: TemplateType) -> Self {
        self.set("type", template_type.into())
    }
}

/// Creates and displays a notification.
///
/// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#method-create)
///
pub fn create(id: Option<String>, options: &Options, callback: Option<&Function>) {
    match (id, callback) {
        (Some(id), Some(callback)) => {
            create_with_id_and_callback_impl(&id, options, callback);
        }
        (Some(id), None) => {
            create_with_id_impl(&id, options);
        }
        (None, Some(callback)) => {
            create_with_callback_impl(options, callback);
        }
        (None, None) => {
            create_impl(options);
        }
    }
}

/// Clears the specified notification.
///
/// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#method-clear)
///
pub fn clear(id: &str, callback: Option<&Function>) {
    if let Some(callback) = callback {
        clear_with_callback_impl(id, callback);
    } else {
        clear_impl(id);
    }
}

/// Updates an existing notification.
///
/// [Chrome Doc](https://developer.chrome.com/docs/extensions/reference/notifications/#method-update)
///
pub fn update(id: &str, options: &Options, callback: Option<&Function>) {
    if let Some(callback) = callback {
        update_with_callback_impl(id, options, callback);
    } else {
        update_impl(id, options);
    }
}