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.base.SFConfig;
import com.sharefaith.thesharefaithapp.models.SFChapterModel;
import com.sharefaith.thesharefaithapp.ui.SFBibleActivity;

/**
 * Created by sfwebsitedev on 3/18/16.
 */
public class SFBibleChapterAdapter extends BaseAdapter
{
    private SFApplication mApplication = SFApplication.getInstance();
    private SFConfig mConfig;
    private SFChapterModel mChapters[];
    private int mBookIndex;

    /**
     * Constructor
     * @param chapters
     * @param bookIndex
     */
    public SFBibleChapterAdapter( SFChapterModel[] chapters, int bookIndex )
    {
       mChapters = chapters;
        mBookIndex = bookIndex;
        this.mConfig = new SFConfig();
    }

    static class ViewHolder
    {
        TextView mChapter;
    }

    /**
     *
     * @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_bible_chapter_frame, null);
            //Yikes, the cost of inflation
            holder.mChapter = (TextView) convertView.findViewById(R.id.chapter_textView);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }


        if( holder.mChapter != null )
        {
            holder.mChapter.setText( Integer.toString( mChapters[position].mChapter) );
        }
        //final ViewHolder finalHolder = holder;
        convertView.setOnClickListener( new View.OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                SFBibleActivity activity = (SFBibleActivity) mApplication.getCurrentActivity();
                mApplication.bookIndex= mBookIndex;
                mApplication.chapterIndex = position;
                activity.loadWebView();
                activity.toggleBookSelect();
                mApplication.releaseMediaPlayer();
                activity.mPlayButton.setImageResource( R.drawable.audio_play );
                activity.mPlayButton.setPadding( mApplication.getDP( 20 ), mApplication.getDP( 18 ), mApplication.getDP( 16 ), mApplication.getDP( 18 ) );
            }
        } );
        return convertView;
    }

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

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