package it.unipr.dsg.mobdev;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {

	public static final String TAG = "ADSource3";
	
    /**
     * The android.support.v4.view.PagerAdapter that will provide fragments for each of the
     * three primary sections of the app. We use a android.support.v4.app.FragmentPagerAdapter
     * derivative, which will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a android.support.v4.app.FragmentStatePagerAdapter.
     */
    private AppSectionsPagerAdapter mAppSectionsPagerAdapter;

    /**
     * The ViewPager that will display the three primary sections of the app, one at a
     * time.
     */
    private ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        // Set up the action bar.
        final ActionBar actionBar = getSupportActionBar();
        
        // Specify that the Home/Up button should not be enabled, since there is no hierarchical
        // parent.
        actionBar.setHomeButtonEnabled(false);        
        
        //Uncommenting the following lines we can hide the top part of the action bar
        //actionBar.setDisplayShowTitleEnabled(false);
        //actionBar.setDisplayHomeAsUpEnabled(false);
        //actionBar.setDisplayShowHomeEnabled(false);
        
        // Specify that we will be displaying tabs in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        
        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if we have a reference to the
                // Tab.
                actionBar.setSelectedNavigationItem(position);
            }
        });

        //Add tabs to the ActionBar
        Tab addTab = actionBar.newTab();
        addTab.setText(mAppSectionsPagerAdapter.getPageTitle(0)).setTabListener(this);
        addTab.setIcon(R.drawable.ic_action_content_new_event);
    	actionBar.addTab(addTab);
    	
    	Tab topTab = actionBar.newTab();
    	topTab.setText(mAppSectionsPagerAdapter.getPageTitle(1)).setTabListener(this);
    	topTab.setIcon(R.drawable.ic_action_rating_not_important);
    	actionBar.addTab(topTab);
    	
    	Tab myTab = actionBar.newTab();
    	myTab.setText(mAppSectionsPagerAdapter.getPageTitle(2)).setTabListener(this);
    	myTab.setIcon(R.drawable.ic_action_collections_view_as_list);
    	actionBar.addTab(myTab);
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return new AddBookmarkFragment();
                case 1:
                    return new BookmarkGridFragment();
                case 2:
                    return new BookmarkListFragment();    
                default:
                    return new AddBookmarkFragment();
            }
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
        	 switch (position) {
             case 0:
                 return "Add";
             case 1:
                 return "Top";
             case 2:
                 return "My List";    
             default:
                 return "No Title !";
         }
        }
    }
}
