package com.sharefaith.thesharefaithapp.adapters;

import android.app.ActivityManager;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
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.SFConfig;
import com.sharefaith.thesharefaithapp.base.SFUtil;
import com.sharefaith.thesharefaithapp.models.SFNewsletterModel;

import java.io.File;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 *@deprecated
 */
public class SFNewsletterAdapter extends BaseAdapter
{
	private Typeface myFace;
	private Typeface headerFace;

	private SFNewsletterModel[] mItems;
	private BitmapFactory.Options mOptions;
	private static LimitedMapCache<String, Bitmap> mBitMapCache;
	public Context mContext;
	public SFConfig mConfig;

	public SFNewsletterAdapter( Context context, SFNewsletterModel[] items )
	{
		this.mItems = items;
		this.mContext = context;

		this.mConfig = new SFConfig();

		this.mOptions = SFUtil.commonImgOpt();

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

	@Override
	public View getView( int position, View convertView, ViewGroup parent )
	{

		//Holds view info:
		ViewHolder holder;

		if( convertView == null )
		{
			LayoutInflater vi = LayoutInflater.from(this.mContext);
			convertView = vi.inflate(R.layout.fragment_sfnewsletter_item, null);  // Posts and Newsletters use the same interface
			//Yikes, the cost of inflation
			holder = new ViewHolder();
			holder.imageView = (ImageView) convertView.findViewById(R.id.newsletterMenuImageView);
			holder.imgFile = null; //So won't match below.

			holder.titleView = (TextView) convertView.findViewById( R.id.newsletterTitleTextView );
			holder.excerptView = (TextView) convertView.findViewById( R.id.newsletterExcerptTextView );
			holder.dateView = (TextView) convertView.findViewById( R.id.showDateTextView );
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}


		SFNewsletterModel currentObject = this.mItems[position];

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

		if (currentObject != null) //Fill in variable info, constructing view or not:
		{
			String showTitle = SFUtil.stripHtml( currentObject.mTitle ).toString();
			if( holder.titleView != null )
			{
				holder.titleView.setText( showTitle );
			}

			if( holder.excerptView != null )
			{
				String showContent = SFUtil.stripHtml( currentObject.mContent ).toString();
				int stringEnd = (showContent.length() > 255 ) ? 255 : showContent.length();
				showContent = showContent.substring( 0, stringEnd );
				holder.excerptView.setText( showContent );
			}

			if( currentObject.mThumbnail.length() > 0 && currentThumbnail.exists())// && !currentThumbnail.equals(holder.imgFile))// && !holder.renderedImg)
			{
				holder.imgFile = currentThumbnail;
				String fileStr = currentThumbnail.getAbsolutePath();

				Bitmap bitmap = this.mBitMapCache.get(fileStr); //Memory's faster than IO
				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.INVISIBLE );
			}
			else
			{
				holder.imageView.setImageBitmap( null );

				holder.dateView.setVisibility( View.VISIBLE );
				holder.dateView.setText( currentObject.mShowDate );
			}

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

	public void clearCache()
	{
		this.mBitMapCache.clear();
	}

}
