package org.cobaltians.sidemenu.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.MenuItem;

import fr.cobaltians.cobalt.Cobalt;
import fr.cobaltians.cobalt.activities.CobaltActivity;
import fr.cobaltians.cobalt.fragments.CobaltFragment;

import org.cobaltians.sidemenu.R;
import org.cobaltians.sidemenu.fragments.DefaultFragment;

public class WithSidemenuActivity extends CobaltActivity {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;

    /***********************************************************************************************
     *
     * LIFECYCLE
     *
     **********************************************************************************************/

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this,
                mDrawerLayout,
                R.string.open_drawer,
                R.string.close_drawer);

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

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

        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    /***********************************************************************************************
     *
     * MENU
     *
     **********************************************************************************************/

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle.
        // if it returns true, then it has handled the app icon touch event
        return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
    }

    /***********************************************************************************************
     *
     * COBALT
     *
     **********************************************************************************************/

    @Override
    protected int getLayoutToInflate() {
        return R.layout.activity_sidemenu;
    }

    @Override
    public String getInitController() {
        return "withSidemenu";
    }

    @Override
    public String getInitPage() {
        return "home.html";
    }

    @Override
    protected CobaltFragment getFragment() {
        return Cobalt.getInstance(this).getFragmentForController(DefaultFragment.class,
                "withSidemenu",
                "home.html");
    }

    /******************************************************
     * HELPERS
     ******************************************************/

    public void setDrawerState (boolean isEnabled) {
        if (isEnabled) {
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            mDrawerToggle.setDrawerIndicatorEnabled(true);
            mDrawerToggle.syncState();
        }
        else {
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            mDrawerToggle.setDrawerIndicatorEnabled(false);
            mDrawerToggle.syncState();
        }
    }

    public void setDrawerVisible (final boolean isShow) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isShow && !mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                    mDrawerLayout.openDrawer(GravityCompat.START);
                }
                else if (!isShow && mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                    mDrawerLayout.closeDrawers();
                }
            }
        });
    }

    public void setDrawerToggle() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                    mDrawerLayout.closeDrawers();
                }
                else mDrawerLayout.openDrawer(GravityCompat.START);
            }
        });
    }

    public void switchFragment(final Fragment fragment) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();

                mDrawerLayout.closeDrawers();
            }
        });
    }
}

@@ -287,28 +287,111 @@
 ctionBar
-DrawerToggle
+;%0Aimport android.support.v7.app.ActionBarDrawerToggle;%0Aimport android.support.v7.widget.Toolbar
 ;%0Aimport
@@ -1146,32 +1146,391 @@
 nstanceState);%0A%0A
+        Toolbar topBar = (Toolbar) findViewById(R.id.top_bar);%0A        setSupportActionBar(topBar);%0A        ActionBar actionBar = getSupportActionBar();%0A        if (actionBar != null) %7B%0A            actionBar.setDisplayHomeAsUpEnabled(true);%0A            actionBar.setHomeButtonEnabled(true);%0A            actionBar.setDisplayShowTitleEnabled(false);%0A        %7D%0A%0A
         mDrawerL

add toolbar visible