package fishrungames.bashgid;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;

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

public class PhotoAlbumFragment extends Fragment
{
	
	AlbumFullData album = AlbumManager.CreateEmptyAlbum();
	String albumName = "";
	
	ListAdapter listAdapter;
	
	ProgressBar progressBar;
	
	DatabaseGetAlbumTask databaseGetAlbumTask = null;
	
	public PhotoAlbumFragment()
	{
	}
	
	public PhotoAlbumFragment(AlbumFullData album)
	{
		this.album = album;
		this.albumName = album.name;
	}
	
	public PhotoAlbumFragment(String albumName)
	{
		this.albumName = albumName;
		RestoreAlbum();
	}
	
	@Override
    public void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("albumName", albumName);
    }
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
			
		View rootView = inflater.inflate(R.layout.fragment_photo_album_page, container, false);
		
		progressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
		
		progressBar.setVisibility(View.VISIBLE);

		listAdapter = new ListAdapter(getActivity());
		
		ListView listView = (ListView) rootView.findViewById(R.id.listView);
		
		listView.setAdapter(listAdapter);
		
		listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				BashgidApplication.SetLastAlbumName(albumName);
				MainActivity.getInstance().OpenPhotoScreen(album, position, MainActivity.TAG_BACK_TO_ALBUMFRAGMENT);
				
			}
		
		});
		
		if (savedInstanceState != null)
		{
			albumName = savedInstanceState.getString("albumName");
        }
		
		if (albumName.length() > 0 && album.photoRecordArr.size() == 0)
		{
			RestoreAlbum();
		}
			
		return rootView;
	}
	
	@Override
	public void onDestroyView()
	{
		CancelTaskIfRunning();
		
		super.onDestroyView();
	}
	
	public void OnDatabaseDonwloadProgress(Integer percent)
	{
		/*
		databaseDownloadProgressBar.setProgress(percent);
		
		if (percent >= 100)
		{
			databaseDownloadProgressBar.setVisibility(View.GONE);
		}
		else
		{
			databaseDownloadProgressBar.setVisibility(View.VISIBLE);
		}
		*/
		if (databaseGetAlbumTask == null || percent == 100)
		{
			RestoreAlbum();
		}
		
		//SetJournalList(r.journalList);
	}
	
	
	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.notifyDataSetChanged();
		
	}
	
	public void CancelTaskIfRunning()
	{
		if (databaseGetAlbumTask != null)
		{
			databaseGetAlbumTask.cancel(false);
			databaseGetAlbumTask = null;
		}
	}
	
	public void RefreshAdapter()
	{
		if (listAdapter != null)
		{
			listAdapter.notifyDataSetChanged();
		}
	}
	
	public class ListAdapter extends BaseAdapter {
	    private Context mContext;

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

	    public int getCount() {
	        return album.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.photo_album_element, null);
	        	
	   
	        } else {
	        	view = convertView;
	        }
	    
	        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
	        
	        
	        if (album.photoRecordArr.get(position) != null)
			{
				ImageManager.getInstance().ApplySmallImageToImageView(imageView, album.photoRecordArr.get(position).imageUrl);
			}
			else
			{
				ImageManager.getInstance().ApplySmallImageToImageView(imageView, "R.drawable.no_picture");
			}
	        
	        return view;
	    }

	}
	
	
}