package com.sharefaith.thesharefaithapp.adapters;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sharefaith.thesharefaithapp.R;
import com.sharefaith.thesharefaithapp.base.SFAppData;
import com.sharefaith.thesharefaithapp.base.SFApplication;
import com.sharefaith.thesharefaithapp.base.SFConfig;
import com.sharefaith.thesharefaithapp.base.SFUtil;
import com.sharefaith.thesharefaithapp.models.SFMoreModel;

import java.io.File;

/**
 * Created by sfwebsitedev on 4/6/16.
 * Adapter for More items
 */
public class SFMoreAdapter extends BaseAdapter
{
    private SFMoreModel[] mItems;
    private BitmapFactory.Options mOptions;
    private static LimitedMapCache<String, Bitmap> mBitMapCache;
    private SFApplication mApplication = SFApplication.getInstance();
    private SFConfig mConfig;
    private SFAppData mAppData = new SFAppData();

    /**
     * Constructor
     * @param items
     */
    public SFMoreAdapter( SFMoreModel[] items )
    {
        this.mItems = items;
        this.mOptions = SFUtil.commonImgOpt();
        this.mConfig = new SFConfig();

        int memMB = ((ActivityManager)mApplication.getSystemService( Context.ACTIVITY_SERVICE )).getMemoryClass();
        this.mBitMapCache = new LimitedMapCache<String,Bitmap> ( memMB/2 );
    }

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

        ViewHolder holder;
        if (convertView == null)
        {
            LayoutInflater vi = LayoutInflater.from( mApplication );
            int layout = mAppData.getLayout( mApplication.currentMorePosition );
            //Log.d("mTag","layout = " + layout );
            convertView = getLayout( vi , layout );
            //Yikes, the cost of inflation
            holder = new ViewHolder();

            //holder = null;
            holder.imageView = (ImageView) convertView.findViewById( R.id.imageView );
            holder.imgFile = null;//So it won't match below

            holder.titleView = (TextView) convertView.findViewById( R.id.title_textView );
            holder.excerptView = (TextView) convertView.findViewById( R.id.content_textView );
            holder.dateView = (TextView) convertView.findViewById( R.id.date_textView ); //big box
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        SFMoreModel currentObject = this.mItems[position];

        File currentThumbnail = new File( this.mConfig.mCustomAssetPath + currentObject.mThumbnailPath );

        if( currentObject != null ) //Set the variable things, title and excerpt, even if not creating.
        {
            String showTitle = SFUtil.stripHtml( currentObject.mTitle ).toString();
            if( holder.titleView != null && mAppData.getIsTitleEnabled( mApplication.currentMorePosition ) )
            {
                //holder.titleView.setTypeface( this.headerFace );
                //holder.titleView.setTextSize( mContext.getResources().getDimension( R.dimen.post_preview_title_font_size ) );
                holder.titleView.setText( showTitle );
                holder.titleView.setVisibility( View.VISIBLE );
            }
            else
            {
                //if image is missing show title
                if( holder.titleView != null && currentObject.mThumbnailPath.length() < 1 )
                {
                    holder.titleView.setText( showTitle );
                    holder.titleView.setVisibility( View.VISIBLE );
                }
                else
                {
                    holder.titleView.setVisibility( View.GONE );
                }
            }
            //Must consider cases where the view was recycled.
            if( currentObject.mThumbnailPath.length() > 0 && currentThumbnail.exists())//  && !currentThumbnail.equals(holder.imgFile) )
            {
                //The view has different img.
                holder.imgFile = currentThumbnail;
                String fileStr = currentThumbnail.getAbsolutePath();

                Bitmap bitmap = this.mBitMapCache.get(fileStr);
                if (null == bitmap)
                {
                    try {
                        bitmap = BitmapFactory.decodeFile(fileStr, this.mOptions);
                        this.mBitMapCache.add(fileStr, bitmap);
                    }
                    catch (OutOfMemoryError e)
                    {
                        //((SFMainActivity)this.mContext).clearCaches();
                        System.gc();
                        bitmap = BitmapFactory.decodeFile(fileStr, this.mOptions);
                    }
                }
                holder.imageView.setImageBitmap(bitmap);
                holder.dateView.setVisibility( View.GONE );
            }
			else
			{
				holder.imageView.setImageBitmap( null );
			}
            convertView.setTag(holder);
        }

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

    /**
     * clear Bitmap cache
     */
    public void clearCache()
    {
        this.mBitMapCache.clear();
    }

    /**
     * Get the layout of the more section
     * @param vi
     * @param layout
     * @return
     */
    private View getLayout( LayoutInflater vi, int layout)
    {
        View view = null;

        if( layout == 0 )
        {
            view = vi.inflate( R.layout.sf_gamut_item, null );
        }

        else if( layout == 1 )
        {
            view = vi.inflate( R.layout.sf_nook_item, null );
        }

        else if( layout == 2 )
        {
            view = vi.inflate( R.layout.sf_nook_item, null );
        }

        return view;
    }
}


