package fishrungames.bashgid;

import java.util.ArrayList;

import fishrungames.bashgid.core.AlbumManager;

import fishrungames.networkutils.ImageManager;
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.view.View.OnClickListener;
import android.widget.BaseAdapter;

import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;


public class PhotoAlbumListFragment extends Fragment
{
	
	ArrayList<AlbumManager.AlbumStruct> albumArr;
	
	ListAdapter listAdapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		View rootView = inflater.inflate(R.layout.fragment_photo_album_list_page, container, false);

		albumArr = AlbumManager.getInstance().getAlbums();
		
		listAdapter = new ListAdapter(getActivity());
		
		ListView listView = (ListView) rootView.findViewById(R.id.listView);
		
		listView.setAdapter(listAdapter);
		
		return rootView;
	}
	
	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.photo_album_list_element, null);
	        	
	   
	        } else {
	        	view = convertView;
	        }
	        
	        
	        ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton);
	        
	        ImageManager.getInstance().ApplyImageToImageView(imageButton, albumArr.get(position).imageIds.get(0));

	        imageButton.setOnClickListener(new ButtonOnClickListener(albumArr.get(position)));
	        
	        TextView textView = (TextView)view.findViewById(R.id.textView);
	        
	        textView.setText(albumArr.get(position).name);
	        
	        return view;
	    }

	}
	
	
	public static class ButtonOnClickListener implements OnClickListener
	{
		
		AlbumManager.AlbumStruct album;
		
		public ButtonOnClickListener(AlbumManager.AlbumStruct album)
		{
			this.album = album;
		}

		@Override
		public void onClick(View v)
		{
			MainActivity.getInstance().OpenPhotoAlbumScreen(album);
		}
		
	}
}

