package com.sharefaith.thesharefaithapp.adapters;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sharefaith.thesharefaithapp.R;
import com.sharefaith.thesharefaithapp.base.SFApplication;
import com.sharefaith.thesharefaithapp.base.SFConfig;
import com.sharefaith.thesharefaithapp.base.SFUtil;
import com.sharefaith.thesharefaithapp.models.SFNavMenuModel;
import com.sharefaith.thesharefaithapp.ui.SFSharedUIMethods;

/**
 * Created by Sharefaith1 on 2014-10-22.
 * Adapter for Nav menu items
 */
public class SFNavMenuAdapter extends BaseAdapter
{
	private SFNavMenuModel[] mItems;
	public Context mContext;
	private SFConfig mConfig;

	/**
	 * Constructor
	 * @param context
	 * @param items
	 */
	public SFNavMenuAdapter( Context context, SFNavMenuModel[] items )
	{
		this.mItems = items;
		this.mContext = context;
		this.mConfig = new SFConfig();
	}

	/**
	 *
	 * @param position
	 * @param convertView
	 * @param parent
	 * @return
	 */
	@Override
	public View getView( int position, View convertView, ViewGroup parent )
	{
		View v = convertView;

		if( v == null )
		{
			LayoutInflater vi = LayoutInflater.from( this.mContext );
			v = vi.inflate( R.layout.fragment_nav_menu_item, null );
		}

		SFNavMenuModel currentObject = this.mItems[position];
		if( currentObject != null )
		{

			TextView contentView = (TextView) v.findViewById( R.id.mainMenuItem );

			if( contentView != null )
			{
				contentView.setTextColor( Color.parseColor( "#" + this.mConfig.customFontColorForNavColor() ) );
				contentView.setText( currentObject.mDisplayLabel );
			}

		}

		return v;
	}

	/**
	 *  Actions to get number of items in arraylist
	 *
	 * @since		20120106
	 */

	@Override
	public int getCount()
	{
		return this.mItems.length;
	}

	/**
	 *  Actions to get position of item
	 *
	 * @since		20120106
	 */

	@Override
	public Object getItem( int position )
	{
		return null;
	}

	/**
	 *  Actions to get item ID
	 *
	 * @since		20120106
	 */

	@Override
	public long getItemId( int id )
	{
		return 0;
	}

	/**
	 * Defines the onClick handler for a listview
	 *
	 * @since		20120106
	 */
	public AdapterView.OnItemClickListener handleTap( final Activity context )
	{
		final SFApplication application = SFApplication.getInstance();

		return new AdapterView.OnItemClickListener()
		{
			@Override
			public void onItemClick( AdapterView<?> parent, View view, int position, long id )
			{
				SFNavMenuModel[] items = application.getAppSections( false );
				SFNavMenuModel currentItem = items[position];
				if( currentItem.mSfTag.equals( "location" ) )
				{
					SFSharedUIMethods.closeMenu();
					SFUtil.logSiteEvent( "Opened Map" );
					SFSharedUIMethods.launchMap(currentItem.mPositionIndex);
				}
				else if( currentItem.mSfTag.equals( "donate" ) )
				{
					SFSharedUIMethods.closeMenu();
					SFUtil.logSiteEvent( "Opened Link" );
					SFSharedUIMethods.launchDonate(currentItem.mPositionIndex);
				}
				else if( currentItem.mPositionIndex == application.currentSectionPosition )
				{
					SFSharedUIMethods.closeMenu();
				}
				else
				{
					application.currentSectionPosition = currentItem.mPositionIndex;
					SFSharedUIMethods.swapToViewWithTag( currentItem.mSfTag, currentItem.mPositionIndex );
				}

			}
		};
	}
}
