package fishrungames.bashgid;

import fishrungames.bashgid.core.AlbumManager.AlbumFullData;
import fishrungames.bashgid.core.JournalManager.JournalSemiFullData;

import fishrungames.networkutils.ImageManager;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;

import android.widget.TextView;

public class MainPageHListViewAdapters
{

	public static class PhotoAlbumElementAdapter extends BaseAdapter
	{

		private Context mContext;

		AlbumFullData albumFullData;

		public PhotoAlbumElementAdapter(Context c, AlbumFullData albumFullData)
		{
			this.mContext = c;
			this.albumFullData = albumFullData;
		}

		public int getCount()
		{
			return albumFullData.photoRecordArr.size();
		}

		public Object getItem(int position)
		{
			return null;
		}

		public long getItemId(int position)
		{
			return 0;
		}

		// create a new ImageView for each item referenced by the Adapter
		@SuppressLint("InflateParams")
		public View getView(int position, View convertView, ViewGroup parent)
		{

			View view;
			if (convertView == null)
			{ // if it's not recycled, initialize some attributes
				LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.article_album_element_item, null);

			} else
			{
				view = convertView;
			}

			ImageView imageView = (ImageView) view.findViewById(R.id.imageView);

			ImageManager.getInstance().ApplyImageToImageView(imageView, albumFullData.photoRecordArr.get(position).imageUrl);

			return view;
		}

	}

	public static class JournalElementAdapter extends BaseAdapter
	{

		private Context mContext;

		JournalSemiFullData journal;

		public JournalElementAdapter(Context c, JournalSemiFullData journal)
		{
			this.mContext = c;
			this.journal = journal;

		}

		public int getCount()
		{
			return journal.articleArr.size();
		}

		public Object getItem(int position)
		{
			return null;
		}

		public long getItemId(int position)
		{
			return 0;
		}

		// create a new ImageView for each item referenced by the Adapter
		@SuppressLint("InflateParams")
		public View getView(final int position, View convertView, ViewGroup parent)
		{

			View view;
			if (convertView == null)
			{ // if it's not recycled, initialize some attributes
				LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.simple_hview_list_element, null);

			} else
			{
				view = convertView;
			}

			TextView textView = (TextView) view.findViewById(R.id.dateTextView);

			TextView subTextView = (TextView) view.findViewById(R.id.subTextView);

			ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton);

			textView.setText(journal.articleArr.get(position).title);

			subTextView.setText(journal.articleArr.get(position).dateTime.toString());

			String imageUrl = journal.articleArr.get(position).previewImageUrl;

			ImageManager.getInstance().ApplyImageToImageView(imageButton, imageUrl);

			imageButton.setOnClickListener(new OnClickListener()
			{

				@Override
				public void onClick(View v)
				{
					MainActivity.getInstance().OpenArticleScreen(journal.articleArr.get(position).name,
							MainActivity.TAG_BACK_TO_MAINFRAGMENT);
				}

			});

			return view;
		}

	}

}
