/**
 * proxy setting plugin
 *
 * @author cho45
 * @author halt feits
 * @version 0.5
 */

const prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);

proxy_settings = [
    /**
     *
    function YourSettingName() {
        prefs.setIntPref("network.proxy.type", 1);
        prefs.setCharPref("network.proxy.http", 'localhost');
        prefs.setIntPref("network.proxy.http_port", 8080);
    },
    */

    function Disable() {
        prefs.setIntPref("network.proxy.type", 0);
    },

    function Office() {
        prefs.setIntPref("network.proxy.type", 1);
        prefs.setCharPref("network.proxy.http", 'localhost');
        prefs.setIntPref("network.proxy.http_port", 8080);
    }
];

vimperator.commands.add(new vimperator.Command(["proxy"],
        function (args)
        {
            var name = args;

            if (!name) {
                vimperator.echo("Usage: proxy {setting name}");
            }

            for (var i = 0; i < proxy_settings.length; i++) {
                if (proxy_settings[i].name.toLowerCase() == name.toLowerCase()) {

                    //delete setting
                    ['http', 'ssl', 'ftp', 'gopher'].forEach(
                        function (p) {
                            prefs.setCharPref("network.proxy." + p, '');
                            prefs.setIntPref("network.proxy." + p + "_port", 0);
                        }
                    );
                    proxy_settings[i]();
                    vimperator.echo("set config:" + name);
                    break;
                }
            }
        },
        {
            usage: ["proxy {setting name}"],
            shortHelp: "Change proxy setting",
            completer: function (filter) {
                var completions = [];

                proxy_settings.forEach(function ($_) {
                    if ($_.name.toLowerCase().indexOff(filter.toLowerCase()) == 0) {
                        completions.push([$_.name, $_.name]);
                    }
                });

                var completions = [ [Office,A], [Disable,a]];

                return vimperator.completion.filterUrlArray(completions, filter);
            }
        }
));
