package com.uwflow.flow_android;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.facebook.Session;
import com.uwflow.flow_android.adapters.NavDrawerAdapter;
import com.uwflow.flow_android.constant.Constants;
import com.uwflow.flow_android.entities.NavDrawerItem;
import com.uwflow.flow_android.fragment.AboutFragment;
import com.uwflow.flow_android.fragment.ExploreFragment;
import com.uwflow.flow_android.fragment.ProfileFragment;
import com.uwflow.flow_android.fragment.ShortlistFragment;

import java.util.ArrayList;

public class MainFlowActivity extends FlowActivity {
    private ListView mDrawerList;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private NavDrawerAdapter mNavDrawerAdapter;
    private ArrayList<NavDrawerItem> mDrawerItems;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.flow_main);

        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawerItems = new ArrayList<NavDrawerItem>();
        mDrawerItems.add(new NavDrawerItem("Profile", R.drawable.drawer_profile_icon));
        mDrawerItems.add(new NavDrawerItem("Explore", R.drawable.drawer_explore_icon));
        mDrawerItems.add(new NavDrawerItem("Shortlist", R.drawable.drawer_shortlist_icon));
        mDrawerItems.add(new NavDrawerItem("About", R.drawable.drawer_about_icon));
        mDrawerItems.add(new NavDrawerItem("Log out", R.drawable.drawer_log_out_icon));

        mNavDrawerAdapter = new NavDrawerAdapter(mDrawerItems, this);
        mDrawerList.setAdapter(mNavDrawerAdapter);

        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                R.drawable.ic_drawer_am,
                R.string.drawer_open,
                R.string.drawer_close
        ) {
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);

            }
            public void onDrawerOpened(View view) {
                super.onDrawerOpened(view);
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        Fragment profileFrag = new ProfileFragment();

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .add(R.id.content_frame, profileFrag)
                .commit();

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        mDrawerList.setItemChecked(0, true);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        getActionBar().setTitle(title);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action buttons
        switch(item.getItemId()) {
            case R.id.action_search:
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.content_frame, new ExploreFragment())
                        .addToBackStack(null)
                        .commit();
                // TODO: mark the Explore page as checked in the nav drawer
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /** Swaps fragments in the main content view */
    private void selectItem(int position) {
        // Create a new fragment and specify the planet to show based on position
        Fragment fragment = null;
        switch (position){
            case(Constants.NAV_DRAWER_PROFILE_INDEX) :
                fragment = new ProfileFragment();
                break;
            case(Constants.NAV_DRAWER_EXPLORE_INDEX) :
                fragment = new ExploreFragment();
                break;
            case(Constants.NAV_DRAWER_SHORTLIST_INDEX) :
                // TODO: implement ShortlistFragment
//                fragment = new ShortlistFragment();
                fragment = new ProfileFragment();
                Bundle bundle = new Bundle();
                bundle.putInt(Constants.TAB_ID, Constants.PROFILE_COURSES_PAGE_INDEX);
                fragment.setArguments(bundle);
                break;
            case(Constants.NAV_DRAWER_ABOUT_INDEX) :
                fragment = new AboutFragment();
                break;
            case(Constants.NAV_DRAWER_LOG_OUT_INDEX) :
                // Log out (clear Facebook session)
                Session session = Session.getActiveSession();
                if (session != null) {
                    if (!session.isClosed()) {
                        session.closeAndClearTokenInformation();
                    }
                } else {
                    session = new Session(getApplicationContext());
                    Session.setActiveSession(session);
                    session.closeAndClearTokenInformation();
                }
                return;
        }

        if (fragment != null){
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, fragment)
                    .addToBackStack(null)
                    .commit();
            mDrawerList.setItemChecked(position, true);
            mDrawerLayout.closeDrawer(mDrawerList);
        }
    }
}

@@ -27,16 +27,47 @@
 droid;%0A%0A
+import android.content.Intent;%0A
 import a
@@ -5920,257 +5920,253 @@
     
-// Log out (clear Facebook session)%0A                Session session = Session.getActiveSession();%0A                if (session != null) %7B%0A                    if (!session.isClosed()) %7B%0A                        session.closeAndClearTokenInformation(
+if (Session.getActiveSession() != null) %7B%0A                    Session.getActiveSession().closeAndClearTokenInformation();%0A                %7D%0A                Session.setActiveSession(null);%0A                mDrawerLayout.closeDrawer(mDrawerList
 );%0A
+%0A
     
@@ -6181,220 +6181,384 @@
     
-    %7D%0A                %7D else %7B%0A                    session = new Session(getApplicationContext());%0A                    Session.setActiveSession(session);%0A                    session.closeAndClearTokenInformation(
+Intent intent = new Intent(this, LoginActivity.class);%0A                intent.putExtra(%22finish%22, true); // if you are checking for this in your other Activities%0A                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP %7C%0A                        Intent.FLAG_ACTIVITY_CLEAR_TASK %7C%0A                        Intent.FLAG_ACTIVITY_NEW_TASK);%0A                startActivity(intent
 );%0A 
@@ -6564,33 +6564,41 @@
                 
-%7D
+finish();
 %0A               

Return user to login screen after fb logout