package com.unis.sxzcl.ui.home;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentMainBinding;
import com.unis.sxzcl.ui.work_module.WorkModuleFragment;
import com.unis.sxzcl.widget.MyFragmentStatePagerAdapter;
import com.unis.sxzcl.widget.dialog.SystemDialog;

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

/**
 * 包名: com.unis.sxzcl.ui.home
 * 类名: MainFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/8 17:34
 * 更新人:
 * 更新时间: 2020/12/8 17:34
 * 更新说明:
 * 版本号: 1.0
 */
public class MainFragment extends BaseFragment<FragmentMainBinding> {

    private List<Fragment> mFragmentList = new ArrayList<>();

    private ImageView[] mTabImageViews;
    private TextView[] mTabTextViews;
    private boolean[] mTabTopWhite;

    private int indexFlag = -1;//tab选中标记
    private SystemDialog mSelectDepotDialog;


    @Override
    protected FragmentMainBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentMainBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        //初始化tab控件
        initTab();

        //初始化Fragment
        initFragment();

        //初始化ViewPager
        initViewPager();

    }


    /**
     * 初始化ViewPager
     */
    private void initViewPager() {
        //创建适配器
        MyFragmentStatePagerAdapter adapter = new MyFragmentStatePagerAdapter(getChildFragmentManager(), mFragmentList);
        mBinding.viewPager.setAdapter(adapter);
        //设置页面留存数
        mBinding.viewPager.setOffscreenPageLimit(mFragmentList.size() + 1);
        //监听翻页事件
        mBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                updateTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 初始化Fragment
     */
    private void initFragment() {
        mFragmentList.add(new HomeFragment());
//        mFragmentList.add(WorkModuleFragment.newFragment(true, ""));
        mFragmentList.add(new WorkModuleFragment());
    }

    /**
     * 初始化导航控件
     */
    private void initTab() {
        //给布局添加索引
        mBinding.layoutHome.setTag(0);
        mBinding.layoutWork.setTag(1);
        //监听点击事件
        mBinding.layoutHome.setOnClickListener(tabClickListener);
        mBinding.layoutWork.setOnClickListener(tabClickListener);

        //图片控件 按顺序初始化数组
        mTabImageViews = new ImageView[]{
                mBinding.ivHome,
                mBinding.ivWork,
        };
        //文字控件 按顺序初始化数组
        mTabTextViews = new TextView[]{
                mBinding.tvHome,
                mBinding.tvWork,
        };
        //顶部状态栏文字是否为白色 按顺序初始化数组
        mTabTopWhite = new boolean[]{
                true,
                true,
        };
    }

    @Override
    protected void onFirstShow() {
        updateTab(0);//默认选中第一个tab
    }



    View.OnClickListener tabClickListener = v -> {
        int index = (int) v.getTag();
        updateTab(index);
    };

    /**
     * 更新导航栏选项
     *
     * @param index 选中索引
     */
    private void updateTab(int index) {
        if (indexFlag == index)
            return;
        if (indexFlag != -1) {
            //上次选择置为默认
            mTabImageViews[indexFlag].setSelected(false);
            mTabTextViews[indexFlag].setEnabled(true);
        }

        //当前选择置为选中
        mTabImageViews[index].setSelected(true);
        mTabTextViews[index].setEnabled(false);

        if (mBinding.viewPager.getCurrentItem() != index) {
            mBinding.viewPager.setCurrentItem(index, false);
        }

        //设置状态栏颜色
        setStatusBarWhite(mTabTopWhite[index]);

        //保存索引为当前选择
        indexFlag = index;
    }


}
