package com.jamieadkins.gwent.deck.detail.user;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.app.Fragment;
import android.view.MenuItem;
import android.view.View;

import com.jamieadkins.gwent.R;
import com.jamieadkins.gwent.deck.detail.DeckDetailActivity;

public class UserDeckDetailActivity extends DeckDetailActivity {
    private static final String STATE_DECK_BUILDER_OPEN = "com.jamieadkins.com.gwent.deck.open";
    private static final String TAG_BOTTOM_FRAGMENT = "com.jamieadkins.com.gwent.deck.builder";

    private boolean mDeckBuilderOpen = false;
    private Fragment bottomFragment;
    private FloatingActionButton mAddCardButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAddCardButton = findViewById(R.id.deck_add);
        mAddCardButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openDeckBuilderMenu();
            }
        });
        if (savedInstanceState != null) {
            mDeckBuilderOpen = savedInstanceState.getBoolean(STATE_DECK_BUILDER_OPEN);
            if (mDeckBuilderOpen) {
                mAddCardButton.hide();
                getSupportActionBar().setHomeAsUpIndicator(VectorDrawableCompat.create(getResources(), R.drawable.ic_close, getTheme()));
            }
            bottomFragment = getSupportFragmentManager().findFragmentByTag(TAG_BOTTOM_FRAGMENT);
        } else {
            bottomFragment = CardDatabaseFragment.newInstance(mDeckId, mFactionId);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.contentContainer2, bottomFragment, TAG_BOTTOM_FRAGMENT)
                    .hide(bottomFragment)
                    .commit();
        }

        if (mDeckBuilderOpen) {
            openDeckBuilderMenu();
        } else {
            closeDeckBuilderMenu();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (mDeckBuilderOpen) {
                    onBackPressed();
                    return true;
                } else {
                    return super.onOptionsItemSelected(item);
                }
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putBoolean(STATE_DECK_BUILDER_OPEN, mDeckBuilderOpen);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        closeDeckBuilderMenu();
    }

    protected void closeDeckBuilderMenu() {
        mDeckBuilderOpen = false;
        mAddCardButton.show();
        getSupportActionBar().setHomeAsUpIndicator(VectorDrawableCompat.create(getResources(), R.drawable.ic_arrow_back, getTheme()));
    }

    protected void openDeckBuilderMenu() {
        mDeckBuilderOpen = true;
        mAddCardButton.hide();
        getSupportActionBar().setHomeAsUpIndicator(VectorDrawableCompat.create(getResources(), R.drawable.ic_close, getTheme()));

        getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_down, R.anim.slide_in_up, R.anim.slide_out_down)
                .show(bottomFragment)
                .hide(fragment)
                .addToBackStack(null)
                .commit();
    }
}

@@ -2270,29 +2270,36 @@
         
-onBackPressed
+closeDeckBuilderMenu
 ();%0A    
@@ -2784,31 +2784,36 @@
     
-super.onBackPressed();%0A
+if (mDeckBuilderOpen) %7B%0A    
     
@@ -2836,24 +2836,86 @@
 lderMenu();%0A
+        %7D else %7B%0A            super.onBackPressed();%0A        %7D%0A
     %7D%0A%0A    p
@@ -3148,24 +3148,300 @@
 etTheme()));
+%0A%0A        getSupportFragmentManager().beginTransaction()%0A                .setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_down, R.anim.slide_in_up, R.anim.slide_out_down)%0A                .show(fragment)%0A                .hide(bottomFragment)%0A                .commit();
 %0A    %7D%0A%0A    
@@ -3882,32 +3882,32 @@
 bottomFragment)%0A
+
                 
@@ -3926,46 +3926,8 @@
 nt)%0A
-                .addToBackStack(null)%0A
     

Fix for incorrect backstack management