package fishrungames.bashgid;

import java.util.ArrayList;

import fishrungames.bashgid.core.AlbumManager.AlbumShortData;

import fishrungames.networkutils.ImageManager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;

import android.widget.TextView;

public class AlbumListFragment extends Fragment
{

	ListAdapter listAdapter;

	GridView gridView;

	ArrayList<AlbumShortData> albumArr;

	ProgressBar progressBar;

	public AlbumListFragment()
	{
		RestoreAlbumList();
	}

	public AlbumListFragment(ArrayList<AlbumShortData> albumArr)
	{
		this.albumArr = albumArr;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{

		View rootView = inflater.inflate(R.layout.fragment_video_page, container, false);

		progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);

		progressBar.setVisibility(View.VISIBLE);

		gridView = (GridView) rootView.findViewById(R.id.gridView);

		listAdapter = new ListAdapter(getActivity());

		gridView.setAdapter(listAdapter);

		gridView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				MainActivity.getInstance().OpenPhotoAlbumScreen(albumArr.get(position).name);
			}

		});

		return rootView;
	}

	public void RestoreAlbumList()
	{
		albumArr = new ArrayList<AlbumShortData>();

		DatabaseGetAlbumListTask task = new DatabaseGetAlbumListTask();

		task.mHandler = MainActivity.getInstance().mHandler;

		task.execute();
	}

	public void SetAlbumList(ArrayList<AlbumShortData> albumArr)
	{
		progressBar.setVisibility(View.GONE);

		this.albumArr = albumArr;

		listAdapter.notifyDataSetChanged();
	}

	public static class DatabaseGetAlbumListTask extends AsyncTask<String, Integer, Long>
	{

		ArrayList<AlbumShortData> albumArr;

		public Handler mHandler;

		protected Long doInBackground(String... queryArr)
		{

			albumArr = MainActivity.getInstance().albumDataSource.GetAllAlbumShortDataExceptHidden();

			return (long) 0;
		}

		protected void onProgressUpdate(Integer... progress)
		{
			Message completeMessage = mHandler.obtainMessage(MainActivity.DATABASE_GET_ALBUM_LIST_STATE_UPDATE, albumArr);

			completeMessage.sendToTarget();

		}

		protected void onPostExecute(Long result)
		{
			Message completeMessage = mHandler.obtainMessage(MainActivity.DATABASE_GET_ALBUM_LIST_STATE_FINISHED, albumArr);

			completeMessage.sendToTarget();
		}
	}

	public class ListAdapter extends BaseAdapter
	{
		private Context mContext;

		public ListAdapter(Context c)
		{
			mContext = c;
		}

		public int getCount()
		{
			return albumArr.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.video_page_list_element, null);

			} else
			{
				view = convertView;
			}

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

			ImageManager.getInstance().ApplyImageToImageView(imageView, albumArr.get(position).firstImageUrl);

			nameTextView.setText(albumArr.get(position).title);

			return view;
		}

	}
}
