/*
 * Copyright (C) 2024 Tildeslash Ltd. All rights reserved.
 */

(function ($) {
    var ViewModel = new function () {
        var self = this;

        self.width = ko.observable($('#main').outerWidth());
        self.showCheckUpdatesWarning = ko.observable(false);
        self.checkUpdatesStatus = ko.observable();

        self.loading = ko.observable(true);

        // Methods
        self.setVersionCheckStatus = function (message) {
            self.checkUpdatesStatus(message);
            if (self.welcome.newMonitNotification() || self.welcome.newMMonitNotification()) {
                if (message == "") {
                    self.checkUpdatesStatus("Initializing");
                    self.showCheckUpdatesWarning(false);
                } else if (message != "Success") {
                    self.showCheckUpdatesWarning(true);
                } else {
                    self.showCheckUpdatesWarning(false);
                }
            } else {
                self.showCheckUpdatesWarning(false);
            }
        };

        self.error = function (jqXHR, textStatus, errorThrown) {
            self.setVersionCheckStatus(errorThrown);
        };

        self.welcome = {
            // Properties
            newMMonitNotification: ko.observable(false),
            newMonitNotification:  ko.observable(false),

            // New release notification
            toggleNewMMonitNotification: function (data, e) {
                e.stopPropagation();
                self.welcome.newMMonitNotification(! self.welcome.newMMonitNotification());
                if (! self.loading()) {
                    $.ajax({
                        url:  "../welcome/setmmonit",
                        data: {"newmmonitnotification": self.welcome.newMMonitNotification()},
                        type: 'POST',
                        timeout: 30000,
                        error: self.error,
                        success: function (data, textStatus, jqXHR) {
                            var response = $.parseJSON(data);
                            self.setVersionCheckStatus(response.description);
                        }
                    });
                }
            },
            toggleNewMonitNotification: function (data, e) {
                e.stopPropagation();
                self.welcome.newMonitNotification(! self.welcome.newMonitNotification());
                if (! self.loading()) {
                    $.ajax({
                        url:  "../welcome/setmonit",
                        data: {"newmonitnotification": self.welcome.newMonitNotification()},
                        type: 'POST',
                        timeout: 30000,
                        error: self.error,
                        success: function (data, textStatus, jqXHR) {
                            var response = $.parseJSON(data);
                            self.setVersionCheckStatus(response.description);
                        }
                    });
                }
            }
        };

        // Warning methods
        self.displayCheckUpdatesWarning = function (data, e) {
            e.stopPropagation();
            $(e.currentTarget).tooltip('show');
        };
        self.hideCheckUpdatesWarning = function (data, e) {
            e.stopPropagation();
            $(e.currentTarget).tooltip('hide');
        };

        $.ajax({
            url: "../../admin/settings/get",
            type: 'GET',
            dataType: 'json',
            timeout: 30000,
            error: self.error,
            success: function (data) {
                self.welcome.newMonitNotification(data.versioncheck.monit);
                self.welcome.newMMonitNotification(data.versioncheck.mmonit);
                self.setVersionCheckStatus(data.versioncheck.status);
                self.loading(false);
            }
        });

    } // ViewModel

    // Activate knockout.js
    ko.applyBindings(ViewModel);

    // RESPONSIVE settings
    $(window).on('resize orientationchange', function (e) {
        ViewModel.width($('#main').outerWidth());
    });
})(jQuery);
