package com.jamieadkins.gwent.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.jamieadkins.gwent.ComingSoonFragment;
import com.jamieadkins.gwent.R;
import com.jamieadkins.gwent.base.AuthenticationActivity;
import com.jamieadkins.gwent.data.interactor.DecksInteractorFirebase;
import com.jamieadkins.gwent.deck.DecksContract;
import com.jamieadkins.gwent.deck.DecksPresenter;
import com.jamieadkins.gwent.deck.DeckListFragment;

public class MainActivity extends AuthenticationActivity {

    private DecksPresenter mDecksPresenter;
    private int mCurrentTab;

    private final View.OnClickListener signInClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (isPlayServicesAvailable()) {
                startSignInProcess();
            }
        }
    };

    @Override
    public void initialiseContentView() {
        setContentView(R.layout.activity_main);
    }

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

        BottomNavigationView bottomNavigationView = (BottomNavigationView)
                findViewById(R.id.bottom_navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        FragmentManager fragmentManager = getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        Fragment fragment;
                        switch (item.getItemId()) {
                            case R.id.tab_decks:
                                // Stop authenticated only tabs from being selected.
                                if (!isAuthenticated()) {
                                    showSnackbar(
                                            String.format(getString(R.string.sign_in_only),
                                                    getString(R.string.your_decks)),
                                            getString(R.string.sign_in),
                                            signInClickListener);

                                    // Don't display the item as the selected item.
                                    return false;
                                }

                                // Else, if authenticated.
                                fragment = new DeckListFragment();
                                fragmentTransaction.replace(R.id.contentContainer, fragment, "decks");

                                // Create the presenter.
                                mDecksPresenter = new DecksPresenter((DecksContract.View) fragment,
                                        new DecksInteractorFirebase());
                                break;
                            case R.id.tab_collection:
                                // Stop authenticated only tabs from being selected.
                                if (!isAuthenticated()) {
                                    showSnackbar(
                                            String.format(getString(R.string.sign_in_only),
                                                    getString(R.string.your_collections)),
                                            getString(R.string.sign_in),
                                            signInClickListener);

                                    // Don't display the item as the selected item.
                                    return false;
                                }

                                // Else, if authenticated.
                                fragment = new ComingSoonFragment();
                                fragmentTransaction.replace(R.id.contentContainer, fragment, "collection");
                                break;
                            case R.id.tab_public_decks:
                                fragment = new ComingSoonFragment();
                                fragmentTransaction.replace(R.id.contentContainer, fragment, "public");
                                break;
                            case R.id.tab_card_db:
                                fragment = new ComingSoonFragment();
                                fragmentTransaction.replace(R.id.contentContainer, fragment, "cards");
                                break;
                        }

                        fragmentTransaction.commit();
                        mCurrentTab = item.getItemId();
                        return true;
                    }
                });
    }

    @Override
    protected void onSignedIn() {
        super.onSignedIn();
        invalidateOptionsMenu();
    }

    @Override
    protected void onSignedOut() {
        super.onSignedOut();
        invalidateOptionsMenu();

        if (mCurrentTab == R.id.tab_collection || mCurrentTab == R.id.tab_decks) {
            // Switch to card database tab.
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();

        if (isAuthenticated()) {
            inflater.inflate(R.menu.main_menu_signed_in, menu);
        } else {
            inflater.inflate(R.menu.main_menu_signed_out, menu);
        }

        MenuItem myActionMenuItem = menu.findItem( R.id.action_search);
        SearchView searchView = (SearchView) myActionMenuItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Toast.makeText(MainActivity.this,
                        getString(R.string.coming_soon), Toast.LENGTH_LONG)
                        .show();

                // Return false to hide the keyboard.
                return false;
            }
            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });

        return true;
    }
}

@@ -1475,24 +1475,111 @@
 nceState);%0A%0A
+        // Launch Card DB fragment.%0A        launchFragment(new ComingSoonFragment());%0A%0A
         Bott
@@ -1960,197 +1960,8 @@
 ) %7B%0A
-                        FragmentManager fragmentManager = getSupportFragmentManager();%0A                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();%0A
     
@@ -2908,111 +2908,8 @@
 t();
-%0A                                fragmentTransaction.replace(R.id.contentContainer, fragment, %22decks%22);
 %0A%0A  
@@ -4077,611 +4077,118 @@
     
-fragmentTransaction.replace(R.id.contentContainer, fragment, %22collection%22);%0A                                break;%0A                            case R.id.tab_public_decks:%0A                                fragment = new ComingSoonFragment();%0A                                fragmentTransaction.replace(R.id.contentContainer, fragment, %22public%22);%0A                                break;%0A                            case R.id.tab_card_db:%0A                                fragment = new ComingSoonFragment();%0A                                fragmentTransaction.replace(R.id.contentContainer, fragment, %22cards%22
+break;%0A                            default:%0A                                fragment = new ComingSoonFragment(
 );%0A 
@@ -4280,35 +4280,31 @@
     
-fragmentTransaction.commit(
+launchFragment(fragment
 );%0A 
@@ -4452,358 +4452,940 @@
     
-@Override%0A    protected void onSignedIn() %7B%0A        super.onSignedIn();%0A        invalidateOptionsMenu();%0A    %7D%0A%0A    @Override%0A    protected void onSignedOut() %7B%0A        super.onSignedOut();%0A        invalidateOptionsMenu();%0A%0A        if (mCurrentTab == R.id.tab_collection %7C%7C mCurrentTab == R.id.tab_decks) %7B%0A            // Switch to card database tab.
+private void launchFragment(Fragment fragment) %7B%0A        FragmentManager fragmentManager = getSupportFragmentManager();%0A        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();%0A        fragmentTransaction.replace(%0A                R.id.contentContainer, fragment, fragment.getClass().getSimpleName());%0A        fragmentTransaction.commit();%0A    %7D%0A%0A    @Override%0A    protected void onSignedIn() %7B%0A        super.onSignedIn();%0A        invalidateOptionsMenu();%0A    %7D%0A%0A    @Override%0A    protected void onSignedOut() %7B%0A        super.onSignedOut();%0A        invalidateOptionsMenu();%0A%0A        // If we are currently in an activity that requires authentication, switch to another.%0A        if (mCurrentTab == R.id.tab_collection %7C%7C mCurrentTab == R.id.tab_decks) %7B%0A            // Have to recreate activity since there is no way to dynamically trigger the bottom%0A            // navigation bar.%0A            recreate();
 %0A   

Switch back to card db tab when signing out.