package com.sharefaith.thesharefaithapp.adapters;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
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.SFImageLoaderFromURL;
import com.sharefaith.thesharefaithapp.base.SFUtil;
import com.sharefaith.thesharefaithapp.models.SFVideoStreamModel;

/**
 * Created by Anthony on 12/12/16.
 * Adapter for video streams
 */

public class SFVideoStreamAdapter extends BaseAdapter
{

    private SFVideoStreamModel[] mItems;
    private BitmapFactory.Options mOptions;
    private static LimitedMapCache<String, Bitmap> mBitMapCache;

    public SFConfig mConfig;
    private SFAppData mAppData = new SFAppData();
    private SFApplication mApplication = SFApplication.getInstance();

    /**
     * Constructor
     * @param items
     */
    public SFVideoStreamAdapter( SFVideoStreamModel[] 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.currentVideoStreamPosition );
            convertView = getLayout( vi, layout );
            //Yikes, the cost of inflation
            holder = new ViewHolder();

            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
            holder.status = (TextView) convertView.findViewById( R.id.sf_textview_status );
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        SFVideoStreamModel currentObject = this.mItems[position];
        holder.position = position;


        if( currentObject != null ) //Set the variable things, title and excerpt, even if not creating.
        {
            String showTitle = SFUtil.stripHtml( currentObject.mTitle ).toString();
            holder.dateView.setVisibility( View.VISIBLE );
            if( holder.titleView != null && mAppData.getIsTitleEnabled( mApplication.currentVideoStreamPosition ))
            {
                holder.titleView.setText( showTitle );
                holder.titleView.setVisibility( View.VISIBLE );
            }
            else
            {
                //if image is missing show title
                if( holder.titleView != null && currentObject.mImageURL.length() < 1 )
                {
                    holder.titleView.setText( showTitle );
                    holder.titleView.setVisibility( View.VISIBLE );
                }
                else
                {
                    holder.titleView.setVisibility( View.GONE );
                }
            }
            //long start = System.currentTimeMillis();
            String showContent = currentObject.mDescription;
            if( holder.excerptView != null )
            {
                holder.excerptView.setText( showContent );
            }

            //Must consider cases where the view was recycled.
            if( currentObject.mImageURL.length() > 0 )
            {
                try {
                    SFImageLoaderFromURL imageLoader = new SFImageLoaderFromURL();
                    imageLoader.loadImageFromURL( currentObject.mImageURL, holder.imageView );
                    holder.dateView.setVisibility( View.GONE );
                }
                catch (OutOfMemoryError e)
                {
                    //((SFMainActivity)this.mContext).clearCaches();
                    System.gc();
                    Log.e("Error", "Error: OutOfMemory");
                    //bitmap = BitmapFactory.decodeFile(fileStr, this.mOptions);
                }
            }
            else
            {
                holder.imageView.setImageDrawable( null );
                holder.dateView.setVisibility( View.VISIBLE );
            }
            if( currentObject.mType.equals( "archive" ) )
            {
                holder.status.setVisibility( View.GONE );
            }
            else
            {
                if( currentObject.mType.equals( "upcoming" ) )
                {
                    holder.status.setText( "UPCOMING" );
                    holder.status.setVisibility( View.VISIBLE );
                }
                else
                {
                    holder.status.setText( "LIVE" );
                    holder.status.setVisibility( View.VISIBLE );
                }
            }


            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 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_reveal_item, null );
        }

        return view;
    }
}


