/**
 * License Agreement.
 *
 * Rich Faces - Natural Ajax for Java Server Faces (JSF)
 *
 * Copyright (C) 2007 Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package org.richfaces.sandbox.samples;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.richfaces.model.Ordering;

/**
 * @author k
 * 
 */
public class Bean {

    private List model = new ArrayList();

    private List columns = new ArrayList();
    
    private Integer columnsCount = 3;
    
    private Integer rowCount = 3;
    
    private String columnsCountStr;
    
    private String rowCountStr;
    
    private String str;
    
    private boolean changed = false;
    
       
    public String xhtml() {
	
	return "xhtml";
    }
    
    public String jsp() {
	
	return "jsp";
    }
    
 public String action() {
	
	return null;
    }

    public Bean() {
	init();
    }
    
    private Integer getInteger (String str) {
	Integer i = new Integer(0);
	try {
	    i = Integer.parseInt(str);
	}catch (Exception e) {
	    // TODO: handle exception
	}
	return i;
    }
    
    private void init () {
	
	model = new ArrayList();
	columns = new ArrayList();
	
	if (columnsCountStr != null) {
	    columnsCount = getInteger(columnsCountStr);
	}
	if (rowCountStr != null) {
	    rowCount = getInteger(rowCountStr);
	}
	
	initColumns();
		
	for (int i = 0; i < rowCount; i++) {
//	    Map map = new HashMap();
//	    initMap(map,i);
	    model.add(initArray(i));
 	}
	
    }
    
    private void initColumns () {
	String header;
	String footer;
	Ordering ordering = Ordering.UNSORTED; 
	for (int i = 0; i < columnsCount; i++) {
	    header = "Header" + Integer.toString(i);
	    footer = "Footer" + Integer.toString(i);
	    Column column = new Column(header, footer, ordering);
	    //Facet facet = new Facet(header,footer);
	    columns.add(column);
  
	}
    }
    
    private Object [] initArray(int row) {
	Object [] arr = new Object [columns.size()];
	for (int i = 0; i < arr.length; i++) {
	    int r = ((Double)(Math.random() * 100.0)).intValue();
	    arr[i] = (r < 10) ? "0" + Integer.toString(r) : Integer.toString(r);  
	}
	return arr;
    }
    


    /**
     * @return the model
     */
    public List getModel() {
	if (changed) {
	    init();
	    changed = false;
	}
        return model;
    }

    /**
     * @param model the model to set
     */
    public void setModel(List model) {
        this.model = model;
    }

    /**
     * @return the columns
     */
    public List getColumns() {
	if (changed) {
	    init();
	    changed = false;
	}
        return columns;
    }

    /**
     * @param columns the columns to set
     */
    public void setColumns(List columns) {
        this.columns = columns;
    }

    /**
     * @return the columnsCountStr
     */
    public String getColumnsCountStr() {
        return columnsCount.toString();
    }

    /**
     * @param columnsCountStr the columnsCountStr to set
     */
    public void setColumnsCountStr(String columnsCountStr) {
	if (!columnsCountStr.equals(this.columnsCountStr)) {
	    changed = true;
	}
        this.columnsCountStr = columnsCountStr;
    }

    /**
     * @return the rowCountStr
     */
    public String getRowCountStr() {
        return rowCount.toString();
    }

    /**
     * @param rowCountStr the rowCountStr to set
     */
    public void setRowCountStr(String rowCountStr) {
	if (!rowCountStr.equals(this.rowCountStr)) {
	    changed = true;
	}
	this.rowCountStr = rowCountStr;
    }

    /**
     * @return the str
     */
    public String getStr() {
        return str;
    }

    /**
     * @param str the str to set
     */
    public void setStr(String str) {
        this.str = str;
    }

   

}