package com.bonait.bnframework.modules.home.fragment.from;

import androidx.annotation.NonNull;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.modules.home.adapter.FragmentAdapter;
import com.bonait.bnframework.modules.home.fragment.from.fragment.SystemCsFltlFragment;
import com.bonait.bnframework.modules.home.fragment.from.fragment.SystemCsJrFragment;
import com.bonait.bnframework.modules.home.fragment.from.fragment.SystemCsTlFragment;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.QMUIViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WhActivity extends BaseActivity {
    @BindView(R.id.topbar)
    QMUITopBar mTopBar;
    @BindView(R.id.navigation)
    BottomNavigationView bottomNavigationView;
    @BindView(R.id.viewpager)
    QMUIViewPager viewPager;
    private MenuItem menuItem;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wh);//属性绑定
        ButterKnife.bind(this);
        initTopBar();

        initFragment();
        viewPager.addOnPageChangeListener(pageChangeListener);
        // 设置viewPager缓存多少个fragment
        viewPager.setOffscreenPageLimit(4);
        bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }
    private void initTopBar() {
        mTopBar.setTitle("系统维护");
        mTopBar.addLeftImageButton(R.mipmap.fanhui,R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /**
     * viewPager里添加fragment
     */
    private void initFragment() {
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        fragmentAdapter.addFragment(new SystemCsJrFragment());
        fragmentAdapter.addFragment(new SystemCsTlFragment());
        fragmentAdapter.addFragment(new SystemCsFltlFragment());
        viewPager.setAdapter(fragmentAdapter);
    }
    //-------------------------配置viewPager与fragment关联----------------------------//
    /**
     * 配置bottom底部菜单栏监听器，手指点击底部菜单监听
     */
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.bottom_navigation_1:
                    viewPager.setCurrentItem(0);
                    return true;
                case R.id.bottom_navigation_2:
                    viewPager.setCurrentItem(1);
                    return true;
                case R.id.bottom_navigation_3:
                    viewPager.setCurrentItem(2);
                    return true;
            }
            return false;
        }
    };


    /**
     * 配置ViewPager监听器，手指滑动监听
     */
    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            MenuItem menuItem = bottomNavigationView.getMenu().getItem(position);
        }

        @Override
        public void onPageSelected(int position) {
            menuItem = bottomNavigationView.getMenu().getItem(position);
            menuItem.setChecked(true);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected boolean canDragBack() {
        return false;
    }
}