/*
 * LINGUIST.LLMAP.Preferences
 * Written by Joshua M. Thompson <joshua@linguistlist.org>
 *
 * A singleton class for client UI preferences management via cookies
 */

LINGUIST.LLMAP.Preferences = {
    cookieName: 'llmap_mapui',
    prefs: {},

    init: function() {
    },

    create: function(config) {
        var type = config.xtype;
        var pref;

        if (type == 'yesno') {
            pref = new LINGUIST.LLMAP.YesNoPref(config);
        }
        else if (type == 'showtip') {
            pref = new LINGUIST.LLMAP.ShowTipPref(config);
        }
        else {
            return null; // FIXME: probably error out here, or something.
        }

        return this.prefs[pref.name] = pref;
    },

    get: function(name, callback, scope, data) {
        return this.prefs[name].get(this, callback, scope, data);
    },

    load: function() {
        var date     = new Date();
        var provider = new Ext.state.CookieProvider({
            expires: new Date(date.getTime() + (1000 * 60 * 60 * 24 * 365 * 10)) // 10 years, in milliseconds
        });
        var data     = provider.get(this.cookieName, {});

        for (var name in data) {
            var pref = this.prefs[name];

            if (name) {
                pref.value = data[name];
            }
        }

        this.provider = provider;
    },

    save: function() {
        var data = {};

        for (var name in this.prefs) {
            data[name] = this.prefs[name].value;
        }

        this.provider.set(this.cookieName, data);
    }
};

LINGUIST.LLMAP.Pref = function(config) {
    Ext.applyIf(config, {
        name: 'preference',
        title: 'Preference',
        text: 'What is the airspeed velocity of an unladen swallow?',
        value: null
    });

    Ext.apply(this, config);
}

LINGUIST.LLMAP.ShowTipPref = Ext.extend(LINGUIST.LLMAP.Pref, {
    title: 'LL-MAP Tip',
    get: function(manager, callback, scope, data) {
        if (this.value == null) {
            Ext.Msg.show({
                title: this.title,
                msg: this.text,
                icon: Ext.MessageBox.INFO,
                buttons: {
                    ok: true,
                    cancel: 'Don\'t show this again'
                },
                scope: this,
                fn: function(btn) {
                    if (btn != 'ok') {
                        this.value = true;

                        manager.save();
                    }

                    callback.call(scope, this, this.value, data);
                }
            });
        }
        else {
            // preference is already set

            callback.call(scope, this, this.value, data);
        }
    }
});

LINGUIST.LLMAP.YesNoPref = Ext.extend(LINGUIST.LLMAP.Pref, {
    get: function(manager, callback, scope, data) {
        if (this.value == null) {
            Ext.Msg.show({
                title: this.title,
                msg: this.text,
                icon: Ext.MessageBox.QUESTION,
                buttons: {
                    yes: true,
                    no: true,
                    cancel: 'Don\'t ask again'
                },
                scope: this,
                fn: function(btn) {
                    var value;

                    if (btn == 'yes') {
                        value = true;
                    }
                    else if (btn == 'no') {
                        value = false;
                    }
                    else {
                        this.value = value = false;

                        manager.save();
                    }

                    callback.call(scope, this, value, data);
                }
            });
        }
        else {
            // preference is already set

            callback.call(scope, this, this.value, data);
        }
    }
});
