package csu.matos.actionBar;

import java.util.Date;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

public class Fragment2 extends Fragment implements OnItemClickListener  {
	EditText txtMsg;
	Button btnGo;
	Integer[] smallImages = { 
			R.drawable.pic01_small, R.drawable.pic02_small, R.drawable.pic03_small, 
			R.drawable.pic04_small, R.drawable.pic05_small, R.drawable.pic06_small,
			R.drawable.pic07_small, R.drawable.pic08_small, R.drawable.pic09_small,
			R.drawable.pic10_small, R.drawable.pic11_small, R.drawable.pic12_small,
			R.drawable.pic13_small, R.drawable.pic14_small, R.drawable.pic15_small };
	Integer selectedRow;
	
	
	public Fragment2(Integer selectedRow) {
		super();
		this.selectedRow = selectedRow;
	}

	// this view is inflated using an XML layout file
	@Override
	public View onCreateView(LayoutInflater inflater, 
			ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.gridview, null);
		GridView listView = (GridView) view.findViewById(R.id.mainGrid);
		
		txtMsg = (EditText) view.findViewById(R.id.editText1);
		
		btnGo = (Button) view.findViewById(R.id.button1);
		btnGo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String text = new Date().toString();
				txtMsg.setText("NEW " + text);
			}
		});
		
		
		listView.setAdapter(new Adapter( getActivity() ));
		listView.setOnItemClickListener(this);
		
		// tell here what picture was already selected in fragment1
		String text = "User selected from Listview (Fragment1)\nrow: " 
		            + selectedRow;
		Toast.makeText(getActivity(), text, 1).show();
		
		return view;
	}
	
	// //////////////////////////////////////////////////////////////
	private class Adapter extends BaseAdapter {
		Context ctx;
		public Adapter(Context ctx){
			this.ctx = ctx;
		}
		@Override
		public int getCount() {
			return smallImages.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, 
				View convertView, 
				ViewGroup parent) {
			ImageView image;
			if (convertView == null) {
				image = new ImageView(Fragment2.this.getActivity());
				image.setLayoutParams(new GridView.LayoutParams(50, 50));
				image.setScaleType(ScaleType.FIT_XY);
				convertView = image;
			} else {
				image = (ImageView) convertView;
			}

			txtMsg.setText("Position: " + position);
			image.setImageResource(smallImages[position]); 
			return image;
		}

	}//ViewAdapter

	// /////////////////////////////////////////////////////////////
	//TODO: repeat strategy used in fragment1, when user clicks
	//		on image let the callback method in main activity 
	//		know what image (position) has been selected 
	
	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		txtMsg.setText("Position selected " + position);

	}//onItemClick
	
	// /////////////////////////////////////////////////////////////
	public void updateItemFromList1(Integer selectedRow ){
		txtMsg.setText("User chose in LISTVIEW row#: " + selectedRow );
	}


}//Activity
