package fishrungames.bashgid;

import android.os.AsyncTask;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import fishrungames.bashgid.core.AlbumManager;
import fishrungames.bashgid.core.AlbumManager.AlbumFullData;
import fishrungames.bashgid.core.DatabaseGetAlbumTask;

public class PhotoFragment extends Fragment
{

	AlbumFullData album = AlbumManager.CreateEmptyAlbum();
	String albumName = "";
	int currentPhoto = -1;

	PhotoFragmentAdapter listAdapter;
	ViewPager pager;

	ProgressBar progressBar;
	
	DatabaseGetAlbumTask databaseGetAlbumTask = null;

	public PhotoFragment()
	{
	}

	public PhotoFragment(AlbumFullData album, int currentPhoto)
	{
		this.album = album;
		this.albumName = album.name;
		this.currentPhoto = currentPhoto;
	}

	public PhotoFragment(String albumName, int currentPhoto)
	{
		this.albumName = albumName;
		this.currentPhoto = currentPhoto;
	}

	@Override
	public void onSaveInstanceState(final Bundle outState)
	{
		super.onSaveInstanceState(outState);
		outState.putString("albumName", albumName);
		outState.putInt("currentPhoto", currentPhoto);
	}

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

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

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

		if (album == null)
		{
			progressBar.setVisibility(View.VISIBLE);
		} else
		{
			progressBar.setVisibility(View.GONE);
		}

		listAdapter = new PhotoFragmentAdapter(getActivity(), album);

		pager = (ViewPager) rootView.findViewById(R.id.pager);

		pager.setAdapter(listAdapter);

		pager.setCurrentItem(currentPhoto);

		if (savedInstanceState != null)
		{
			albumName = savedInstanceState.getString("albumName");
			currentPhoto = savedInstanceState.getInt("currentPhoto");
		}
		
		
		if (album.photoRecordArr.size() == 0 && albumName.length() > 0)
		{
			RestoreAlbum();
		}

		return rootView;
	}
	
	@Override
	public void onDestroyView()
	{
		CancelTaskIfRunning();
		
		super.onDestroyView();
	}

	void RestoreAlbum()
	{
		databaseGetAlbumTask = new DatabaseGetAlbumTask();

		//databaseGetAlbumTask.mHandler = MainActivity.getInstance().mainHandler;

		databaseGetAlbumTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, albumName);
	}

	public void SetAlbum(AlbumFullData album)
	{
		databaseGetAlbumTask = null;
		
		progressBar.setVisibility(View.GONE);

		this.album = album;

		listAdapter.album = album;

		listAdapter.notifyDataSetChanged();
		
		if (currentPhoto < 0)
		{
			currentPhoto = 0;
		}

		pager.setCurrentItem(currentPhoto);
	}
	
	public void RefreshAdapter()
	{
		if (listAdapter != null)
		{
			listAdapter.notifyDataSetChanged();
		}
	}
	
	public void CancelTaskIfRunning()
	{
		if (databaseGetAlbumTask != null)
		{
			databaseGetAlbumTask.cancel(false);
			databaseGetAlbumTask = null;
		}
	}

}