/*
 * Copyright (c) 2012-2014 Kaarle Ritvanen
 * See LICENSE file for license details
 */

define(
    ["aconf/dom", "aconf/widget/abstract/fields", "jquery", "underscore"],
    function(dom, Base, $, _) {
	return Base.extend({
	    createEl: function() {
		this.prevAction = null;
		this.prevWidget = null;
		return $("<tr>").html($("<td>").addClass("placeholder"));
	    },

	    setStatus: function(status) {
		this.wrapped.trigger("statusChanged", [status]);
	    },

	    showHeading: false,

	    addActionButton: function(label, action) {
		(this.prevAction ?
		 this.prevAction : this.el.find(".placeholder")).after(
		     $("<td>").html(dom.href().text(label).click(action))
		 );
	    },

	    createModelWidgets: function(fields, editable) {
		Base.createModelWidgets.call(this, fields, editable);

		var self = this;
		var tdFields = [];
		_.each(fields, function(field) {
		    if (field.name in self.widgets &&
			self.widgets[field.name].is("td"))
			tdFields.push(field);
		});
		if (_.size(tdFields) == _.size(fields)) return;

		Base.createModelWidgets.call(this, tdFields, false);
		this.wrapped.trigger("hasDetails", [this.path]);
	    },

	    createWidget: function(name, meta, editable, removable) {
		if (!(meta.detail || meta.condition))
		    return Base.createWidget.call(
			this, name, meta, editable, removable
		    );
	    },

	    appendWidget: function(el, label) {
		if (this.prevWidget) this.prevWidget.after(el);
		else {
		    var ph = this.el.find(".placeholder");
		    ph.after(el);
		    ph.remove();
		}
		this.prevWidget = el;
		return el;
	    }
	});
    }
);
