package com.appideas.base;

import com.sharefaith.thesharefaithapp.R;
import com.sharefaith.thesharefaithapp.base.SFApplication;

import java.util.HashMap;
//import android.util.Log;

/**
 * An example implementer of the APPideas Object Template<br/>
 *
 * @author		costmo
 * @version		20111213
 * @since		20111213
 *
 */

public class ObjectTemplate implements AiOT
{
	/**
	 * This object's ID
	 */
	public int mId;

	/**
	 * A string data field
	 */
	public String mStringData;

	/**
	 * An int data field
	 */
	public int mIntData;

	/**
	 * An active flag
	 */
	public int mActive;

	/**
	 * An instance of the database
	 */
	public AiDb mDb;

	/**
	 * Class constructor
	 *
	 * @since		20111213
	 * @param		id			The ID of the object
	 */
	public ObjectTemplate( int id )
	{
		if( id < 0 )
		{
			id = 0;
		}

		this.mDb = SFApplication.getInstance().getDb();
		this.mId = id;
		this.assignMemberVars();
	}

	// checkMemberVars().
	// Inheriting javadoc comments from interface
	public String checkMemberVars( boolean isNew )
	{
		String returnValue = "";

		// do input checking
		AiICs ic = new AiICs();

		if( !ic.checkBlank( this.mStringData ) )
		{
			returnValue = returnValue + R.string.error_stringDataBlank;
		}
		if( !ic.checkBlank( Integer.toString( this.mIntData ) ) )
		{
			returnValue = returnValue + R.string.error_intDataBlank;
		}
		if( isNew )
		{
			if( !ic.checkDuplicates( this.mStringData, "ObjectTemplate", "string_data"  ) )
			{
				returnValue = returnValue + R.string.error_stringDataExists;
			}
			if( !ic.checkDuplicates( this.mIntData, "ObjectTemplate", "int_data"  ) )
			{
				returnValue = returnValue + R.string.error_intDataExists;
			}
		}
		else
		{
			if( !ic.checkDuplicates( this.mStringData, "ObjectTemplate", "string_data", "id", this.mId ) )
			{
				returnValue = returnValue + R.string.error_stringDataExists;
			}
			if( !ic.checkDuplicates( this.mIntData, "ObjectTemplate", "int_data", "id", this.mId ) )
			{
				returnValue = returnValue + R.string.error_intDataExists;
			}
		}

		if( returnValue.length() < 1 )
		{
			returnValue = "ok";
		}

		return returnValue;
	}

	// populateMemberVars().
	// Inheriting javadoc comments from interface
	public boolean populateMemberVars( HashMap<String,String>  input )
	{
		boolean returnValue = true;

		this.mStringData = AiSTr.denullifyString( input.get( "stringData" ) );
		this.mIntData = AiSTr.stringToInt( input.get( "intData" ) );

		return returnValue;
	}

	// assignMemberVars().
	// Inheriting javadoc comments from interface
	public void assignMemberVars()
	{
		// assign defaults in case the data query returns no result
		this.mStringData = "";
		this.mIntData = 0;
		this.mActive = 0;

		String sql = "SELECT string_data, int_data, active FROM ObjectTemplate WHERE id = " + this.mId;
		String[][] result = this.mDb.query( sql );
		for( int i = 0; i < result.length; i++ )
		{
			this.mStringData = AiSTr.denullifyString( result[i][0] );
			this.mIntData = AiSTr.stringToInt( result[i][1] );
			this.mActive =  AiSTr.stringToInt( result[i][2] );
		}
	}

	// add().
	// Inheriting javadoc comments from interface
	public int add()
	{
		int returnValue = 0;
		this.mId = this.mDb.insertBlank( "ObjectTemplate", "id" );
		if( this.mId != 0 )
		{
			returnValue = this.mId;
			this.setActive( 1 );
			this.save();
		}

		return returnValue;
	}

	// save().
	// Inheriting javadoc comments from interface
	public int save()
	{
		int returnValue = this.mId;

		String sql = 	"UPDATE		ObjectTemplate " +
				"SET 		string_data = '" + AiSTr.dbEscape( this.mStringData ) + "', " +
				"int_data =  " + this.mIntData + " " +
				"WHERE id = " + this.mId;
		this.mDb.noReturnQuery( sql );

		return returnValue;
	}

	// remove().
	// Inheriting javadoc comments from interface
	public boolean remove( boolean delete )
	{
		boolean returnValue = true;

		if( delete )
		{
			String sql = "DELETE FROM ObjectTemplate WHERE id = " + this.mId;
			this.mDb.noReturnQuery( sql );
		}
		else
		{
			this.setActive( 0 );
		}

		return returnValue;
	}

	// setActive().
	// Inheriting javadoc comments from interface
	public boolean setActive( int newValue )
	{
		boolean returnValue = true;

		if( newValue != 0 && newValue != 1 )
		{
			return false;
		}

		String sql = "UPDATE ObjectTemplate SET active = " + newValue + " WHERE id = " + this.mId;
		this.mDb.noReturnQuery( sql );

		return returnValue;
	}

	/**
	 * Creates an array containing all instances of the object<p/>
	 *
	 * Return an array of objects.
	 *
	 * @since		20111213
	 * @return		Object[]
	 * @param		orderBy				The data field to order by
	 * @param		includeInactive		Whether or not inactive records should be included
	 */
	public static ObjectTemplate[] getAll( String orderBy, boolean includeInactive )
	{
		Integer[] idArray = ObjectTemplate.getAllIds( orderBy, includeInactive );
		ObjectTemplate[] objectTemplate = new ObjectTemplate[idArray.length];

		for( int i = 0; i < idArray.length; i++ )
		{
			objectTemplate[i] = new ObjectTemplate( idArray[i].intValue() );
		}

		return objectTemplate;
	}

	/**
	 * Creates an array containing IDs of all data records<p/>
	 *
	 * Return an array of Integers.
	 *
	 * @since		20111213
	 * @return		Integer[]
	 * @param		orderBy				The data field to order by
	 * @param		includeInactive		Whether or not inactive records should be included
	 */
	public static Integer[] getAllIds( String orderBy, boolean includeInactive )
	{
		AiDb db = SFApplication.getInstance().getDb();
		String sql = "SELECT id FROM ObjectTemplate ";
		if( !includeInactive )
		{
			sql = sql + "WHERE active = 1 ";
		}
		sql = sql + "ORDER BY " + orderBy;
		String[][] result = db.query( sql );
		Integer[] outputArray = new Integer[result.length];
		for( int i = 0; i < result.length; i++ )
		{
			outputArray[i] = new Integer( AiSTr.stringToInt( result[i][0] ) );
		}

		return outputArray;
	}

	/**
	 * Gets an object's ID based on a title field value<p/>
	 *
	 * Return an ID or zero if not found
	 *
	 * @since		20111213
	 * @return		int
	 * @param		title			The value to find
	 */
	public static int idFromTitle( String title )
	{
		int returnValue = 0;
		AiDb db = SFApplication.getInstance().getDb();

		String sql = "SELECT id FROM ObjectTemplate WHERE LOWER( string_data ) = '" + AiSTr.dbEscape( title.toLowerCase() ) + "'";
		String[][] result = db.query( sql );
		if( result.length > 0 )
		{
			Integer id = Integer.valueOf( new String( result[0][0] ) );
			returnValue = id.intValue();
		}

		return returnValue;
	}
}