package com.sharefaith.thesharefaithapp.adapters;

import android.content.res.Configuration;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
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.SFBookModel;
import com.sharefaith.thesharefaithapp.models.SFChapterModel;

/**
 * Created by Anthony on 3/18/16.
 * Adapter for Bible Books
 */
public class SFBibleBookAdapter extends BaseAdapter
{
    private SFApplication mApplication = SFApplication.getInstance();
    private SFBookModel[] mItems;
    private SFConfig mConfig;
    private GridView lastClicked;
    private boolean isChapterPickerOpen;

    /**
     * Constructor
     * @param items
     */
    public SFBibleBookAdapter( SFBookModel[] items )
    {
        this.mItems = items;
        this.mConfig = new SFConfig();
    }

    static class ViewHolder
    {
        TextView mBookLong;
        GridView mChapterList;
    }

    /**
     *
     * @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_book_frame, null);
            //Yikes, the cost of inflation
            holder.mBookLong = (TextView) convertView.findViewById(R.id.book_textView);
            holder.mChapterList = (GridView) convertView.findViewById( R.id.chapter_gridView );
            holder.mChapterList.setVisibility( View.GONE );
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
            if(position != mApplication.positionOfOpenBook)
            {
                holder.mChapterList.setVisibility( View.GONE );
            }
        }

        final SFBookModel currentObject = this.mItems[position];

        if (currentObject != null) //Fill in variable info, constructing view or not:
        {
            if( holder.mBookLong != null )
            {
                holder.mBookLong.setText( currentObject.mFullname.toUpperCase() );
            }
            final ViewHolder finalHolder = holder;
            holder.mBookLong.setOnClickListener( new View.OnClickListener()
            {
                @Override
                public void onClick( View v )
                {
                    if(lastClicked!=null)
                    {
                        lastClicked.setAdapter( null );
                        lastClicked.setVisibility( View.GONE );
                    }
                    if(finalHolder.mChapterList != lastClicked||!isChapterPickerOpen)
                    {
                        isChapterPickerOpen = true;
                        mApplication.positionOfOpenBook = position;
                        lastClicked = finalHolder.mChapterList;

                        SFChapterModel chapters[] = new SFChapterModel[mItems[position].mNumChapters];
                        for (int i = 0; i < mItems[position].mNumChapters; i++)
                        {
                            chapters[i] = new SFChapterModel( i + 1 );
                            //Log.d( "mTag", "chapter: " + chapters[i].getChapter() );
                        }
                        mApplication.setChapters( chapters );
                        SFBibleChapterAdapter chapterAdapter = new SFBibleChapterAdapter( chapters,position);
                        finalHolder.mChapterList.setAdapter( chapterAdapter );
                        ViewGroup.LayoutParams list = finalHolder.mChapterList.getLayoutParams();
                        list.height = mApplication.getDP( getGridHeight( mItems[position].mNumChapters ) );
                        finalHolder.mChapterList.setLayoutParams( list );
                        finalHolder.mChapterList.setVisibility( View.VISIBLE );
                    }
                    else
                    {
                        isChapterPickerOpen = false;
                    }
                    notifyDataSetChanged();
                }
            } );
        }
        return convertView;
    }

    /**
     *  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;
    }

    /**
     * Gets the height of the grid
     * @param numChapters
     * @return
     */
    private int getGridHeight(int numChapters)
    {
        Configuration configuration = mApplication.getResources().getConfiguration();

        //float density  = mApplication.getResources().getDisplayMetrics().density;
        int dpWidth  = configuration.screenWidthDp; //outMetrics.widthPixels / density;
        //Log.d("mTag","dpWidth = " + Float.toString( dpWidth ));
        int numChaptersWide =  dpWidth/80;
        int numChaptersHigh =  numChapters/numChaptersWide;
        if(numChapters%numChaptersWide!=0)
        {
            numChaptersHigh++;
        }
        return numChaptersHigh * 80;
    }
}
