package xyz.android.amrro.popularmovies.ui.movie.pager;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Objects;

import xyz.android.amrro.popularmovies.R;
import xyz.android.amrro.popularmovies.common.BaseFragment;
import xyz.android.amrro.popularmovies.common.Navigator;
import xyz.android.amrro.popularmovies.databinding.PagerFragmentBinding;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MoviePagerFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public final class MoviePagerFragment extends BaseFragment {
    private PagerFragmentBinding binding;
    private MoviePagerAdapter pagerAdapter;

    public MoviePagerFragment() {
        // Required empty public constructor
    }

    public static MoviePagerFragment newInstance(@NonNull final Integer id) {
        MoviePagerFragment fragment = new MoviePagerFragment();
        Bundle args = new Bundle();
        args.putInt(Navigator.KEY_ITEM_ID, Objects.requireNonNull(id));
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pager, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        pagerAdapter = new MoviePagerAdapter(getFragmentManager(), itemId());
        binding.pager.setAdapter(pagerAdapter);
        setUpTabs();
    }

    private void setUpTabs() {
        binding.navigation.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.action_info:
                    binding.pager.setCurrentItem(0);
                    break;
                case R.id.action_trailers:
                    binding.pager.setCurrentItem(1);
                    break;
                case R.id.action_reviews:
                    binding.pager.setCurrentItem(2);
                    break;
            }
            return true;
        });

        binding.pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                binding.navigation.getMenu().getItem(position).setChecked(true);
            }
        });
    }
}

@@ -916,16 +916,72 @@
 gment %7B%0A
+    static final String CURRENT_PAGE = %22CURRENT_PAGE%22;%0A%0A
     priv
@@ -1030,38 +1030,23 @@
 ate 
-MoviePagerAdapter pagerAdapter
+int currentItem
 ;%0A%0A 
@@ -1474,20 +1474,20 @@
  public 
-View
+void
  onCreat
@@ -1491,82 +1491,292 @@
 eate
-View(LayoutInflater inflater, ViewGroup container,%0A               
+(@Nullable Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A        // restore current tab.%0A        if (savedInstanceState != null && savedInstanceState.containsKey(CURRENT_PAGE)) %7B%0A            currentItem = savedInstanceState.getInt(CURRENT_PAGE, 0);%0A
         
     
@@ -1771,21 +1771,113 @@
         
+%7D%0A
     
- 
+%7D%0A%0A    @Override%0A    public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle 
@@ -2192,16 +2192,34 @@
         
+MoviePagerAdapter 
 pagerAda
@@ -2364,34 +2364,370 @@
     
-private void setUpTabs() %7B
+@Override%0A    public void onSaveInstanceState(Bundle outState) %7B%0A        outState.putInt(CURRENT_PAGE, currentItem);%0A        super.onSaveInstanceState(outState);%0A    %7D%0A%0A    private void setUpTabs() %7B%0A        if (currentItem %3E= 0 && currentItem %3C 3) %7B%0A            binding.pager.setCurrentItem(currentItem);%0A        %7D else %7B%0A            currentItem = 0;%0A        %7D%0A
 %0A   
@@ -2875,16 +2875,53 @@
 n_info:%0A
+                    currentItem = 0;%0A
         
@@ -2961,17 +2961,27 @@
 entItem(
-0
+currentItem
 );%0A     
@@ -2994,32 +2994,33 @@
          break;%0A
+%0A
                 
@@ -3046,16 +3046,53 @@
 ailers:%0A
+                    currentItem = 1;%0A
         
@@ -3132,17 +3132,27 @@
 entItem(
-1
+currentItem
 );%0A     
@@ -3165,32 +3165,33 @@
          break;%0A
+%0A
                 
@@ -3216,16 +3216,53 @@
 eviews:%0A
+                    currentItem = 2;%0A
         
@@ -3302,17 +3302,27 @@
 entItem(
-2
+currentItem
 );%0A     
@@ -3559,24 +3559,64 @@
 position) %7B%0A
+                currentItem = position;%0A
             
@@ -3656,24 +3656,27 @@
 getItem(
-position
+currentItem
 ).setChe

save the current tab (fragment created twice)