package com.appideas.base;

import com.sharefaith.thesharefaithapp.base.SFApplication;

//import android.util.Log;

/**
 * The APPideas Input Checks.
 *
 * @author		costmo
 * @version		20111213
 * @since		20111213
 */

public class AiICs
{
	/**
	 * Database instance
	 */
	AiDb mDb;

	/**
	 * Class constructor
	 *
	 * @since		20111213
	 */
	public AiICs()
	{
		this.mDb = SFApplication.getInstance().getDb();
	}

	/**
	 * Verify that the text input is not blank<p/>
	 *
	 * Return true if input passes the test, false otherwise
	 *
	 * @since		20111213
	 * @return		boolean
	 * @param		input			The input to check
	 */
	public boolean checkBlank( String input )
	{
		boolean returnValue = true;

		if( input == null || input.equals( "" ) )
		{
			returnValue = false;
		}

		return returnValue;
	}

	/**
	 * Verify that the input looks like a valid email address<p/>
	 *
	 * Return true if input passes the test, false otherwise
	 *
	 * @since		20111213
	 * @return		boolean
	 * @param		input			The input to check
	 */
	public boolean checkEmail( String input )
	{
		boolean returnValue = true;

		if(		input == null ||
				input.length() < 6 ||
				!input.matches( ".*[@].*" ) ||
				!input.matches( ".*[.].*" )
				)
		{
			returnValue = false;
		}

		return returnValue;
	}

	/**
	 * Verify that two strings match<p/>
	 *
	 * Return true if input passes the test, false otherwise
	 *
	 * @since		20111213
	 * @return		boolean
	 * @param		leftInput			The first input to compare
	 * @param		rightInput			The second input to compare
	 */
	public boolean checkMismatch( String leftInput, String rightInput )
	{
		boolean returnValue = true;

		if( !leftInput.equals( rightInput ) )
		{
			returnValue = false;
		}

		return returnValue;
	}

	/**
	 * Verify that the input matches minimum requirements for passwords<p/>
	 *
	 * Return true if input passes the test, false otherwise
	 *
	 * @since		20111213
	 * @return		boolean
	 * @param		input			The input to check
	 */
	public boolean checkPassStrength( String input )
	{
		boolean returnValue = true;

		// not implemented generically

		return returnValue;
	}

	/**
	 * Verify that the input is not a duplicate in a data source<p/>
	 *
	 * Use this method for data additions with string input to check<p/>
	 *
	 * Return true if input passes the test, false otherwise
	 *
	 * @since		20111213
	 * @return		boolean
	 * @param		input				The first input to check
	 * @param		tableName			The name of the data table to check
	 * @param		fieldName			The name of the field to check for duplicates
	 */
	public boolean checkDuplicates( String input, String tableName, String fieldName )
	{
		boolean returnValue = true;

		String sql = "SELECT COUNT(*) FROM " + tableName + " WHERE " + fieldName + " = '" + AiSTr.dbEscape( input ) + "'";
		String[][] result = this.mDb.query( sql );
		if( result.length > 0 )
		{
			Integer count = Integer.valueOf( new String( result[0][0] ) );
			if( count > 0 )
			{
				returnValue = false;
			}

		}

		return returnValue;
	}

	/**
	 * Verify that the input is not a duplicate in a data source<p/>
	 *
	 * Use this method for data additions with integer input to check<p/>
	 *
	 * Return true if input passes the test, false otherwise
	 *
	 * @since		20111213
	 * @return		boolean
	 * @param		input				The first input to check
	 * @param		tableName			The name of the data table to check
	 * @param		fieldName			The name of the field to check for duplicates
	 */
	public boolean checkDuplicates( int input, String tableName, String fieldName )
	{
		boolean returnValue = true;

		String sql = "SELECT COUNT(*) FROM " + tableName + " WHERE " + fieldName + " = " + input;
		String[][] result = this.mDb.query( sql );
		if( result.length > 0 )
		{
			Integer count = Integer.valueOf( new String( result[0][0] ) );
			if( count > 0 )
			{
				returnValue = false;
			}

		}

		return returnValue;
	}

	/**
	 * Verify that the input is not a duplicate in a data source<p/>
	 *
	 * Use this method for data modifications with string input to check<p/>
	 *
	 * Return true if input passes the test, false otherwise
	 *
	 * @since		20111213
	 * @return		boolean
	 * @param		input				The first input to check
	 * @param		tableName			The name of the data table to check
	 * @param		fieldName			The name of the field to check for duplicates
	 * @param		idField				The name of the field containing the unique key
	 * @param		idValue				The unique key value
	 */
	public boolean checkDuplicates( String input, String tableName, String fieldName, String idField, int idValue )
	{
		boolean returnValue = true;

		String sql = "SELECT COUNT(*) FROM " + tableName + " WHERE " + fieldName + " = '" + AiSTr.dbEscape( input ) + "' AND " + idField + " != " + idValue;
		String[][] result = this.mDb.query( sql );
		if( result.length > 0 )
		{
			Integer count = Integer.valueOf( new String( result[0][0] ) );
			if( count > 0 )
			{
				returnValue = false;
			}

		}

		return returnValue;
	}

	/**
	 * Verify that the input is not a duplicate in a data source<p/>
	 *
	 * Use this method for data modifications with integer input to check<p/>
	 *
	 * Return true if input passes the test, false otherwise
	 *
	 * @since		20111213
	 * @return		boolean
	 * @param		input				The first input to check
	 * @param		tableName			The name of the data table to check
	 * @param		fieldName			The name of the field to check for duplicates
	 * @param		idField				The name of the field containing the unique key
	 * @param		idValue				The unique key value
	 */
	public boolean checkDuplicates( int input, String tableName, String fieldName, String idField, int idValue )
	{
		boolean returnValue = true;

		String sql = "SELECT COUNT(*) FROM " + tableName + " WHERE " + fieldName + " = " + input + " AND " + idField + " != " + idValue;
		String[][] result = this.mDb.query( sql );
		if( result.length > 0 )
		{
			Integer count = Integer.valueOf( new String( result[0][0] ) );
			if( count > 0 )
			{
				returnValue = false;
			}

		}

		return returnValue;
	}

}
