/**
* @package Zim.Command
* @desc Application-wide command objects
*
* Copyright (c) 2007 Anil Bawa-Cavia
* Published under an MIT License. See http://www.zimoutliner.com/license.html
*/

Zim.Command = function() {};
Zim.Command.prototype= {
    execute:function(){
        Zim.Menu.Clicked = true;
        this._execute();
    }
};

/**
* Zim menu commands
*/
Zim.Command.Zim = {};
/**
* @desc about screen
*/
Zim.Command.Zim.About = function(){};
Zim.Command.Zim.About.prototype = {
    _execute:function() {
        window.location='/about/';
    }
};
Object.extend(Zim.Command.Zim.About.prototype,Zim.Command.prototype);

/**
* @desc preferences screen
*/
Zim.Command.Zim.Preferences = function(){};
Zim.Command.Zim.Preferences.prototype = {
    _execute:function() {
        window.location='/user/prefs';
    }
};
Object.extend(Zim.Command.Zim.Preferences.prototype,Zim.Command.prototype);

/**
* File commands
*/

Zim.Command.File = {};
Zim.Command.File.Create = function(){};
Zim.Command.File.Create.prototype = {
    _execute:function(){
        window.location='/list/create';
    }
};

Object.extend(Zim.Command.File.Create.prototype,Zim.Command.prototype);

/**
* @package File
* @desc Save a file
*/


Zim.Command.File.Save = function(){};
Zim.Command.File.Save.prototype = {
    _execute:function(){
        Zim.Outline.Modified = true;
        if(Zim.Outline.Current.isTodo()) {
            Zim.Outline.Save(false,true);
        } else {
            Zim.Outline.Save();
        }
    }
    
};
Object.extend(Zim.Command.File.Save.prototype,Zim.Command.prototype);

/**
* @desc Export a file
*/

Zim.Command.File.Export = function(id){this.id = id;};
Zim.Command.File.Export.prototype = {
    id:null,
    _execute:function(){
    }
};

Object.extend(Zim.Command.File.Export.prototype,Zim.Command.prototype);


/**
* @desc OPML Export
*/
Zim.Command.File.Export.OPML = function(id){this.id = id;};
Zim.Command.File.Export.OPML.prototype = {
    id:null,
    _execute:function(){
        if(!this.id) {
            this.id = Zim.Outline.Current.id.substring(4);
        }
        //console.log('/list/export/' + this.id);
        window.location='/list/export/' + this.id + '?type=opml';
    }
};

Object.extend(Zim.Command.File.Export.OPML.prototype,Zim.Command.prototype);

/**
* @desc Plain Text Export
*/
Zim.Command.File.Export.Text = function(id){this.id = id;};
Zim.Command.File.Export.Text.prototype = {
    id:null,
    _execute:function(){
        if(!this.id) {
            this.id = Zim.Outline.Current.id.substring(4);
        }
        //console.log('/list/export/' + this.id + '?type=text');
        window.location='/list/export/' + this.id + '?type=text';
    }
};

Object.extend(Zim.Command.File.Export.Text.prototype,Zim.Command.prototype);

/**
* @desc HTML Export
*/
Zim.Command.File.Export.HTML = function(id){this.id = id;};
Zim.Command.File.Export.HTML.prototype = {
    id:null,
    _execute:function(){
        if(!this.id) {
            this.id = Zim.Outline.Current.id.substring(4);
        }
        //console.log('/list/exporttext' + this.id);
        window.location='/list/export/' + this.id + '?type=HTML';
    }
};

Object.extend(Zim.Command.File.Export.HTML.prototype,Zim.Command.prototype);

/**
* @desc Delete a file
*/
Zim.Command.File.Delete = function(){};
Zim.Command.File.Delete.prototype = {
    _execute:function(){
        //console.log(Zim.Menu.selectedLists.length);
        var msg = Zim.Menu.selectedLists.length > 1 ? "Are you sure you want to delete the selected lists?" : "Are you sure you want to delete the list?";
        var answer = confirm( msg + " You can't undo this");
        //console.log(answer);
        if(answer) {
            var url = '/list/delete/';
            new Ajax.Request(url, {
                method: 'post',
                parameters: 'id='+Zim.Menu.selectedLists.toString(),
                onSuccess: this.onSuccess
            });
        }
    },
    
    onSuccess:function(response) {
        window.location='/list/view?msg=You deleted '+response.responseText+ ' list(s)';
    }
};
Object.extend(Zim.Command.File.Delete.prototype,Zim.Command.prototype);

/**
* View commands
*/
Zim.Command.View = {};
Zim.Command.View.Bundles = function() {};
Zim.Command.View.Bundles.prototype = {
    _execute:function() {
        window.location ='/list/view';
    }
};
Object.extend(Zim.Command.View.Bundles.prototype,Zim.Command.prototype);

/**
* @desc Go to projects screen
*/

Zim.Command.View.Projects = function() {};
Zim.Command.View.Projects.prototype = {
    _execute:function() {
    }
};
Object.extend(Zim.Command.View.Projects.prototype,Zim.Command.prototype);

/**
* @desc Go to changeblog
*/

Zim.Command.View.Changeblog = function() {};
Zim.Command.View.Changeblog.prototype = {
    _execute:function() {
        window.location = '/blog/';
    }
};
Object.extend(Zim.Command.View.Changeblog.prototype,Zim.Command.prototype);

/**
* @desc Go to todo list
*/
Zim.Command.View.TodoList = function() {};
Zim.Command.View.TodoList.prototype = {
    _execute:function() {
        window.location = '/list/todo/';
    }
};
Object.extend(Zim.Command.View.TodoList.prototype,Zim.Command.prototype);

/**
* Account commands
*/
Zim.Command.Account = {};

/**
* @desc Logout
*/
Zim.Command.Account.Logout = function(){};
Zim.Command.Account.Logout.prototype = {
    _execute:function() {
        window.location='/user/logout';
    }
};
Object.extend(Zim.Command.Account.Logout.prototype,Zim.Command.prototype);

/**
* @desc Login
*/

Zim.Command.Account.Login = function(){};
Zim.Command.Account.Login.prototype = {
    _execute:function() {
        window.location='/user/login';
    }
};
Object.extend(Zim.Command.Account.Login.prototype,Zim.Command.prototype);

/**
* @desc Sign up screen
*/

Zim.Command.Account.SignUp = function(){};
Zim.Command.Account.SignUp.prototype = {
    _execute:function() {
        window.location='/user/join';
    }
};
Object.extend(Zim.Command.Account.SignUp.prototype,Zim.Command.prototype);

/**
* Bundle Commands
*/
Zim.Command.Bundle = {};

/**
* @desc Add a bundle
*/
Zim.Command.Bundle.Add = function(){};
Zim.Command.Bundle.Add.prototype = {
    _execute:function(m) {
        var msg = m ? m : 'Give your bundle a name';
        var name = prompt(msg);
        try {
            if(name.length < 1) {
                this._execute('Please provide a name for your bundle');
            } else {
                this.send(name);
            }
        } catch (ex){};
    },
    
    send:function(name) {
        var url = '/bundle/create';
        new Ajax.Request(url, {
            method: 'post',
            parameters: 'name='+encodeURIComponent(name),
            onSuccess: this.onSuccess
        });
    },
    
    onSuccess:function(response){
        if(parseInt(response.responseText) > 0) {
            window.location='/list/view?msg=You created a new bundle';
        } else {
            Zim.Menu.setStatus('There was an error creating your bundle','error');
        }
    }
};
Object.extend(Zim.Command.Bundle.Add.prototype,Zim.Command.prototype);

/**
* @desc Save bundles and outline assocs
*/
Zim.Command.Bundle.Save = function(id){this.id = id;this.execute();};
Zim.Command.Bundle.Save.prototype = {
    id:null,
    _execute:function() {
        var bundleJSON = {};
        Bundles.each(function(bundleId) {
            bundleJSON[bundleId] = {Outlines:[]};
            $A($('bundleList' + bundleId).childNodes).each (function(oOutline) {
                if(oOutline.id!=undefined) {
                    bundleJSON[bundleId].Outlines.push(oOutline.id.substring(4));
                }
            });
        });
        this.send(bundleJSON);
    },
    
    send:function(bundleJSON) {
         var url = '/bundle/saveOutlineAssoc';
        Zim.Menu.setStatus('Autosaving...','info');
        new Ajax.Request(url, {
            method: 'post',
            parameters: 'oBundleOutlineAssoc='+encodeURIComponent(Object.toJSON(bundleJSON)),
            onSuccess: this.onSuccess
        });
    },
    
    onSuccess:function(response) {
        Zim.Menu.setStatus('Changes saved','info');
        //$('notice').update(response.responseText);
    }
};
Object.extend(Zim.Command.Bundle.Save.prototype,Zim.Command.prototype);

/**
* @desc Update bundle title
*/
Zim.Command.Bundle.Title = {};
Zim.Command.Bundle.Title.Save = function(id,title){this.title=title;this.id=id;this.execute()};
Zim.Command.Bundle.Title.Save.prototype={
    title:null,
    id:null,
    _execute:function(){
        var url = '/bundle/editTitle/'+this.id;
        Zim.Menu.setStatus('Autosaving...','info');
        new Ajax.Request(url, {
            method: 'post',
            parameters: 'title='+encodeURIComponent(this.title),
            onSuccess: this.onSuccess
        });
    },
    onSuccess:function(response) {
        Zim.Menu.setStatus('Changes saved','info');
        //$('notice').update(response.responseText);
    }
};
Object.extend(Zim.Command.Bundle.Title.Save.prototype,Zim.Command.prototype);

Zim.Command.Bundle.Delete = function(id){ this.id=id;};
Zim.Command.Bundle.Delete.prototype={
    id:null,
    _execute:function(){
        var answer = confirm("Are you sure you want to delete this bundle? Don't worry, your outlines will not be deleted.");
        if(answer) {
            var url = '/bundle/delete/';
            new Ajax.Request(url, {
                method: 'post',
                parameters: 'id='+this.id,
                onSuccess: this.onSuccess
            });
        }
    },
    onSuccess:function(){
        window.location='/list/view?msg=You deleted a bundle';
    }
}
Object.extend(Zim.Command.Bundle.Delete.prototype,Zim.Command.prototype);
