package com.sharefaith.thesharefaithapp.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sharefaith.thesharefaithapp.R;
import com.sharefaith.thesharefaithapp.base.SFApplication;
import com.sharefaith.thesharefaithapp.models.SFCalendarEventModel;
import com.sharefaith.thesharefaithapp.ui.SFCalendarActivity;

/**
 * Created by Anthony on 11/28/16.
 * Adapter for Calendar Events
 */

public class SFEventAdapter extends BaseAdapter
{
    private SFApplication mApplication = SFApplication.getInstance();
    private SFCalendarEventModel[] mEventModels;

    /**
     * Constructor
     * @param eventModels
     */
    public SFEventAdapter( SFCalendarEventModel[] eventModels )
    {
        this.mEventModels = eventModels;
    }

    static class ViewHolder
    {
        TextView mTitle;
        TextView mDescription;

    }

    /**
     *
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView( final int position, View convertView, ViewGroup parent )
    {
        //Holds view info:
        ViewHolder holder = new ViewHolder();

        if( convertView == null )
        {
            LayoutInflater vi = LayoutInflater.from(mApplication);
            convertView = vi.inflate( R.layout.sf_event_item, parent, false);
            //Yikes, the cost of inflation
            holder.mTitle = (TextView) convertView.findViewById(R.id.textView_title);
            holder.mDescription = (TextView) convertView.findViewById( R.id.textView_description );
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        if( holder.mTitle != null )
        {
            holder.mTitle.setText( this.mEventModels[position].mTitle );
        }
        if( holder.mDescription != null )
        {
            holder.mDescription.setText( this.mEventModels[position].mDescription );
        }

        convertView.setOnClickListener( new View.OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                SFCalendarActivity activity = (SFCalendarActivity) mApplication.getCurrentActivity();
                activity.populateEventDetails( mEventModels[position] );
                activity.mEventListContainer.setVisibility( View.INVISIBLE );
            }
        } );
        return convertView;
    }

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

    @Override
    public int getCount()
    {
        return mEventModels.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;
    }
}