package csu.matos.actionBar;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.Toast;
import csu.matos.actionBar.Fragment1.OnPictureSelectedListener;

public class ActionBarMain extends Activity implements TabListener, 
	OnPictureSelectedListener  {
	
	// data shared by fragments 1 & 2
	Integer selectedRow = 0;
		
	RelativeLayout mainLayout;
	
	FragmentTransaction fragTransactMgr = null;

	// tab's captions
	private final String CAPTION1 = "ListView";
	private final String CAPTION2 = "GridView";
	private final String CAPTION3 = "ImageView";

	// -----------------------------------------------------------

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_action_bar_main);
		try {
			mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);

			fragTransactMgr = getFragmentManager().beginTransaction();
			
			ActionBar bar = getActionBar();

			//create tabs adding caption and icon 
			bar.addTab(bar.newTab().setText(CAPTION1)
								   .setIcon(R.drawable.ic_4_collections_view_as_list)
								   .setTabListener(this));
			bar.addTab(bar.newTab().setText(CAPTION2)
								   .setIcon(R.drawable.ic_4_collections_view_as_grid)
								   .setTabListener(this));
			bar.addTab(bar.newTab().setText(CAPTION3)
					               .setIcon(R.drawable.ic_5_content_picture)
					               .setTabListener(this));

			bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
					|             ActionBar.DISPLAY_USE_LOGO);

			bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

			bar.setDisplayShowHomeEnabled(true);
			bar.setDisplayShowTitleEnabled(false);
			bar.show();

		} catch (Exception e) {
			e.getMessage();
		}		
	}//onCreate

	// ////////////////////////////////////////////////////////////
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_action_bar_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Toast.makeText(this, "Option selected: " + item.getTitle(), 
				       Toast.LENGTH_LONG).show(); 
		return true;
	}

	// ////////////////////////////////////////////////////////////
	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {

		if (tab.getText().equals(CAPTION1)) {
			executeFragment( new Fragment1() );
		} else if (tab.getText().equals(CAPTION2)) {
			executeFragment( new Fragment2(selectedRow) );
		} else if (tab.getText().equals(CAPTION3)) {
			executeFragment( new Fragment3(selectedRow) );
		}

	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {

	}
	
	// //////////////////////////////////////////////////////////////
	public void executeFragment (Fragment fragment) {
		try {			 
			mainLayout.removeAllViews();
			fragTransactMgr.addToBackStack(null);
			fragTransactMgr = getFragmentManager().beginTransaction();
			fragTransactMgr.add(mainLayout.getId(), fragment);
			fragTransactMgr.commit();			
		} catch (Exception e) {
			
		}
	}//executeFragment

	// ///////////////////////////////////////////////////////////////
	// this method supports fragment-to-Activity communication. When 
	// a row in Fragment1 is selected, this callBack is invoked. It 
	// updates the valued of 'selectedRow' held in the main activity. 
		
	@Override
	public void onPictureSelected(Integer selectedRow) {
		// as soon as the user picks a row in fragment1, 
		// its value (position in the list) is saved here
		this.selectedRow = selectedRow;  
		
	}
	
}//class
