/**
 * project: OPlay
 * <p/>
 * <p/>
 * ========================================================================
 * amend date			amend user			amend reason
 * 2013-3-6			    CsHeng
 */

package com.gh.gamecenter.common.base.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;

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

import java.util.List;


/**
 * ViewPager 配合ViewGroup Checkable实现双切换<br/>
 * 记得自己控制onCreateView返回和ViewGroup里面Checkable个数,ViewPager的布局<br/>
 *
 * @author CsHeng
 * @date 2013-3-6
 * @update 2014-09-29
 */
public abstract class BaseFragment_ViewPager_Checkable extends BaseFragment_ViewPager implements
        ViewPager.OnPageChangeListener {

    protected ViewGroup mCheckableGroup;

    private int mLastPosition = 0;

    @IdRes
    protected abstract int getCheckableGroupId();

    protected boolean getSmoothScroll() {
        return false;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mCheckableGroup = (ViewGroup) view.findViewById(getCheckableGroupId());
        for (int i = 0, size = mCheckableGroup.getChildCount(); i < size; i++) {
            mCheckableGroup.getChildAt(i).setOnClickListener(this);
        }
        mViewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mViewPager.removeOnPageChangeListener(this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        checkIndex(mCheckedIndex);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int index) {
        onPageChanged(index);
        try {
            // 补充Viewpager Fragment的生命周期， 先调用旧选中 fragment 的 onPause 再当前的 onResume
            if (mFragmentsList.size() > mLastPosition) {
                Fragment fragment = mFragmentsList.get(mLastPosition);
                fragment.onPause();

                FragmentManager childFragmentManager = fragment.getChildFragmentManager();
                List<Fragment> fragments = childFragmentManager.getFragments();
                for (Fragment childFragment : fragments) {
                    childFragment.onPause();
                }
            }

            if (mFragmentsList.size() > index) {
                Fragment fragment = mFragmentsList.get(index);
                fragment.onResume();

                FragmentManager childFragmentManager = fragment.getChildFragmentManager();
                List<Fragment> fragments = childFragmentManager.getFragments();
                for (Fragment childFragment : fragments) {
                    childFragment.onResume();
                }
            }
            mLastPosition = index;
        } catch (Exception ignore) {

        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    protected boolean handleOnClick(View view) {
        final int toCheck = mCheckableGroup.indexOfChild(view);
        if (toCheck != -1) {
            mViewPager.setCurrentItem(toCheck, getSmoothScroll());
            return true;
        }
        return super.handleOnClick(view);
    }

    protected void checkIndex(int index) {
        final int childCount = mCheckableGroup.getChildCount();
        if (index < childCount && mCheckedIndex < childCount) {
            final View toChecked = mCheckableGroup.getChildAt(index);
            if (toChecked instanceof Checkable) {
                ((Checkable) toChecked).setChecked(true);
            }
            if (index != mCheckedIndex) {
                final View checkedChild = mCheckableGroup.getChildAt(mCheckedIndex);
                if (checkedChild instanceof Checkable) {
                    ((Checkable) checkedChild).setChecked(false);
                }
            }
            mCheckedIndex = index;
        }
    }

    protected void onPageChanged(int index) {
        checkIndex(index);
    }

}
