// Path: navigation-fragment/src/main/java/com/github/dmcapps/navigationfragment/common/interfaces/Navigation.java
// public interface Navigation {
// 
//     String getNavTag();
// 
//     void setNavBundle(Bundle bundle);
//     Bundle getNavBundle();
// 
//     NavigationManager getNavigationManager();
// 
//     /**
//      * Begins a Transaction for presenting the next fragment allowing for overriding of animations, bundles, etc.
//      *
//      * @return
//      *      returns an instance of {@link PresentationTransaction} for the programmer to describe the next presentation
//      */
//     PresentationTransaction beginPresentation();
// 
//     /**
//      * Push a new Fragment onto the stack and presenting it to the screen
//      * Uses default animation of slide in from right and slide out to left.
//      *
//      * @param
//      *      navFragment -> The Fragment to show. It must be a Fragment that implements {@link Navigation}
//      */
//     void presentFragment(Navigation navFragment);
// 
//     /**
//      * Push a new Fragment onto the stack and presenting it to the screen
//      * Uses default animation of slide in from right and slide out to left.
//      * Sends a Bundle with the Fragment that can be retrieved using {@link Navigation#getNavBundle()}
//      *
//      * @param
//      *      navFragment -> The Fragment to show. It must be a Fragment that implements {@link Navigation}
//      * @param
//      *      navBundle -> Bundle to add to the presenting of the Fragment.
//      */
//     void presentFragment(Navigation navFragment, Bundle navBundle);
// 
//     /**
//      * Dimiss the current fragment off the top of the stack and dismiss it.
//      * Uses default animation of slide in from left and slide out to right animation.
//      */
//     void dismissFragment();
// 
//     /**
//      * Pop the current fragment off the top of the stack and dismiss it.
//      * Uses default animation of slide in from left and slide out to right animation.
//      *
//      * @param
//      *      navBundle -> The navigation bundle to add to the fragment after the pop occurs
//      */
//     void dismissFragment(Bundle navBundle);
// 
//     /**
//      * Dismiss all fragments to the given index in the stack
//      */
//     void dismissToIndex(int index);
// 
//     /**
//      * Dismiss all fragments from the stack until we reach the Root Fragment (the fragment at the min stack size)
//      */
//     void dismissToRoot();
// 
//     /**
//      * Remove all fragments from the stack including the Root. The add the given {@link Navigation}
//      * as the new root fragment. The definition of the Root Fragment is the Fragment at the min stack size position.
//      *
//      * @param
//      *      navFragment -> The fragment that you would like as the new Root of the stack.
//      */
//     void replaceRootFragment(Navigation navFragment);
// 
//     void setTitle(String title);
//     void setTitle(int resId);
//     String getTitle();
// 
//     boolean isPortrait();
//     boolean isTablet();
// }
// 
// Path: navigation-fragment/src/main/java/com/github/dmcapps/navigationfragment/common/interfaces/Stack.java
// public interface Stack extends Serializable {
// 
//     Navigation pushFragment(NavigationManager navigationManager, Navigation navFragment, PresentationTransaction transaction);
// 
//     Navigation popFragment(NavigationManager navigationManager, Bundle navBundle);
// 
//     void clearNavigationStackToIndex(NavigationManager navigationManager, int index);
// 
//     void clearNavigationStackToIndex(NavigationManager navigationManager, int index, boolean inclusive);
// 
//     Navigation getFragmentAtIndex(NavigationManager navigationManager, int index);
// 
// }
// 
// Path: navigation-fragment/src/main/java/com/github/dmcapps/navigationfragment/common/interfaces/State.java
// public interface State extends Serializable {
// 
//     Stack<String> getStack();
// 
//     void isTablet(boolean isTablet);
// 
//     boolean isTablet();
// 
//     void isPortrait(boolean isPortrait);
// 
//     boolean isPortrait();
// }


import android.app.FragmentManager;
import android.os.Bundle;
import android.util.Log;
import com.github.dmcapps.navigationfragment.common.interfaces.Navigation;
import com.github.dmcapps.navigationfragment.common.interfaces.Stack;
import com.github.dmcapps.navigationfragment.common.interfaces.State;
import java.util.Locale;
        clearNavigationStackToIndex(navigationManager, index, false);
    }

    @Override
    public void clearNavigationStackToIndex(NavigationManager navigationManager, int index, boolean inclusive) {
        if (index < 0) {
            throw new RuntimeException("You cannot remove fragments below the 0 index. Please use the 0 index with inclusive = true to remove it. It's likely better to use replaceRootFragment() to make sure you always have a root fragment");
        }

        State state = navigationManager.getState();
        java.util.Stack<String> stack = state.getStack();

        if ((index + 1) >= stack.size()) {
            Log.e(TAG, String.format(Locale.ENGLISH, "Cannot clear navigation to index (%d) when the stack size is (%d)", index, stack.size()));
            return;
        }

        String tag = stack.get(index);
        int popToStackSize = inclusive ? index : index + 1;

        while (stack.size() > popToStackSize) {
            stack.pop();
        }

        FragmentManagerWrapper fragmentManager = new FragmentManagerWrapper(navigationManager.getContainer().getNavChildFragmentManager());
        fragmentManager.popBackStackImmediate(tag, inclusive ? FragmentManager.POP_BACK_STACK_INCLUSIVE : 0);
    }

    @Override
    public Navigation getFragmentAtIndex(NavigationManager navigationManager, int index) {
// Path: navigation-fragment/src/main/java/com/github/dmcapps/navigationfragment/common/interfaces/Navigation.java
// public interface Navigation {
// 
//     String getNavTag();
// 
//     void setNavBundle(Bundle bundle);
//     Bundle getNavBundle();
// 
//     NavigationManager getNavigationManager();
// 
//     /**
//      * Begins a Transaction for presenting the next fragment allowing for overriding of animations, bundles, etc.
//      *
//      * @return
//      *      returns an instance of {@link PresentationTransaction} for the programmer to describe the next presentation
//      */
//     PresentationTransaction beginPresentation();
// 
//     /**
//      * Push a new Fragment onto the stack and presenting it to the screen
//      * Uses default animation of slide in from right and slide out to left.
//      *
//      * @param
//      *      navFragment -> The Fragment to show. It must be a Fragment that implements {@link Navigation}
//      */
//     void presentFragment(Navigation navFragment);
// 
//     /**
//      * Push a new Fragment onto the stack and presenting it to the screen
//      * Uses default animation of slide in from right and slide out to left.
//      * Sends a Bundle with the Fragment that can be retrieved using {@link Navigation#getNavBundle()}
//      *
//      * @param
//      *      navFragment -> The Fragment to show. It must be a Fragment that implements {@link Navigation}
//      * @param
//      *      navBundle -> Bundle to add to the presenting of the Fragment.
//      */
//     void presentFragment(Navigation navFragment, Bundle navBundle);
// 
//     /**
//      * Dimiss the current fragment off the top of the stack and dismiss it.
//      * Uses default animation of slide in from left and slide out to right animation.
//      */
//     void dismissFragment();
// 
//     /**
//      * Pop the current fragment off the top of the stack and dismiss it.
//      * Uses default animation of slide in from left and slide out to right animation.
//      *
//      * @param
//      *      navBundle -> The navigation bundle to add to the fragment after the pop occurs
//      */
//     void dismissFragment(Bundle navBundle);
// 
//     /**
//      * Dismiss all fragments to the given index in the stack
//      */
//     void dismissToIndex(int index);
// 
//     /**
//      * Dismiss all fragments from the stack until we reach the Root Fragment (the fragment at the min stack size)
//      */
//     void dismissToRoot();
// 
//     /**
//      * Remove all fragments from the stack including the Root. The add the given {@link Navigation}
//      * as the new root fragment. The definition of the Root Fragment is the Fragment at the min stack size position.
//      *
//      * @param
//      *      navFragment -> The fragment that you would like as the new Root of the stack.
//      */
//     void replaceRootFragment(Navigation navFragment);
// 
//     void setTitle(String title);
//     void setTitle(int resId);
//     String getTitle();
// 
//     boolean isPortrait();
//     boolean isTablet();
// }
// 
// Path: navigation-fragment/src/main/java/com/github/dmcapps/navigationfragment/common/interfaces/Stack.java
// public interface Stack extends Serializable {
// 
//     Navigation pushFragment(NavigationManager navigationManager, Navigation navFragment, PresentationTransaction transaction);
// 
//     Navigation popFragment(NavigationManager navigationManager, Bundle navBundle);
// 
//     void clearNavigationStackToIndex(NavigationManager navigationManager, int index);
// 
//     void clearNavigationStackToIndex(NavigationManager navigationManager, int index, boolean inclusive);
// 
//     Navigation getFragmentAtIndex(NavigationManager navigationManager, int index);
// 
// }
// 
// Path: navigation-fragment/src/main/java/com/github/dmcapps/navigationfragment/common/interfaces/State.java
// public interface State extends Serializable {
// 
//     Stack<String> getStack();
// 
//     void isTablet(boolean isTablet);
// 
//     boolean isTablet();
// 
//     void isPortrait(boolean isPortrait);
// 
//     boolean isPortrait();
// }


// Path: navigation-fragment/src/main/java/com/github/dmcapps/navigationfragment/common/core/StackManager.java
import android.app.FragmentManager;
import android.os.Bundle;
import android.util.Log;
import com.github.dmcapps.navigationfragment.common.interfaces.Navigation;
import com.github.dmcapps.navigationfragment.common.interfaces.Stack;
import com.github.dmcapps.navigationfragment.common.interfaces.State;
import java.util.Locale;

        clearNavigationStackToIndex(navigationManager, index, false);
    }

    @Override
    public void clearNavigationStackToIndex(NavigationManager navigationManager, int index, boolean inclusive) {
        if (index < 0) {
            throw new RuntimeException("You cannot remove fragments below the 0 index. Please use the 0 index with inclusive = true to remove it. It's likely better to use replaceRootFragment() to make sure you always have a root fragment");
        }

        State state = navigationManager.getState();
        java.util.Stack<String> stack = state.getStack();

        if ((index + 1) >= stack.size()) {
            Log.e(TAG, String.format(Locale.ENGLISH, "Cannot clear navigation to index (%d) when the stack size is (%d)", index, stack.size()));
            return;
        }

        String tag = stack.get(index);
        int popToStackSize = inclusive ? index : index + 1;

        while (stack.size() > popToStackSize) {
            stack.pop();
        }

        FragmentManagerWrapper fragmentManager = new FragmentManagerWrapper(navigationManager.getContainer().getNavChildFragmentManager());
        fragmentManager.popBackStackImmediate(tag, inclusive ? FragmentManager.POP_BACK_STACK_INCLUSIVE : 0);
    }

    @Override
    public Navigation getFragmentAtIndex(NavigationManager navigationManager, int index) {
        State state = navigationManager.getState();