/*
 * This file is part of Nuts Framework.
 * Copyright (C) 2009 Nuts Develop Team.
 *
 * Nuts Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License any later version.
 *
 * Nuts Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Nuts Framework. If not, see <http://www.gnu.org/licenses/>.
 */
package nuts.exts.struts2.views.java.simple;

import java.io.IOException;
import java.util.Iterator;

import nuts.core.lang.StringUtils;
import nuts.exts.struts2.views.java.AbstractTemplateRenderer;
import nuts.exts.struts2.views.java.Attributes;

import org.apache.struts2.components.template.TemplateRenderingContext;
import org.apache.struts2.util.ContainUtil;
import org.apache.struts2.util.MakeIterator;

public class NutsCheckboxListRenderer extends AbstractTemplateRenderer {
	public NutsCheckboxListRenderer(TemplateRenderingContext context) {
		super(context);
	}

	public void render() throws IOException {
		String id = (String)params.get("id");

		write("<div");
		if (StringUtils.isNotEmpty(id)) {
			write(" id=\"" + html(id) + "\"");
		}
		write(" class=\"n-checkboxlist\">");
		
		String name = (String)params.get("name");
		Object disabled = params.get("disabled");
		Object value = params.get("nameValue");

		Object listObj = params.get("list");
		if (listObj != null) {
			String listKey = (String)params.get("listKey");
			String listValue = (String)params.get("listValue");

			int itemCount = 0;
			Iterator itt = MakeIterator.convert(listObj);
			while (itt.hasNext()) {
				itemCount++;

				Object item = itt.next();
				stack.push(item);

				// key
				Object itemKey = findValue(listKey != null ? listKey : "top");
				String itemKeyStr = defs(itemKey == null ? null
						: itemKey.toString());
				// value
				Object itemValue = findValue(listValue != null ? listValue
						: "top");
				String itemValueStr = defs(itemValue == null ? null
						: itemValue.toString());

				boolean checked = ContainUtil.contains(value, itemKey);

				write("<span class=\"n-checkbox-item\">");
				Attributes ca = new Attributes();
				ca.add("type", "checkbox")
					.add("name", name)
					.add("id", id + "_" + itemCount)
					.addIfExists("value", itemKeyStr)
					.addIfTrue("checked", checked)
					.addIfTrue("disabled", disabled)
					.readonly(params)
					.title(params)
					.cssClass(params)
					.cssStyle(params)
					.commons(params)
					.events(params);

				xtag("input", ca);
				
				write("<label");
				write(" for=\"");
				write(html(id) + "_" + itemCount);
				write("\" class=\"n-checkbox-label\">");
				body(itemValueStr);
				write("</label>");
				write("</span>");

				stack.pop();
			}
			
			write("<div class=\"n-clearfix\"></div>");
		}
		else {
			write(" &nbsp;");
		}

        if (!Boolean.FALSE.equals(params.get("multiselectIntercept"))) {
			//hidden input
	        Attributes attrs = new Attributes();
	        attrs.add("type", "hidden")
	                .add("id", "__multiselect_" + id)
	                .add("name", "__multiselect_" + name)
	                .add("value", "")
	                .addIfTrue("disabled", disabled);
	        xtag("input", attrs);
        }
        
		write("</div>");
	}
}
