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
/* view_home_page.rs
 *
 * Copyright 2021 Visvesh Subramanian <visveshs.blogspot.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

use crate::{
    model::User,
    plugins::{PluginDetails, PluginName, PluginObject, PluginSummaryRow, Registrar},
    prelude::*,
};
use adw::prelude::*;
use gtk::glib::{self, subclass::prelude::*};
use std::str::FromStr;

mod imp {
    use crate::{
        core::{Database, Settings},
        plugins::{PluginObject, PluginOverviewRow, PluginSummaryRow, Registrar},
        prelude::*,
    };
    use adw::{prelude::*, subclass::prelude::*};
    use gtk::{
        glib::{self, subclass::Signal, Cast},
        CompositeTemplate,
    };
    use num_traits::cast::ToPrimitive;

    #[derive(Debug, CompositeTemplate, Default)]
    #[template(resource = "/dev/Cogitri/Health/ui/view_home_page.ui")]
    pub struct ViewHomePage {
        pub settings: Settings,
        pub database: Database,

        #[template_child]
        pub stack: TemplateChild<gtk::Stack>,
        #[template_child]
        pub user_selected_data: TemplateChild<gtk::ListBox>,
        #[template_child]
        pub all_data: TemplateChild<gtk::ListBox>,
        #[template_child]
        pub all_data_box: TemplateChild<gtk::Box>,
        #[template_child]
        pub size_group: TemplateChild<gtk::SizeGroup>,
        #[template_child]
        pub summary_size_group: TemplateChild<gtk::SizeGroup>,
        #[template_child]
        pub enabled_plugins_stack: TemplateChild<gtk::Stack>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for ViewHomePage {
        const NAME: &'static str = "HealthViewHomePage";
        type ParentType = adw::Bin;
        type Type = super::ViewHomePage;

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);
            Self::bind_template_callbacks(klass);
        }

        fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
            obj.init_template();
        }
    }

    impl WidgetImpl for ViewHomePage {}

    impl ObjectImpl for ViewHomePage {
        fn signals() -> &'static [Signal] {
            use once_cell::sync::Lazy;
            static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
                vec![Signal::builder("view-changed")
                    .param_types([String::static_type()])
                    .build()]
            });

            SIGNALS.as_ref()
        }

        fn constructed(&self) {
            self.parent_constructed();

            let obj = self.obj();
            let registrar = Registrar::instance();
            let disabled_model = registrar.disabled_plugins();
            let enabled_model = registrar.enabled_plugins();

            let sorter = gtk::CustomSorter::new(|a, b| {
                a.downcast_ref::<PluginObject>()
                    .unwrap()
                    .plugin()
                    .name()
                    .to_u8()
                    .unwrap()
                    .cmp(
                        &b.downcast_ref::<PluginObject>()
                            .unwrap()
                            .plugin()
                            .name()
                            .to_u8()
                            .unwrap(),
                    )
                    .into()
            });
            let enabled_model_sorted =
                gtk::SortListModel::new(Some(enabled_model), Some(sorter.clone()));
            let disabled_model_sorted = gtk::SortListModel::new(Some(disabled_model), Some(sorter));

            self.user_selected_data.bind_model(
                Some(&enabled_model_sorted),
                glib::clone!(@weak obj => @default-panic, move |o| {
                    obj.handle_user_selected_data_bind_model(o)
                }),
            );
            self.all_data.bind_model(
                Some(&disabled_model_sorted),
                glib::clone!(@weak obj => @default-panic, move |o| {
                    obj.handle_all_data_bind_model(o)
                }),
            );

            obj.handle_registrar_plugins_changed();
            registrar.connect_plugins_updated(glib::clone!(@weak obj => move |_| {
                obj.handle_registrar_plugins_changed();
            }));
        }
    }

    impl BinImpl for ViewHomePage {}

    #[gtk::template_callbacks]
    impl ViewHomePage {
        #[template_callback]
        fn handle_user_selected_data_row_activated(
            &self,
            row: gtk::ListBoxRow,
            list_box: gtk::ListBox,
        ) {
            let summary = row.downcast_ref::<PluginSummaryRow>().unwrap();
            let plugin_name = summary.plugin_name();
            self.obj().open_plugin_details(list_box, plugin_name, true);
        }

        #[template_callback]
        fn handle_all_data_row_activated(&self, row: gtk::ListBoxRow, list_box: gtk::ListBox) {
            let overview = row.downcast_ref::<PluginOverviewRow>().unwrap();
            let plugin_name = overview.plugin_name();
            self.obj().open_plugin_details(list_box, plugin_name, false);
        }
    }
}

glib::wrapper! {
    /// An implementation of [View] visualizes activities the user recently did.
    pub struct ViewHomePage(ObjectSubclass<imp::ViewHomePage>)
        @extends gtk::Widget, adw::Bin,
        @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
}

impl ViewHomePage {
    pub fn back(&self) {
        let imp = self.imp();
        imp.stack
            .remove(&imp.stack.child_by_name(&self.current_page()).unwrap());
        imp.stack.set_visible_child_name("home");
    }

    /// Connect to the `view-changed` signal.
    ///
    /// # Arguments
    /// * `callback` - The callback which should be invoked when `view-changed` is emitted.
    ///
    /// # Returns
    /// A [glib::SignalHandlerId] that can be used for disconnecting the signal if so desired.
    pub fn connect_view_changed<F: Fn(&Self, PluginName) + 'static>(
        &self,
        callback: F,
    ) -> glib::SignalHandlerId {
        self.connect_local("view-changed", false, move |values| {
            callback(
                &values[0].get().unwrap(),
                PluginName::from_str(values[1].get::<&str>().unwrap()).unwrap(),
            );
            None
        })
    }

    pub fn current_page(&self) -> String {
        self.imp().stack.visible_child_name().unwrap().to_string()
    }

    pub async fn get_user(&self) -> User {
        let imp = self.imp();
        let user_id = i64::from(imp.settings.active_user_id());
        let user = &imp.database.user(user_id).await.unwrap();
        user.clone()
    }

    pub fn is_current_plugin_enabled(&self) -> bool {
        let registrar = Registrar::instance();
        let current_page = self.current_page();
        registrar
            .enabled_plugins()
            .iter()
            .any(|p| p.name().as_ref() == current_page.as_str())
    }

    pub async fn disable_current_plugin(&self) {
        let imp = self.imp();
        let registrar = Registrar::instance();
        let user = self.get_user().await;

        if let Ok(current_plugin) = PluginName::from_str(&self.current_page()) {
            registrar.disable_plugin(current_plugin);
            imp.all_data_box.set_visible(true);
            user.set_enabled_plugins(Some(
                user.enabled_plugins()
                    .unwrap()
                    .drain(..)
                    .filter(|s| *s != current_plugin)
                    .collect::<Vec<PluginName>>()
                    .as_slice()
                    .to_vec(),
            ));
            if registrar.enabled_plugins().is_empty() {
                imp.enabled_plugins_stack
                    .set_visible_child_name("no-plugins-enabled")
            }
        }
    }

    pub async fn enable_current_plugin(&self) {
        let imp = self.imp();
        let registrar = Registrar::instance();
        let user = self.get_user().await;

        if let Ok(current_plugin) = PluginName::from_str(&self.current_page()) {
            let mut enabled_plugins = user.enabled_plugins().unwrap();
            enabled_plugins.push(current_plugin);
            registrar.enable_plugin(current_plugin);
            user.set_enabled_plugins(Some(enabled_plugins));
            imp.enabled_plugins_stack
                .set_visible_child_name("plugin-list");
            if registrar.disabled_plugins().is_empty() {
                imp.all_data_box.set_visible(false);
            }
        }
    }

    /// Create a new [ViewHomePage] to display previous activities.
    pub fn new() -> Self {
        glib::Object::new()
    }

    fn handle_user_selected_data_bind_model(&self, object: &glib::Object) -> gtk::Widget {
        let summary = object
            .downcast_ref::<PluginObject>()
            .unwrap()
            .plugin()
            .summary();

        gtk_macros::spawn!(glib::clone!(@weak summary => async move {
            if let Err(e) = summary.update().await {
                glib::g_warning!(crate::config::LOG_DOMAIN, "Couldn't update plugin: {e}");
            }
        }));

        self.imp().size_group.add_widget(&summary);

        summary.upcast()
    }

    fn handle_all_data_bind_model(&self, object: &glib::Object) -> gtk::Widget {
        let overview = object
            .downcast_ref::<PluginObject>()
            .unwrap()
            .plugin()
            .overview();

        self.imp().size_group.add_widget(&overview);
        self.imp()
            .summary_size_group
            .add_widget(&overview.icon_widget());

        overview.upcast()
    }

    fn open_plugin_details(&self, list_box: gtk::ListBox, plugin_name: PluginName, enabled: bool) {
        let imp = self.imp();
        let registrar = Registrar::instance();
        let plugin = if enabled {
            registrar.enabled_plugin_by_name(plugin_name).unwrap()
        } else {
            registrar.disabled_plugin_by_name(plugin_name).unwrap()
        };
        let details = plugin.details(!enabled);

        imp.stack.add_named(&details, Some(plugin_name.as_ref()));

        gtk_macros::spawn!(glib::clone!(@weak self as obj => async move {
            obj.update_view(details, plugin_name, list_box).await;
        }));
    }

    pub async fn update(&self) {
        let imp = self.imp();
        let mut i = 0;
        while let Some(row) = imp.user_selected_data.row_at_index(i) {
            if let Err(e) = row
                .downcast_ref::<PluginSummaryRow>()
                .unwrap()
                .update()
                .await
            {
                glib::g_warning!(crate::config::LOG_DOMAIN, "Couldn't update plugin: {e}");
            }
            i += 1;
        }
    }

    async fn update_view(
        &self,
        details: PluginDetails,
        plugin_name: PluginName,
        list_box: gtk::ListBox,
    ) {
        let imp = self.imp();

        if let Err(e) = details.update().await {
            glib::g_warning!(
                crate::config::LOG_DOMAIN,
                "Couldn't update plugin's details: {e}",
            );
        }

        imp.stack.set_visible_child_name(plugin_name.as_ref());
        self.emit_by_name::<()>("view-changed", &[&plugin_name.as_ref()]);
        list_box.unselect_all();
    }

    fn handle_registrar_plugins_changed(&self) {
        let imp = self.imp();
        let registrar = Registrar::instance();
        let disabled_model = registrar.disabled_plugins();
        let enabled_model = registrar.enabled_plugins();

        imp.all_data_box.set_visible(!disabled_model.is_empty());

        if enabled_model.is_empty() {
            imp.enabled_plugins_stack
                .set_visible_child_name("no-plugins-enabled");
        } else {
            imp.enabled_plugins_stack
                .set_visible_child_name("plugin-list")
        }
    }
}

#[cfg(test)]
mod test {
    use super::ViewHomePage;
    use crate::utils::init_gtk;

    #[gtk::test]
    fn new() {
        init_gtk();
        ViewHomePage::new();
    }
}