package cn.ben.tvdemo.mainpage;

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.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnPageChange;
import cn.ben.tvdemo.R;
import cn.ben.tvdemo.data.tvchannel.source.TVChannelsRepository;
import cn.ben.tvdemo.data.tvtype.source.TVTypesRepository;
import cn.ben.tvdemo.mainpage.favorite.FavoriteFragment;
import cn.ben.tvdemo.mainpage.settings.SettingsFragment;
import cn.ben.tvdemo.mainpage.shows.ShowsFragment;

import static cn.ben.tvdemo.mainpage.MainPageActivity.FragmentPosition.FAV_FRAGMENT_POS;
import static cn.ben.tvdemo.mainpage.MainPageActivity.FragmentPosition.SETTINGS_FRAGMENT_POS;
import static cn.ben.tvdemo.mainpage.MainPageActivity.FragmentPosition.SHOWS_FRAGMENT_POS;

@SuppressWarnings("WeakerAccess")
public class MainPageActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.viewpager)
    ViewPager mViewPager;
    @BindView(R.id.bottom_nav)
    BottomNavigationView mBottomNavigationView;
    @BindView(R.id.mToolbar)
    Toolbar mToolbar;

    private int mPrevSelectedMenuItemPos = -1;
    private boolean mCreatedFromSavedInstance;

    enum FragmentPosition {
        SHOWS_FRAGMENT_POS,
        FAV_FRAGMENT_POS,
        SETTINGS_FRAGMENT_POS
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            // TODO: 2017/2/19 load previously saved state
            mCreatedFromSavedInstance = true;
        } else {
            mCreatedFromSavedInstance = false;
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page_act);
        ButterKnife.bind(this);

        setupToolbar();
        setupViewPager();
        setupBottomNavigationView();
    }

    private void setupToolbar() {
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeAsUpIndicator(R.mipmap.ic_launcher);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    private void setupViewPager() {
        MainPagePagerAdapter viewPagerAdapter = new MainPagePagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(viewPagerAdapter);
    }

    private void setupBottomNavigationView() {
        mBottomNavigationView.setOnNavigationItemSelectedListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        TVTypesRepository.destroyInstance();
        TVChannelsRepository.destroyInstance();
    }

    // BottomNavigationView.OnNavigationItemSelectedListener start
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_shows:
                mViewPager.setCurrentItem(SHOWS_FRAGMENT_POS.ordinal());
                break;
            case R.id.menu_item_fav:
                mViewPager.setCurrentItem(FAV_FRAGMENT_POS.ordinal());
                break;
            case R.id.menu_item_settings:
                mViewPager.setCurrentItem(SETTINGS_FRAGMENT_POS.ordinal());
                break;
            default:
                break;
        }
        return true;
    }
    // BottomNavigationView.OnNavigationItemSelectedListener end

    // ViewPager.OnPageChangeListener start
    @OnPageChange(value = R.id.viewpager, callback = OnPageChange.Callback.PAGE_SELECTED)
    public void onPageSelected(int position) {
        if (position == mPrevSelectedMenuItemPos) return;
        if (mPrevSelectedMenuItemPos >= 0 && mPrevSelectedMenuItemPos < mBottomNavigationView.getMenu().size())
            mBottomNavigationView.getMenu().getItem(mPrevSelectedMenuItemPos).setChecked(false);
        if (position >= 0 && position < mBottomNavigationView.getMenu().size())
            mBottomNavigationView.getMenu().getItem(position).setChecked(true);
        mPrevSelectedMenuItemPos = position;
    }
    // ViewPager.OnPageChangeListener end

    class MainPagePagerAdapter extends FragmentPagerAdapter {

        MainPagePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == SHOWS_FRAGMENT_POS.ordinal()) {
                return ShowsFragment.newInstance();
            } else if (position == FAV_FRAGMENT_POS.ordinal()) {
                return FavoriteFragment.newInstance();
            } else if (position == SETTINGS_FRAGMENT_POS.ordinal()) {
                return SettingsFragment.newInstance();
            }
            return null;
        }

        @Override
        public int getCount() {
            return FragmentPosition.values().length;
        }
    }
}

@@ -496,16 +496,42 @@
 enuItem;
+%0Aimport android.view.View;
 %0A%0Aimport
@@ -770,122 +770,8 @@
 ry;%0A
-import cn.ben.tvdemo.mainpage.favorite.FavoriteFragment;%0Aimport cn.ben.tvdemo.mainpage.settings.SettingsFragment;%0A
 impo
@@ -889,19 +889,21 @@
 osition.
-FAV
+SHOWS
 _FRAGMEN
@@ -901,32 +901,35 @@
 S_FRAGMENT_POS;%0A
+%0A//
 import static cn
@@ -983,24 +983,19 @@
 osition.
-SETTINGS
+FAV
 _FRAGMEN
@@ -1001,16 +1001,18 @@
 NT_POS;%0A
+//
 import s
@@ -1067,35 +1067,38 @@
 agmentPosition.S
-HOW
+ETTING
 S_FRAGMENT_POS;%0A
@@ -1586,32 +1586,34 @@
 S_FRAGMENT_POS,%0A
+//
         FAV_FRAG
@@ -1622,16 +1622,18 @@
 NT_POS,%0A
+//
         
@@ -1736,54 +1736,8 @@
 ) %7B%0A
-        if (savedInstanceState != null) %7B%0A    
     
@@ -1787,36 +1787,32 @@
 d state%0A        
-    
 mCreatedFromSave
@@ -1827,87 +1827,35 @@
 e = 
-true;%0A        %7D else %7B%0A            mCreatedFromSavedInstance = false;%0A        %7D
+savedInstanceState != null;
 %0A%0A  
@@ -2683,16 +2683,72 @@
 (this);%0A
+        mBottomNavigationView.setVisibility(View.GONE);%0A
     %7D%0A%0A 
@@ -3234,32 +3234,34 @@
          break;%0A
+//
             case
@@ -3281,16 +3281,18 @@
 em_fav:%0A
+//
         
@@ -3346,32 +3346,34 @@
 POS.ordinal());%0A
+//
                 
@@ -3371,32 +3371,34 @@
          break;%0A
+//
             case
@@ -3423,16 +3423,18 @@
 ttings:%0A
+//
         
@@ -3493,32 +3493,34 @@
 POS.ordinal());%0A
+//
                 
@@ -4693,32 +4693,34 @@
 .newInstance();%0A
+//
             %7D el
@@ -4760,32 +4760,34 @@
 OS.ordinal()) %7B%0A
+//
                 
@@ -4817,32 +4817,34 @@
 .newInstance();%0A
+//
             %7D el
@@ -4889,32 +4889,34 @@
 OS.ordinal()) %7B%0A
+//
                 

remove fav and settings