package usda.weru.weps.reports.query;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author mark
 */

// Unused
// Added for debugging.
// The default H2 SimpleResultSet works fine
//
public class WepsResultSetMetaData implements ResultSetMetaData {

    
    int columnCnt;
    
    static public String colDataName = "colName";
    static public String colDataType = "colType";
    static public String colDataTypeName = "colTypeName";
    static public String colDataPrecision = "colPrec";
    static public String colDataScale = "colScale";
    static public String colDataLabel = "colLabel";
    static public String colDataClass = "colClass";
    @SuppressWarnings("serial")
    private class colData extends HashMap <String, Object> {
    }
    
    ArrayList<colData> colDataAll;
    
    public WepsResultSetMetaData () {
        columnCnt = 0;
        colDataAll = new ArrayList<>();
    }
    
    public void addColumn(String name, int sqlType, int precision, int scale) {
        colData data = new colData();
        data.put(colDataName, name);
        data.put(colDataType, sqlType);
        data.put(colDataPrecision, precision);
        data.put(colDataScale, scale);
        data = setDefClassVal (data, sqlType);
        colDataAll.add(data);
        columnCnt++;
    }
    
    protected colData setDefClassVal (colData data, int type) {
        Class<?> result = String.class;
        switch (type) {
            case Types.CHAR:
            case Types.VARCHAR:
            case Types.LONGVARCHAR:
                result = String.class;
                break;

            case Types.NUMERIC:
            case Types.DECIMAL:
                result = java.math.BigDecimal.class;
                break;

            case Types.INTEGER:
            case Types.SMALLINT:
            case Types.BIGINT:
                result = Integer.class;
                break;

            case Types.FLOAT:
                result = Float.class;
                break;

            case Types.DOUBLE:
                result = Double.class;
                break;
                
            case Types.BIT:
                result = Boolean.class;
                break;
                
            default:
                result = Object.class;
        }
        data.put(colDataClass, result.getCanonicalName());
        return data;
    }
    
    public void addColumn(String name, int sqlType, String sqlTypeName, int precision, int scale) {
        addColumn(name, sqlType, precision, scale);
        colDataAll.get(columnCnt-1).put(colDataTypeName, sqlTypeName);
    }
    
//    public void setColumnCnt (int cnt) {
//        columnCnt = cnt;
//    }
    
    public void setColumnClassName (int col, String classname) {
        colDataAll.get(col).put(colDataClass, classname);
    }

    @Override
    public int getColumnCount() throws SQLException {
        return columnCnt;
    }

    @Override
    public boolean isAutoIncrement(int column) throws SQLException {
        return false;
    }

    @Override
    public boolean isCaseSensitive(int column) throws SQLException {
        return false;
    }

    @Override
    public boolean isSearchable(int column) throws SQLException {
        return false;
    }

    @Override
    public boolean isCurrency(int column) throws SQLException {
        return false;
    }

    @Override
    public int isNullable(int column) throws SQLException {
        return columnNoNulls;
    }

    @Override
    public boolean isSigned(int column) throws SQLException {
        return false;
    }

    @Override
    public int getColumnDisplaySize(int column) throws SQLException {
        return 0;
    }

    @Override
    public String getColumnLabel(int column) throws SQLException {
        String ret = (String)colDataAll.get(column-1).get(colDataName);
        return ret;
    }

    @Override
    public String getColumnName(int column) throws SQLException {
        String ret = (String)colDataAll.get(column-1).get(colDataName);
        return ret;
    }

    @Override
    public String getSchemaName(int column) throws SQLException {
        return "";
    }

    @Override
    public int getPrecision(int column) throws SQLException {
        int i = (Integer)(colDataAll.get(column-1).get(colDataPrecision));
        return i;
    }

    @Override
    public int getScale(int column) throws SQLException {
        int i = (Integer)(colDataAll.get(column-1).get(colDataScale));
        return i;
    }

    @Override
    public String getTableName(int column) throws SQLException {
        return "";
    }

    @Override
    public String getCatalogName(int column) throws SQLException {
        return "";
    }

    @Override
    public int getColumnType(int column) throws SQLException {
        int i = (Integer)(colDataAll.get(column-1).get(colDataType));
        return i;
    }

    @Override
    public String getColumnTypeName(int column) throws SQLException {
        String ret = (String)colDataAll.get(column-1).get(colDataTypeName);
        return ret;
    }

    @Override
    public boolean isReadOnly(int column) throws SQLException {
        return true;
    }

    @Override
    public boolean isWritable(int column) throws SQLException {
        return false;
    }

    @Override
    public boolean isDefinitelyWritable(int column) throws SQLException {
        return false;
    }

    @Override
    public String getColumnClassName(int column) throws SQLException {
        String ret = (String)colDataAll.get(column-1).get(colDataClass);
        return ret;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
