package it.unipr.mobdev;

import it.unipr.dsg.mobdev.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

	public static final String TAG = "BookmarkManager04";
	
    /**
     * 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;

	private MainActivity mContext = null;

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

        // 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);       
        
        //UnComment the following lines to 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);
            }
        });
  	
        //Create a new Tab associated to an icon and a title
    	Tab topTab = actionBar.newTab();
    	topTab.setText(mAppSectionsPagerAdapter.getPageTitle(1)).setTabListener(this);
    	topTab.setIcon(R.drawable.ic_action_rating_not_important);
    	
    	//Add the new tab to the ActionBar
    	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) {
    }
    
    //
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_add:
                openAddBookmarkActivity();
            	return true;
            case R.id.action_info:
            	openAppInfoActivity();
            	return true;
            case R.id.action_export:
            	openExportDialog();
            	return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
	private void openAppInfoActivity()
	{
		Intent newIntent = new Intent(getApplicationContext(), AppInfoWebViewActivity.class);
		startActivity(newIntent);
	}
    
    private void openAddBookmarkActivity()
    {
    	Intent newIntent = new Intent(this, AddEditBookmarkActivity.class);
		startActivity(newIntent);
    }
    
	private void openExportDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Do you want to export your Bookmark List ?")
		.setCancelable(false)
		.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
				if(!BookmarkManager.getInstance(mContext).exportStoredBookMarkList())
					Toast.makeText(mContext, "Unable to Export Bookmark List !", Toast.LENGTH_LONG).show();
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
}
