package com.cafejeunesse.android.navigationdrawer;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.cafejeunesse.android.fragment.BasicFragment;
import com.cafejeunesse.android.fragment.cafefragment.CafeFragment;
import com.cafejeunesse.android.fragment.homefragment.HomeFragment;
import com.cafejeunesse.android.fragment.mapfragment.GoogleMapFragment;
import com.cafejeunesse.android.fragment.servicefragment.ServiceFragment;

/**
 * Created by David Levayer on 17/02/15.
 */
public class NavigationDrawerActivity extends Activity implements AdapterView.OnItemClickListener {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private Fragment[] mFragments;
    private String[] mMenuTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // enable ActionBar app icon to behave as action to toggle nav drawer
        final ActionBar mActionBar = getActionBar();
        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setDisplayHomeAsUpEnabled(true);
        mActionBar.setHomeButtonEnabled(true);

        setContentView(R.layout.activity_navigation_drawer);

        mFragments = new Fragment[BasicFragment.FRAGMENT_NUMBER];
        mFragments[BasicFragment.HOMEFRAGMENT_ID] = new HomeFragment();
        mFragments[BasicFragment.CAFEFRAGMENT_ID] = new CafeFragment();
        mFragments[BasicFragment.SERVICEFRAGMENT_ID] = ServiceFragment.newInstance(this);
        mFragments[BasicFragment.GOOGLEMAPFRAGMENT_ID] = new GoogleMapFragment();

        mMenuTitles = getResources().getStringArray(R.array.menu_titles_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);


        View mDrawerImage = getLayoutInflater().inflate(R.layout.activity_drawer_image,null);
        mDrawerList.addHeaderView(mDrawerImage);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        NavDrawerItem[] mItems = new NavDrawerItem[mMenuTitles.length];
        for(int i=0; i<mMenuTitles.length; i++){
            mItems[i] = new NavDrawerItem(i,mMenuTitles[i]);
        }

        mDrawerList.setAdapter(new DrawerAdapter(this,R.layout.activity_list_item_drawer,mItems));

        mDrawerList.setOnItemClickListener(this);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.string.drawer_open, R.string.drawer_close) {

            private boolean shouldHide = true;
            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                mActionBar.setIcon(R.drawable.actionbar_icon);
                shouldHide = true;
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
                if(shouldHide){
                    mActionBar.setIcon(android.R.color.transparent);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                    shouldHide = false;
                }
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(1);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.navigation_drawer, menu);
        return true;
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @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_websearch:
                // create intent to perform web search for this planet
                Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
                intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
                // catch event that there's no activity to handle intent
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivity(intent);
                } else {
                    Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }

    private void selectItem(int position) {

        if(position == 0)
            return;

        Fragment fragment = mFragments[position-1];

        if(fragment == null){
            switch(position){
                case BasicFragment.HOMEFRAGMENT_ID+1:
                    mFragments[position] = new HomeFragment();
                    break;
                case BasicFragment.CAFEFRAGMENT_ID+1:
                    mFragments[position] = new CafeFragment();
                    break;
                case BasicFragment.SERVICEFRAGMENT_ID+1:
                    mFragments[position] = new ServiceFragment();
                    break;
                case BasicFragment.GOOGLEMAPFRAGMENT_ID+1:
                    mFragments[position] = new GoogleMapFragment();
            }
            fragment = mFragments[position];
        }

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();

        // update selected item title, then close the drawer
        //setTitle(mMenuTitles[position]);
        mDrawerList.setItemChecked(position,true);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    /**
     * 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);
    }

    @Override
    public void onBackPressed() {
        if(getFragmentManager().getBackStackEntryCount() == 0) {
            super.onBackPressed();
        }
        else {
            getFragmentManager().popBackStack();
        }
    }

    // Autres trucs à faire

    // TODO Créer un fragment pour les Paramètres
    // TODO Créer un fragment pour le "A propos"
}

@@ -6663,20 +6663,16 @@
 ition%5D =
- new
  Service
@@ -6671,33 +6671,49 @@
  ServiceFragment
-(
+.newInstance(this
 );%0A             

update of the selectItem method for service loading improvement