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

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTest;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.adapter.FragmentAdapter;
import com.bonait.bnframework.modules.home.fragment.from.fragment.AutoTestMoreFragment;
import com.bonait.bnframework.modules.home.fragment.from.fragment.AutoTestOneFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.QMUIViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AutoTestActivity extends BaseActivity {


    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;
    @BindView(R.id.navigation)
    BottomNavigationView bottomNavigationView;
    @BindView(R.id.viewpager)
    QMUIViewPager viewPager;

    @BindView(R.id.quanxian)
    RelativeLayout quanxian;//权限显示
    @BindView(R.id.edittext_quanxian)
    EditText edittext_quanxian;//权限密码
    @BindView(R.id.quanxianmima)
    Button quanxianmima;//权限密码

    private MenuItem menuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_test);

        ButterKnife.bind(this);
        initTopBar();
        initFragment();
        viewPager.addOnPageChangeListener(pageChangeListener);
        // 设置viewPager缓存多少个fragment
        viewPager.setOffscreenPageLimit(2);
        bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    }

    private void initTopBar() {
        mTopBar.setTitle("模块自动测试");
        mTopBar.addLeftImageButton(R.mipmap.fanhui, R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(ExecuteTest.IsStart || ExecuteTheRecipe.MakeStatus)
                {
                    ToastUtils.warning("请先结束任务！！！");
                    return;
                }else
                {
                    finish();
                }
            }
        });

        quanxianmima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = edittext_quanxian.getText().toString();
                if (s == null || s.isEmpty()) {
                    ToastUtils.warning("权限密码不能为空！");
                } else {
                    if (s.equals("bpa123456")) {
                        quanxian.setVisibility(View.GONE);
                        viewPager.setVisibility(View.VISIBLE);
                    } else {
                        ToastUtils.warning("权限密码不正确！");
                    }
                }
            }
        });
    }

    FragmentAdapter fragmentAdapter = null;

    /**
     * viewPager里添加fragment
     */
    private void initFragment() {
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        fragmentAdapter.addFragment(new AutoTestOneFragment());
        fragmentAdapter.addFragment(new AutoTestMoreFragment());
        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_device_1:
                    viewPager.setCurrentItem(0);
                    return true;
                case R.id.bottom_device_2:
                    viewPager.setCurrentItem(1);
                    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;
    }
}