/*
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com>

This file is part of FitnessTrax.

FitnessTrax 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.

FitnessTrax 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 FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
*/

use crate::components::FileChooserRow;
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use std::path::PathBuf;

/// This is the view to show if the application has not yet been configured. It will walk the user
/// through the most critical setup steps so that we can move on to the other views in the app.
pub struct WelcomeViewPrivate {}

#[glib::object_subclass]
impl ObjectSubclass for WelcomeViewPrivate {
    const NAME: &'static str = "WelcomeView";
    type Type = WelcomeView;
    type ParentType = gtk::Box;

    fn new() -> Self {
        Self {}
    }
}

impl ObjectImpl for WelcomeViewPrivate {}
impl WidgetImpl for WelcomeViewPrivate {}
impl BoxImpl for WelcomeViewPrivate {}

glib::wrapper! {
    pub struct WelcomeView(ObjectSubclass<WelcomeViewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}

impl WelcomeView {
    pub fn new<OnSave>(on_save: OnSave) -> Self
    where
        OnSave: Fn(PathBuf) + 'static,
    {
        let s: Self = Object::builder().build();
        s.set_orientation(gtk::Orientation::Vertical);
        s.set_css_classes(&["welcome"]);

        // Replace this with the welcome screen that we set up in the fitnesstrax/unconfigured-page
        // branch.
        let title = gtk::Label::builder()
            .label("Welcome to FitnessTrax")
            .css_classes(["welcome__title"])
            .build();

        let content = gtk::Box::builder()
            .orientation(gtk::Orientation::Vertical)
            .vexpand(true)
            .build();

        let save_button = gtk::Button::builder()
            .label("Save Settings")
            .sensitive(false)
            .build();

        // The database selection row should be a box that shows a default database path, along with a
        // button that triggers a file chooser dialog. Once the dialog returns, the box should be
        // updated to reflect the chosen path.
        let db_row = FileChooserRow::new({
            let save_button = save_button.clone();
            move |_| save_button.set_sensitive(true)
        });

        content.append(&gtk::Label::new(Some("Welcome to FitnessTrax. The application has not yet been configured, so I will walk you through that. Let's start out by selecting your database.")));
        content.append(&db_row);

        save_button.connect_clicked({
            let db_row = db_row.clone();
            move |_| {
                if let Some(path) = db_row.path() {
                    on_save(path);
                }
            }
        });

        s.append(&title);
        s.append(&content);
        s.append(&save_button);

        s
    }
}
