package com.peterjosling.scroball;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.google.common.collect.ImmutableList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

  private ScroballApplication application;

  /**
   * The {@link android.support.v4.view.PagerAdapter} that will provide
   * fragments for each of the sections. We use a
   * {@link FragmentPagerAdapter} derivative, which will keep every
   * loaded fragment in memory. If this becomes too memory intensive, it
   * may be best to switch to a
   * {@link android.support.v4.app.FragmentStatePagerAdapter}.
   */
  private SectionsPagerAdapter mSectionsPagerAdapter;

  /**
   * The {@link ViewPager} that will host the section contents.
   */
  private ViewPager mViewPager;

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

    application = (ScroballApplication) getApplication();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
  }


  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.settings_item:
        Intent intent = new Intent(getBaseContext(), SettingsActivity.class);
        startActivityForResult(intent, 1);
        return true;
      case R.id.logout_item:
        logout();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  public void logout() {
    new AlertDialog.Builder(this)
        .setTitle(R.string.are_you_sure)
        .setMessage(R.string.logout_confirm)
        .setPositiveButton(android.R.string.yes, (dialog, whichButton) -> {
          SharedPreferences preferences = application.getSharedPreferences();
          SharedPreferences.Editor editor = preferences.edit();
          editor.remove(getString(R.string.saved_session_key));
          editor.apply();

          application.getScroballDB().clear();
          application.getLastfmClient().clearSession();

          Intent intent = new Intent(this, SplashScreen.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(intent);
          finish();
        })
        .setNegativeButton(android.R.string.no, null).show();
  }

  /**
   * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
   * one of the sections/tabs/pages.
   */
  public class SectionsPagerAdapter extends FragmentPagerAdapter {

    List<Fragment> fragments = ImmutableList.of(
        new NowPlayingFragment(), new ScrobbleHistoryFragment());

    public SectionsPagerAdapter(FragmentManager fm) {
      super(fm);
    }

    @Override
    public Fragment getItem(int position) {
      // getItem is called to instantiate the fragment for the given page.
      // Return a PlaceholderFragment (defined as a static inner class below).
      return fragments.get(position);
    }

    @Override
    public int getCount() {
      return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
      switch (position) {
        case 0:
          return "Now Playing";
        case 1:
          return "History";
      }
      return null;
    }
  }
}

@@ -786,21 +786,16 @@
  provide
-%0A   *
  fragmen
@@ -808,24 +808,29 @@
  each of the
+%0A   *
  sections. W
@@ -836,21 +836,16 @@
 We use a
-%0A   *
  %7B@link 
@@ -903,20 +903,20 @@
 very
-%0A   *
  loaded
+%0A   *
  fra
@@ -972,21 +972,16 @@
 sive, it
-%0A   *
  may be 
@@ -1003,20 +1003,20 @@
 to a
-%0A   *
  %7B@link
+%0A   *
  and
@@ -1121,37 +1121,32 @@
 rAdapter;%0A%0A  /**
-%0A   *
  The %7B@link View
@@ -1188,19 +1188,16 @@
 ontents.
-%0A  
  */%0A  pr
@@ -1461,18 +1461,8 @@
 ar =
- (Toolbar)
  fin
@@ -1796,20 +1796,8 @@
 er =
- (ViewPager)
  fin
@@ -1903,20 +1903,8 @@
 ut =
- (TabLayout)
  fin
@@ -1976,17 +1976,16 @@
 );%0A  %7D%0A%0A
-%0A
   @Overr
@@ -2696,32 +2696,45 @@
 tPositiveButton(
+%0A            
 android.R.string
@@ -2738,16 +2738,28 @@
 ing.yes,
+%0A           
  (dialog
@@ -2778,16 +2778,20 @@
 n) -%3E %7B%0A
+    
         
@@ -2856,24 +2856,28 @@
 ferences();%0A
+    
           Sh
@@ -2934,24 +2934,28 @@
 ;%0A          
+    
 editor.remov
@@ -3002,24 +3002,28 @@
 ;%0A          
+    
 editor.apply
@@ -3029,32 +3029,36 @@
 y();%0A%0A          
+   
+ 
 application.getS
@@ -3074,24 +3074,28 @@
 ().clear();%0A
+    
           ap
@@ -3141,32 +3141,36 @@
 n();%0A%0A          
+ 
+   
 Intent intent = 
@@ -3217,16 +3217,20 @@
         
+  
+  
 intent.a
@@ -3272,32 +3272,36 @@
 TOP);%0A          
+    
 startActivity(in
@@ -3317,16 +3317,20 @@
         
+    
 finish()
@@ -3339,16 +3339,20 @@
         
+ 
+   
 %7D)%0A     
@@ -3399,16 +3399,25 @@
 o, null)
+%0A        
 .show();
@@ -3504,21 +3504,16 @@
 nding to
-%0A   *
  one of 
@@ -3511,24 +3511,29 @@
 o one of the
+%0A   *
  sections/ta
@@ -3646,16 +3646,24 @@
 gments =
+%0A       
  Immutab
@@ -3672,25 +3672,16 @@
 List.of(
-%0A        
 new NowP

Clean up MainActivity