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

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

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

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

import com.lightgame.adapter.BaseFragmentPagerAdapter;
import com.lightgame.config.CommonDebug;
import com.lightgame.view.DoubleTapTextView;

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

/**
 * ViewPager 配合RadioGroup实现双切换<br/>
 * 记得自己控制onCreateView返回和radioGroup里面radiobutton个数,Viewpager的布局<br/>
 *
 * @author CsHeng
 * @date 2013-3-6
 */
public abstract class BaseFragment_ViewPager extends ToolbarFragment implements DoubleTapTextView.OnDoubleTapListener {

    public static final String ARGS_INDEX = "index";
    protected int mCheckedIndex = 0;
    protected PagerAdapter mAdapter;
    protected List<Fragment> mFragmentsList;
    protected ViewPager mViewPager;

    @LayoutRes
    protected abstract int getLayoutId();

    @IdRes
    protected abstract int getViewPagerId();

    protected abstract void initFragmentList(List<Fragment> fragments);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mViewPager = view.findViewById(getViewPagerId());
        mFragmentsList = restoreFragments();
        if (mFragmentsList.size() == 0) {
            initFragmentList(mFragmentsList);
        }
        mAdapter = BaseFragmentPagerAdapter.newInstance(getChildFragmentManager(), mFragmentsList);
        final Bundle args = getArguments();
        if (args != null) {
            mCheckedIndex = args.getInt(ARGS_INDEX);
        }
        mViewPager.setOffscreenPageLimit(mFragmentsList.size());
        mViewPager.setAdapter(mAdapter);
        if (mCheckedIndex < mFragmentsList.size()) {
            mViewPager.setCurrentItem(mCheckedIndex, false);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (getArguments() != null) {
            getArguments().putInt(ARGS_INDEX, mCheckedIndex);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mViewPager != null) {
            mViewPager.setAdapter(null);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mFragmentsList != null) {
            mFragmentsList.clear();
        }
    }

    @Override
    public boolean onDoubleTap() {
        final Fragment fragment = mFragmentsList.get(mViewPager.getCurrentItem());
        return fragment instanceof DoubleTapTextView.OnDoubleTapListener && ((DoubleTapTextView.OnDoubleTapListener)
                fragment).onDoubleTap();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (CommonDebug.IS_DEBUG) {
            CommonDebug.logMethodWithParams(this, requestCode, resultCode, data);
        }
        List<Fragment> fragments = getChildFragmentManager().getFragments();
        if (fragments != null) {
            for (Fragment fragment : fragments) {
                fragment.onActivityResult(requestCode, resultCode, data);
            }
        }
    }

    public ArrayList<Fragment> restoreFragments() {
        String tag = "android:switcher:" + mViewPager.getId() + ":";
        ArrayList<Fragment> fragments = new ArrayList<>();
        int childCount = getChildCount();
        for (int index = 0; index < childCount; index++) {
            Fragment fragment = getChildFragmentManager().findFragmentByTag(tag + index);
            if (fragment != null) {
                fragments.add(fragment);
            }
        }

        return fragments;
    }

    public abstract int getChildCount();

    public int getCurrentItem() {
        return mViewPager != null ? mViewPager.getCurrentItem() : 0;
    }

}
