package com.sheep.gamegroup.view.fragment;

import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.PagerAdapter;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.Locale;

import butterknife.BindView;

/**
 * Created by ljy on 2018/8/17.
 */

public class FgtAskGetMoneyFriend extends BaseFragment {

    @BindView(R.id.friend_extract_vp)
    ViewPager friend_extract_vp;
    @BindView(R.id.friend_extract_last_page_tv)
    TextView friend_extract_last_page_tv;
    @BindView(R.id.friend_extract_page_tv)
    TextView friend_extract_page_tv;
    @BindView(R.id.friend_extract_next_page_tv)
    TextView friend_extract_next_page_tv;


    public static FgtAskGetMoneyFriend newInstance(){
        return new FgtAskGetMoneyFriend();
    }
    @Override
    public int getLayoutId() {
        return R.layout.fgt_askgetmoney_friend;
    }

    @Override
    public void onViewCreated() {
        initView();
    }

    private void initView() {
        initFriendExtractList();
    }

    private SparseArray<FgtFriendExtractPage> mTestFragments;
    private int totalPage = 0;
    public void setFriendExtractCount(int count, int page, int size) {
        totalPage = ListUtil.getTotalPage(count, 5);
        friend_extract_page_tv.setText(String.format(Locale.CHINA, "%d/%d", curPage, totalPage));
        if (count > 0)
            checkAddPage(page, size);
    }
    //无好友，不显示翻页操作按钮
    public void updateByNoFriend(){
        if(friend_extract_last_page_tv.getParent() instanceof View)
            ViewUtil.setVisibility(((View) friend_extract_last_page_tv.getParent()), false);
    }

    private void checkAddPage(int page, int size) {
        if (curPage + 2 > page && size == FgtFriendExtractPage.per_page) {//已经到最后一页面,且数据是满的，可以添加一页面
            int nextPage = page + 1;
            if (mTestFragments.get(nextPage) == null) {
//                if(BuildConfig.DEBUG) G.showToast("添加新的一页:" + nextPage);
                mTestFragments.put(nextPage, FgtFriendExtractPage.newInstance(nextPage));
                mPagerAdapter.notifyDataSetChanged();
            } else {
//                if(BuildConfig.DEBUG) G.showToast("不用添加新的一页:" + nextPage);
            }
        }
        if (size > FgtFriendExtractPage.per_page) {
            if (BuildConfig.DEBUG) G.showToast("页面" + page + "的数据为：" + size);
        }
    }
    private int curPage = 1;
    private PagerAdapter mPagerAdapter;
    private void initFriendExtractList() {
        mTestFragments = new SparseArray<>();
        mTestFragments.put(1, FgtFriendExtractPage.newInstance(1));
        mPagerAdapter = new PagerAdapter(getChildFragmentManager(), mTestFragments);
        friend_extract_vp.setAdapter(mPagerAdapter);
        friend_extract_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                curPage = i + 1;
                friend_extract_page_tv.setText(String.format(Locale.CHINA, "%d/%d", curPage, totalPage));
                checkAddPage(curPage, mTestFragments.get(curPage).getList().size());
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        friend_extract_last_page_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                friend_extract_vp.setCurrentItem(curPage - 2);
            }
        });
        friend_extract_next_page_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                friend_extract_vp.setCurrentItem(curPage);
            }
        });
    }

}
