/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2005 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or 1any later version.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jonasadmin.test.util;

import org.xml.sax.SAXException;

import com.meterware.httpunit.TableCell;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;

/**
 * Utils
 * @author kemlerp
 *
 */
public class JonasAdminUtils {

    /**
     * A class which represents the row and the column of a cell
     * @author kemlerp
     *
     */
    public class CoordCell {

        /**
         * Row of the cell
         */
        private Integer row = null;

        /**
         * Column of the cell
         */
        private Integer column = null;

        /**
         * Constructor
         * @param row a positive integer
         * @param column a positive integer
         */
        public CoordCell (Integer row, Integer column) {
            this.row = row;
            this.column = column;
        }

        /**
         * Get column
         * @return column number
         */
        public Integer getColumn() {
            return column;
        }

        /**
         * Set column
         * @param column a positive integer
         */
        public void setColumn(Integer column) {
            this.column = column;
        }

        /**
         * Get row
         * @return row number
         */
        public Integer getRow() {
            return row;
        }

        /**
         * Set row
         * @param row a positive integer
         */
        public void setRow(Integer row) {
            this.row = row;
        }
    }

    /**
     * Get the last row of table where text is found in the first column
     * @param text title of the row
     * @param table the table which contains text
     * @return -1 if the text was not found in the first column else an integer
     */
    public Integer getRow(String text, WebTable table) {
        Integer row = new Integer(-1);
        TableCell cell;
        for (int i = 0; i < table.getRowCount(); i++) {
            cell = table.getTableCell(i, 0);
            if (cell.getText().indexOf(text) != -1) {
                row = new Integer(i);
            }
        }
        return row;
    }

    /**
     * Get the first row of table where text is found in the given column
     * @param text title of the row
     * @param table the table which contains text
     * @param column the column
     * @return -1 if the text was not found in the first column else an integer
     */
    public Integer getFirstRow(String text, WebTable table, int column) {
        boolean found = false;
        Integer row = new Integer(-1);
        TableCell cell;
        int i = 0;
        while (i < table.getRowCount() && !found) {
            cell = table.getTableCell(i, column);
            if (cell != null && cell.getText().indexOf(text) != -1) {
                row = new Integer(i);
                found = true;
            }
            i++;
        }
        return row;
    }

    /**
     * Get the last row of table where text is found in the given column
     * @param text title of the row
     * @param table the table which contains text
     * @param column the column
     * @return -1 if the text was not found in the first column else an integer
     */
    public Integer getRow(String text, WebTable table, int column) {
        Integer row = new Integer(-1);
        TableCell cell;
        for (int i = 0; i < table.getRowCount(); i++) {
            cell = table.getTableCell(i, column);
            if (cell.getText().indexOf(text) != -1) {
                row = new Integer(i);
            }
        }
        return row;
    }

    /**
     * Get the last column of table where text is found in the first row
     * @param text title of the column
     * @param table the table which contains text
     * @return -1 if the text was not found in the first row else an integer
     */
    public Integer getColumn(String text, WebTable table) {
        Integer column = new Integer(-1);
        TableCell cell;
        for (int i = 0; i < table.getColumnCount(); i++) {
            cell = table.getTableCell(0, i);
            if (cell.getText().indexOf(text) != -1) {
                column = new Integer(i);
            }
        }
        return column;
    }

    /**
     * Get the row and the column of the cell which contains selected item
     * @param table tree Table
     * @return Null if no item is selected, else the row and the column
     */
    public CoordCell getSelectedItemRow(WebTable table) {
        Integer row = null;
        Integer column = null;
        TableCell cell = null;
        CoordCell coord = null;
        String attribut;
        boolean found = false;
        int i = 0;
        int j = 0;

        while (i < table.getRowCount() && !found) {
            j = 0;
            while (j < table.getColumnCount() && !found) {
                cell = table.getTableCell(i, j);
                if (cell.getElementsWithAttribute("class", "tree-control-selected").length == 1) {
                    found = true;
                    row = new Integer(i);
                    column = new Integer(j);
                    coord = new CoordCell(row, column);
                }
                j++;
            }
            i++;
        }
        return coord;
    }

    /**
     * Get table num
     * @param contentFrame the content frame
     * @param num integer between 0 and number of tables in the cell
     * @return table
     * @throws SAXException if a table or a cell doesn't match.
     */
    public WebTable getTable(WebResponse contentFrame, int num) throws SAXException {
        WebTable table = contentFrame.getTables()[1];
        TableCell cell = table.getTableCell(1, 0);
        table = cell.getTables()[0];
        cell = table.getTableCell(0, 0);
        table = cell.getTables()[0];
        cell = table.getTableCell(0, 0);
        // Get the (num+1)th table
        table = cell.getTables()[num];
        return table;
    }

    /**
     * Get table of tabs
     * @param contentFrame the content frame
     * @return table of tabs
     * @throws SAXException if a table or a cell doesn't match.
     */
    public WebTable getTabTable(WebResponse contentFrame) throws SAXException {
        WebTable table = contentFrame.getTables()[1];
        TableCell cell = table.getTableCell(0, 0);
        table = cell.getTables()[0];
        return table;
    }
}
