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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.model.mode.ResMenuLeft;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.adapter.jiaoyan_adapter;
import com.orhanobut.logger.Logger;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class JiaoYanFragment extends BaseFragment {


    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;//顶部标题

    @BindView(R.id.rv_left)
    ListView rv_left;//左侧菜单

    @BindView(R.id.viewpager_jiaoyan)
    FrameLayout viewpager_jiaoyan;//右侧控件


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

    private Context context;
    public static int mPosition;

    public ArrayList<ResMenuLeft> menuModes=new ArrayList<>();

    public JiaoYanFragment() {
    }

    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_jiaoyan, null);
        ButterKnife.bind(this, root);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        context = getContext();
        initTopBar();
        initData();
    }

    public jiaoyan_adapter adapter;
    /**
     * 初始化TopBar
     */
    private void initTopBar() {
        mTopBar.setTitle("设备校正");

        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(ConfigName.getInstance().user.pass))
                    {
                        quanxian.setVisibility(View.GONE);
                        main_show.setVisibility(View.VISIBLE);
                    }else
                    {
                        ToastUtils.warning("权限密码不正确！");
                    }
                }
            }
        });
    }
    private List<Fragment> fragmentList;
    public void initData()
    {
        fragmentList = new ArrayList<>();
        menuModes.clear();

        //DzcjyActivity 修改为窗体
        //JiaoYan_wdsz_fragment 修改为窗体
        //JiaoYan_sdkz_fragment 删除控制
        //ZdqxActivity

//        if (ConfigName.getInstance().versionSelectionEnum.equals("配料吧台")) {
//            menuModes.add(new ResMenuLeft(new JiaoYan_lsjy_fragment(),"流速校验", R.mipmap.lsjy, R.mipmap.lsjy_select,false));
//            menuModes.add(new ResMenuLeft(new JiaoYan_clsfbc_fragment(),"出料算法补偿", R.mipmap.clsfbc, R.mipmap.clsfbc_selectd,false));
//        }else
//        {
//            menuModes.add(new ResMenuLeft(new JiaoYan_lsjy_fragment(),"流速校验", R.mipmap.lsjy, R.mipmap.lsjy_select,false));
//        }

        for(ResMenuLeft item:menuModes)
        {
            fragmentList.add(item.fragment);
        }
        ShowFragment(menuModes.get(0).fragment);
        rv_left.setSelection(0);

        adapter=new jiaoyan_adapter(getContext(),menuModes);
        rv_left.setAdapter(adapter);
        rv_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ItemClick(adapterView,view,i,l);
            }
        });

        if(ConfigName.getInstance().user.name.equals("admin")
                || ConfigName.getInstance().user.account.equals("admin"))
        {
            quanxian.setVisibility(View.GONE);
            main_show.setVisibility(View.VISIBLE);
        }
    }
    /**
     * 配置ViewPager监听器，手指滑动监听
     */
    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }
        @Override
        public void onPageSelected(int position) {
            mPosition=position;
            adapter.notifyDataSetChanged();
        }
        @Override
        public void onPageScrollStateChanged(int state) {
        }
    };
    /**
     * Item 点击事件
     * @param adapterView
     * @param view
     * @param position
     * @param id
     */
    public void ItemClick(AdapterView<?> adapterView, View view, int position, long id) {

        if(DataBus.getInstance().ZDQX_IsStart)
        {
            ToastUtils.warning("自动清洗中,请等待清洗完成！");
            return;
        }
        //即使刷新adapter
        mPosition=position;
        adapter.notifyDataSetChanged();
        ShowFragment(menuModes.get(position).fragment);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.d("第一页销毁");
    }

    /**
     * 当在activity设置viewPager + BottomNavigation + fragment时，
     * 为防止viewPager左滑动切换界面，与fragment左滑返回上一界面冲突引起闪退问题，
     * 必须加上此方法，禁止fragment左滑返回上一界面。
     * <p>
     * 切记！切记！切记！否则会闪退！
     * <p>
     * 当在fragment设置viewPager + BottomNavigation + fragment时，则不会出现这个问题。
     */
    @Override
    protected boolean canDragBack() {
        return false;
    }
}