
var global_settings = {};
var global_done = {};
var global_args;
var global_opts;

var mashmaker_itemsbyprop = {
	
	// creates a standard settings panel, and associated management functions
	// adds all the stuff straight to the body
	settings: function(id,opts){
        for(var i in opts.slots){
            var prop = opts.slots[i];
            if(typeof prop == "string"){
                opts.slots[i] = {name:prop,url:true,text:true};
            }
        }

        var odd = opts.odd;
        window.mashmaker_widget_settings = function(args){
			global_settings = args.settings;
            if(!global_settings) global_settings = {};
            updateUi();
		};
        window.mashmaker_widget_init = window.mashmaker_widget_settings;
        window.refreshSettings  = function(){
            updateData();
            updateUi();
        }
        window.updateUi = function(){
            var i;
            if(opts.path) mashmaker_settings.setPathSelect("path","",global_settings.path,{subprops:true});
            for(i in opts.slots){
                var prop = opts.slots[i];
                mashmaker_settings.setPathSelect(prop.name,global_settings.path,global_settings[prop.name],prop);
            }
            if(opts.request) document.getElementById("request").value = global_settings.request;
            if(opts.updateUi) opts.updateUi();
            mashmaker.setHeight(document.body.offsetHeight);
        }
        window.updateData = function(){
            if(opts.path) global_settings.path = document.getElementById("path").value;
            for(var i in opts.slots){
                var prop = opts.slots[i];
                global_settings[prop.name] = document.getElementById(prop.name).value;
            }
            if(opts.request) global_settings.request = document.getElementById("request").value;
            if(opts.updateData) opts.updateData();
        }
        window.refresh = function(){
            updateData();
            mashmaker.setSettings(global_settings);
        }
        var table = document.getElementById(id);

        if(opts.path){
            var pathrow = this.makeRow("Target items:",odd,this.makeSelect("path"));
            table.appendChild(pathrow);
        }else{
            odd = !odd;
        }
        for(var i in opts.slots){
            var prop = opts.slots[i];
            odd = !odd;
            var proprow = this.makeRow(prop.name,odd,this.makeSelect(prop.name));
            table.appendChild(proprow);
        }

        if(opts.request){
            odd = !odd;
            var reqsel = this.makeSelect("request");
            reqsel.appendChild(mashmaker_settings.makeOption("request individually","true"));
            reqsel.appendChild(mashmaker_settings.makeOption("all at once","false"));
            var request = this.makeRow("Request behaviour:",odd,reqsel);
            table.appendChild(request);
        }
        var button = document.createElement("button");
        button.textContent = "refresh";
        button.addEventListener("click",function(){
            refresh();
        },false);
        var refreshrow = this.makeRow("",!odd,button);
        table.appendChild(refreshrow);
    },

    widget: function(opts){
        var me = this;
        global_opts = opts;
        window.mashmaker_widget_init = function(args){
            global_args = args;
            global_settings = args.settings;
            if(!global_settings){
                if(opts.guess){
                    opts.guess();
                }else{
                    me.guess_settings();
//                    global_opts.addRemoveItems([],[]);
                }
                return;
            }
            if(global_settings.path){
                mashmaker.getData(0,global_settings.path, [], false, function(nodes){
                    me.process_nodes(nodes,me.notify_nodes);
                })
            }else{
                me.process_nodes([{id:0}],me.notify_nodes);
            }
        };

        window.mashmaker_widget_settings = function(args){
            mashmaker.removeAll();
            mashmaker_widget_init(args);
        };

        // TODO: make these efficient
        window.mashmaker_widget_addProp = function(args){
            if(global_settings.path){
                mashmaker.getData(0,global_settings.path, [], false, function(nodes){
                    me.process_nodes(nodes,me.notify_nodes);
                })
            }else{
                me.process_nodes([{id:0}],me.notify_nodes);
            }            
        };

        window.mashmaker_widget_removeProp = window.mashmaker_widget_addProp;

    },

    guess_slots: function(path){
        var done = 0;
        for(var i in global_opts.slots){
               mashmaker_settings.guessPropPath(path,[global_opts.slots[i]],function(prop){
                   global_settings[global_opts.slots[i]] = prop;
                   done++;
                   if(done == global_opts.slots.length){
                       global_args.settings = global_settings;
                       mashmaker.setSettings(global_settings);
                       mashmaker_widget_settings(global_args);
                   }
               });
           }
    },

    guess_settings: function(){
        var me = this;
        global_settings = {};
        if(global_opts.path){
            mashmaker_settings.guessItemPath("",global_opts.slots,function(path){
                global_settings.path = path;
                me.guess_slots(path);
            });
        }else{
            me.guess_slots("");
        }
    },

    // cheap ugly way. We just run process_nodes again to find out what has added
    notify_nodes: function(items){
        // added
        var nowdone = {};
        var items_added = [];
        var items_removed = [];
        for(var i in items){
            if(!items[i].slots) continue;
            nowdone[items[i].id] = true;                            
            if(!global_done[items[i].id]){
                items_added.push(items[i]);
            }
        }
        for(var i in global_done){
            if(!nowdone[i]){
                items_removed.push(i);
            }
        }
        global_done = nowdone;
        global_opts.addRemoveItems(items_added,items_removed);
    },

    process_nodes: function(nodes,callback){
        var items = [];
        var len = nodes.length;
        for(var i in nodes){
            this.process_node(nodes[i].id,function(item){
                items.push(item);
                if(items.length == len){
                    callback(items);
                }
            });
        }
    },

    // TODO: make this efficient
    process_node: function(id,callback){
        var map = {};
        var cnt = 0;
        for(var i in global_opts.slots){
            var prop = global_opts.slots[i];
            mashmaker.getData(id,global_settings[prop],[],false,function(nodes){
                if(nodes && nodes[0] && nodes[0].text){
                    map[prop] = [];
                    for(var j in nodes){
                        if(nodes[j] && nodes[j].text){
                            map[prop].push(nodes[j].text);
                        }
                    }
                }else{
                    map = null;
                }
                cnt++;
                if(cnt == global_opts.slots.length){
                    callback({id:id,slots:map});
                }
            });
        }
    },

    makeSelect: function(name){
        var select = document.createElement("select");
        select.className = "mm-select";
        select.setAttribute("id",name);
        select.addEventListener("change",function(){
            refreshSettings();
        },false);
        return select;
    },

    makeRow: function(nametxt,odd,contents){
        var row = document.createElement("tr");
        var name = document.createElement("td");
        var val = document.createElement("td");
        if(odd){
            row.className = "mm-oddrow";
        }else{
            row.className = "mm-evenrow";
        }
        name.className = "mm-name";
        name.textContent = nametxt;
        val.className= "mm-val";
        row.appendChild(name);
        row.appendChild(val);
        val.appendChild(contents);
        return row;
    }
}

var mashmaker_patterns = {
    itemsByProp: mashmaker_itemsbyprop    
};
