package com.nestedworld.nestedworld.ui.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Trace;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import butterknife.ButterKnife;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

/**
 * Abstract class for AppCompatActivity
 * it make a little abstraction of the life cycle
 * it add a butterKnife support and some useful field/method
 */
public abstract class BaseAppCompatActivity extends AppCompatActivity {
    protected final String TAG = getClass().getSimpleName();

    /*
    ** Method that every child will have to implement
     */

    /**
     * get the layout id
     * it will be use under onCreate()
     * as setContentView(getLayoutResource());
     */
    protected abstract int getLayoutResource();

    /**
     * init the activity here
     * This method is equivalent to onCreate()
     */
    protected abstract void init(Bundle savedInstanceState);

    /*
    ** Life cycle
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getLayoutResource());
        ButterKnife.bind(this);

        try {
            Trace.beginSection(TAG + " init");
            init(savedInstanceState);
        } finally {
            Trace.endSection();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case android.R.id.home:
                if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                    getSupportFragmentManager().popBackStack();
                } else {
                    super.onBackPressed();
                }
                return true;
            default:
                return (super.onOptionsItemSelected(menuItem));
        }
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        //We override the font
        //See https://github.com/chrisjenx/Calligraphy
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    /*
    ** Utils
     */
    public String toString() {
        return TAG;
    }

    protected void startActivity(Class clazz, Bundle bundle) {
        try {
            final Intent intent = new Intent(this, clazz);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected void startActivity(Class clazz) {
        startActivity(clazz, null);
    }
}

@@ -153,16 +153,148 @@
 .Trace;%0A
+import android.support.annotation.LayoutRes;%0Aimport android.support.annotation.NonNull;%0Aimport android.support.annotation.Nullable;%0A
 import a
@@ -942,32 +942,47 @@
 rce());%0A     */%0A
+    @LayoutRes%0A
     protected ab
@@ -1140,16 +1140,26 @@
 id init(
+@Nullable 
 Bundle s
@@ -1254,16 +1254,26 @@
 nCreate(
+@Nullable 
 Bundle s
@@ -2445,32 +2445,47 @@
 d startActivity(
+@NonNull final 
 Class clazz, Bun
@@ -2480,16 +2480,26 @@
 s clazz,
+ @Nullable
  Bundle 
@@ -2508,34 +2508,16 @@
 ndle) %7B%0A
-        try %7B%0A    
         
@@ -2567,28 +2567,24 @@
 z);%0A        
-    
 if (bundle !
@@ -2605,20 +2605,16 @@
         
- 
-   
 intent.p
@@ -2639,32 +2639,26 @@
         
-    %7D%0A  
+%7D%0A
         
   startA
@@ -2645,26 +2645,24 @@
   %7D%0A        
-  
 startActivit
@@ -2676,85 +2676,8 @@
 t);%0A
-        %7D catch (Exception ex) %7B%0A            ex.printStackTrace();%0A        %7D%0A
     
@@ -2712,16 +2712,31 @@
 ctivity(
+@NonNull final 
 Class cl

improve baseActivity