package com.sharefaith.thesharefaithapp.models;

import com.appideas.base.AiDb;
import com.appideas.base.AiSTr;
import com.sharefaith.thesharefaithapp.base.SFAppData;
import com.sharefaith.thesharefaithapp.base.SFApplication;
import com.sharefaith.thesharefaithapp.base.SFConfig;

/**
 * Created by Sharefaith1 on 2014-10-22.
 */
public class SFNavMenuModel
{
	/**
	 * This object's database ID
	 */
	public int mId;

	/**
	 * This object's location in the UI
	 */
	public int mUiLocation;

	/**
	 * This object's position in it's context
	 */
	public int mPositionIndex;

	/**
	 * This last time the object was updated
	 */
	public int mLastUpdate;

	/**
	 * This object's database tag
	 */
	public String mSfTag;

	/**
	 * This object's visible label
	 */
	public String mDisplayLabel;

	/**
	 * The user's current theme (for piecing together the icon location)
	 */
	public String mCurrentTheme;

	public int mExternalId;

	public boolean mTitlesEnabled;

	public int mLayout;

	/**
	 * Instance of the database
	 */
	private AiDb mDb;

	/**
	 * Class constructor
	 *
	 * @since		20141021
	 */
	public SFNavMenuModel( int id )
	{
		if( id < 0 )
		{
			id = 0;
		}
		this.mId = id;

		this.mDb = SFApplication.getInstance().getDb();
		this.refreshMembers();
	}

	public SFNavMenuModel()
	{
		this( 0 );
	}

	/**
	 * Refresh member variables for a specific menu item from the datasource
	 *
	 * @since		20141021
	 */
	public void refreshMembers()
	{
		this.mSfTag = "";
		this.mDisplayLabel = "";
		this.mUiLocation = 0;
		this.mPositionIndex = 0;
		this.mLastUpdate = 0;
		this.mExternalId = 0;
		this.mTitlesEnabled = true;
		this.mLayout = -1;

		if( this.mDb == null )
		{
			this.mDb = SFApplication.getInstance().getDb();
		}

		SFConfig config = new SFConfig();
		if( this.mCurrentTheme == null || this.mCurrentTheme.length() < 1 )
		{
			this.mCurrentTheme = config.mTheme;
		}

		String sql =	"SELECT     sf_tag, display_label, ui_location, position_index, last_update, external_id, titles_enabled, layout " +
						"FROM       ondevice_sections " +
						"WHERE      id = " + this.mId;
		String[][] result = this.mDb.query( sql );
		for( int i = 0; i < result.length; i++ )
		{
			this.mSfTag =  AiSTr.denullifyString( result[i][0] );
			this.mDisplayLabel =  AiSTr.denullifyString( result[i][1] );
			this.mUiLocation = AiSTr.denullifyInt( result[i][2] );
			this.mPositionIndex = AiSTr.denullifyInt( result[i][3] );
			this.mLastUpdate = AiSTr.denullifyInt( result[i][4] );
			this.mExternalId = AiSTr.denullifyInt( result[i][5] );
			this.mTitlesEnabled= AiDb.fixBoolean( AiSTr.denullifyInt( result[i][6] ) );
			this.mLayout = AiSTr.denullifyInt( result[i][7] );


			// IMPORTANT: Due to Google banning us based on wording of the menus, we are forcing the donate link title
			//if( this.mSfTag.equals( "donate" ) )
			{
				//this.mDisplayLabel = "ONLINE GIVING";
			}
		}

		//SFAppData appData = new SFAppData();
	}

	/**
	 * Get the Nav models that are not under more section
	 * @return
	 */
	public static SFNavMenuModel[] getTopLevel()
	{
		SFAppData appData = new SFAppData();
		Integer[] idArray = appData.getTopLevelSectionIds();
		SFNavMenuModel[] menuItems = new SFNavMenuModel[idArray.length];

		for( int i = 0; i < idArray.length; i++ )
		{
			menuItems[i] = new SFNavMenuModel( idArray[i] );
		}

		return menuItems;
	}



}
