package com.kfzs.duanduan.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.kfzs.duanduan.BaseCompatFragment;
import com.kfzs.duanduan.mine.GiftpackListAdapter;
import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.FindTag;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.view.adapter.AdpTryMakemoney;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import org.afinal.simplecache.ApiKey;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.FIND_TAG;

/**
 * Created by realicing on 2018/6/27.
 * realicing@sina.com
 * 主页--发现模块
 */
public class FgtFind extends BaseCompatFragment {
    private Activity activity;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.common_tab_vp);
        activity = getActivity();
        ButterKnife.bind(this, mContentView);
        initView();
        initData();
    }

    @BindView(R.id.layout_navigationBar)
    View layout_navigationBar;
    @BindView(R.id.indicator)
    TabLayout indicator;
    @BindView(R.id.pager)
    ViewPager pager;

    private GiftpackListAdapter mAdapter;

    private void initView() {
        layout_navigationBar.setVisibility(View.GONE);
        mAdapter = new GiftpackListAdapter(getFragmentManager(), SheepApp.getInstance());
        mAdapter.add(FgtFindChild.newInstance(0), "全部");
    }

    private void initData() {
        //尝试获取缓存数据
        List<FindTag> newList = DataUtil.getInstance().getCacheList(ApiKey.articles_tags, FindTag.class);
        if(ListUtil.isEmpty(newList)){
            initNetData();
        } else {
            loadList(newList);
        }
    }

    private int onResumeCount = 0;

    @Override
    public void onResume() {
        super.onResume();
        if (list.isEmpty() && onResumeCount != 0)//非第一次时，或者没有数据时调用该接口
            initNetData();
        onResumeCount++;
    }

    private void initNetData() {
        SheepApp.getInstance().getNetComponent().getApiService().getFindTagList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<FindTag> newList = baseMessage.getDatas(FindTag.class);
                        if(list.isEmpty() && !ListUtil.isEmpty(newList))//有缓存时不会加载，没有缓存时获取到数据会加载
                            loadList(newList);
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                    }
                });
    }

    //没有添加数据返回true
    private void loadList(List<FindTag> newList) {
        if (!ListUtil.isEmpty(newList)) {
            list.clear();
            ListUtil.addAll(list, newList);
            CommonUtil.getInstance().initUrlConfigByNet("awaken_on", new Action1<String>() {
                @Override
                public void call(String url) {
                    int awaken_on = NumberFormatUtils.parseInteger(url);
                    int count = 1;
                    if (awaken_on == 1) {
                        count++;
                        mAdapter.add(new FgtRouser(), "看点");
                    }
                    for (FindTag findLabel : list) {
                        mAdapter.add(FgtFindChild.newInstance(findLabel.getId()), findLabel.getName());
                        count++;
                    }
                    pager.setAdapter(mAdapter);
                    indicator.setupWithViewPager(pager);
                    pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int i, float v, int i1) {

                        }

                        @Override
                        public void onPageSelected(int i) {
                            FIND_TAG.onEvent("find_tag_name", ListUtil.hasIndex(list, i - 1) ? list.get(i - 1).getName() : "全部");
                        }

                        @Override
                        public void onPageScrollStateChanged(int i) {

                        }
                    });
                    CommonUtil.getInstance().reflex(indicator, activity);
                    pager.setOffscreenPageLimit(count);

                }
            });
        }
    }

    private List<FindTag> list = ListUtil.emptyList();

}
