package fishrungames.bashgid;


import android.annotation.SuppressLint;
import android.content.Context;
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;
	String albumName;
	
	ListAdapter listAdapter;
	
	ProgressBar progressBar;
	
	public PhotoAlbumFragment()
	{
		album = null;
		albumName = "";
	}
	
	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) {
		
		if (savedInstanceState != null) {
			albumName = savedInstanceState.getString("albumName");
			RestoreAlbum();
        }
		
		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)
			{
				MainActivity.getInstance().SetLastAlbumName(albumName);
				MainActivity.getInstance().OpenPhotoScreen(album, position, MainActivity.TAG_BACK_TO_ALBUMFRAGMENT);
				
			}
		
		});
		
		return rootView;
	}
	
	void RestoreAlbum()
	{
		album = AlbumManager.CreateEmptyAlbum();
		
		DatabaseGetAlbumTask task = new DatabaseGetAlbumTask();

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

		task.execute(albumName);
	}
	
	public void SetAlbum(AlbumFullData album)
	{
		progressBar.setVisibility(View.GONE);

		this.album = album;
		
		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);
	        
	        ImageManager.getInstance().ApplyImageToImageView(imageView, album.photoRecordArr.get(position).imageUrl);
	        
	        return view;
	    }

	}
	
	
}