// client side
// preferences.js
// - handle ui/data for input and changes of user preferences
// - keep user preferences in sync with the server

// TODO

var Pref = {};

defines['preferences-url-base'] = `/api/${defines.API_VERSION}/preferences`;

Pref.NOTIFICATION_NODE_SELECTOR = ".modal#fc-preferences .fc-preferences-notifications";
Pref.HOTWORDS_NODE_SELECTOR = ".modal#fc-preferences .fc-preferences-hotwords";

/**
 * @return {boolean}
 */
Pref.isNotificationsEnabled = function() {
    return false;
};

/**
 * Returns a set of the user's hotwords
 * @return {Set<string>}
 */
Pref.hotwords = function() {
    return new Set();
};

Pref.updateLocal = function(preferences) {
    var notificationCheckBox = document.querySelector(Pref.NOTIFICATION_NODE_SELECTOR);
    var hotwordsBox = document.querySelector(Pref.HOTWORDS_NODE_SELECTOR);
    notificationCheckBox.checked = preferences.notifications;
    hotwordsBox.value = preferences.hotwords.join(', ');
};

Pref.fetchFromServer = function(fn) {
    $.get(defines['preferences-url-base'], fn);
};

Pref.saveToServer = function(preferences, opt_fn) {
    var defaultPreferencesParams = { notifications: false, hotwords: [] };
    preferences = toolbelt.normalize(preferences, defaultPreferencesParams);
    $.post(defines['preferences-url-base'], {preferences: preferences}, opt_fn || function(res) {
        console.log('successfully updated preferences', res);
    });
};

Pref.handleCancel = function() {
    Pref.fetchFromServer(function(res) {
        Pref.updateLocal(res);
    });
};

Pref.handleSave = function() {
    var notificationCheckBox = document.querySelector(Pref.NOTIFICATION_NODE_SELECTOR);
    var hotwordsBox = document.querySelector(Pref.HOTWORDS_NODE_SELECTOR);
    var preferences = {
        notifications: notificationCheckBox.checked,
        hotwords: hotwordsBox.value.split(',').map(s => s.trim())
    };

    Pref.saveToServer(preferences, function() {
        Pref.updateLocal(preferences);
    });
};

window.addEventListener('load', function() {
    var saveButton = document.querySelector('.modal#fc-preferences .fc-preferences-save');
    var cancelButton = document.querySelector('.modal#fc-preferences .fc-preferences-cancel');

    saveButton.addEventListener('click', function() {
        Pref.handleSave();
    });

    cancelButton.addEventListener('click', function() {
        Pref.handleCancel();
    });
});

@@ -493,147 +493,380 @@
     
-return false;%0A%7D;%0A%0A/**%0A * Returns a set of the user's hotwords%0A * @return %7BSet%3Cstring%3E%7D%0A */%0APref.hotwords = function() %7B%0A    return new Set(
+var notificationCheckBox = document.querySelector(Pref.NOTIFICATION_NODE_SELECTOR);%0A    return notificationCheckBox.checked;%0A%7D;%0A%0A/**%0A * Returns a set of the user's hotwords%0A * @return %7BSet%3Cstring%3E%7D%0A */%0APref.hotwords = function() %7B%0A    var hotwordsBox = document.querySelector(Pref.HOTWORDS_NODE_SELECTOR);%0A    return new Set(hotwordsBox.value.split(',').map(s =%3E s.trim())
 );%0A%7D

Update preferences frontend functions