package com.team980.thunderscout;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
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.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.team980.thunderscout.info.ThisDeviceFragment;
import com.team980.thunderscout.preferences.SettingsActivity;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    public static String INTENT_FLAG_SHOWN_FRAGMENT = "SHOWN_FRAGMENT";
    public static int INTENT_FLAGS_HOME = 0;
    public static int INTENT_FLAGS_THIS_DEVICE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        int shownFragment = getIntent().getIntExtra(INTENT_FLAG_SHOWN_FRAGMENT, INTENT_FLAGS_HOME);

        Fragment fragment;

        switch (shownFragment) {
            case 0: //INTENT_FLAGS_HOME
                navigationView.setCheckedItem(R.id.nav_match_scout);
                fragment = new HomeFragment();
                break;
            case 1: //INTENT_FLAGS_THIS_DEVICE
                navigationView.setCheckedItem(R.id.nav_local_storage);
                fragment = new ThisDeviceFragment();
                break;
            default: //default to INTENT_FLAGS_HOME
                navigationView.setCheckedItem(R.id.nav_match_scout);
                fragment = new HomeFragment();
                break;
        }

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fragment, fragment);
        ft.addToBackStack(null);
        ft.commit();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_match_scout) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.fragment, new HomeFragment());
            ft.addToBackStack(null);
            ft.commit();
        } else if (id == R.id.nav_local_storage) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.fragment, new ThisDeviceFragment());
            ft.addToBackStack(null);
            ft.commit();
        } else if (id == R.id.nav_settings) {
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
        } else if (id == R.id.nav_about) {
            Intent intent = new Intent(this, AboutActivity.class);
            startActivity(intent);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

@@ -371,16 +371,41 @@
 tivity;%0A
+import android.util.Log;%0A
 import a
@@ -932,24 +932,64 @@
 nceState) %7B%0A
+        Log.d(%22FRAGSTATE%22, %22onCreate%22);%0A
         supe
@@ -1349,16 +1349,310 @@
 gment;%0A%0A
+        if (savedInstanceState != null) %7B%0A            //Restore the fragment's instance%0A            fragment = getSupportFragmentManager().getFragment(savedInstanceState, %22mContent%22);%0A            Log.d(%22FRAGSTATE%22, %22restoring%22);%0A        %7D else %7B%0A            Log.d(%22FRAGSTATE%22, %22new frag%22);%0A    
         
@@ -1672,24 +1672,28 @@
 Fragment) %7B%0A
+    
             
@@ -1728,32 +1728,36 @@
                 
+ 
+   
 navigationView.s
@@ -1801,32 +1801,36 @@
                 
+    
 fragment = new H
@@ -1852,32 +1852,36 @@
                 
+    
 break;%0A         
@@ -1879,24 +1879,28 @@
             
+   
+ 
 case 1: //IN
@@ -1922,16 +1922,20 @@
 _DEVICE%0A
+    
         
@@ -2005,32 +2005,36 @@
                 
+    
 fragment = new T
@@ -2046,32 +2046,36 @@
 viceFragment();%0A
+    
                 
@@ -2093,16 +2093,20 @@
         
+    
 default:
@@ -2129,32 +2129,36 @@
 TENT_FLAGS_HOME%0A
+    
                 
@@ -2218,32 +2218,36 @@
                 
+    
 fragment = new H
@@ -2269,32 +2269,36 @@
                 
+  
+  
 break;%0A        %7D
@@ -2288,33 +2288,124 @@
  break;%0A        
-%7D
+    %7D%0A        %7D%0A%0A        Log.d(%22FRAGSTATE%22, %22frag class: %22 + fragment.getClass().getName());
 %0A%0A        Fragme
@@ -2569,32 +2569,467 @@
 ommit();%0A    %7D%0A%0A
+    @Override%0A    protected void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A%0A        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment);%0A%0A        Log.d(%22FRAGSTATE%22, %22saving%22);%0A        Log.d(%22FRAGSTATE%22, %22class: %22 + fragment.getClass());%0A%0A        //Save the fragment's instance%0A        getSupportFragmentManager().putFragment(outState, %22mContent%22, fragment);%0A    %7D%0A%0A
     @Override%0A  

Store fragment state in main activity!