package com.sheep.gamegroup.absBase;

import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.view.fragment.BaseListFragment6;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by realicing on 2018/11/28.
 * realicing@sina.com
 */
public abstract class NetApiRefresh<T> implements IApiRefresh {
    public NetApiRefresh(BaseListFragment6<?> baseListFragment6) {
        this.baseListFragment6 = baseListFragment6;
    }

    public abstract String getKey(int page, int per_page);

    public abstract Observable<BaseMessage> getApi(ApiService apiService);

    public abstract Class<T> getTClass();

    protected BaseMessage lastMessage;//最后一个网络获取的结果
    protected int page = 1;//页数
    protected int per_page = DataUtil.PER_PAGE;
    private boolean loadMore;
    protected List<T> list = ListUtil.emptyList();

    public List<T> getList() {
        return list;
    }

    protected List<T> lastCacheList = ListUtil.emptyList();

    public List<T> getLastCacheList() {
        return lastCacheList;
    }

    public BaseMessage getLastMessage() {
        return lastMessage;
    }

    public void loadMoreData() {
        if (!loadMore) {
            loadMore = true;
            if (hasMore()) {
                page += 1;
                initData();
            } else {
                if (baseListFragment6 != null)
                    baseListFragment6.notifyDataSetChanged(page, hasMore());
            }
        }
    }

    //还有数据没有获取
    protected boolean hasMore() {
        if(lastMessage != null && lastMessage.getTotal() > 0){
            return lastMessage.getTotal() > ListUtil.size(list);
        }
        return ListUtil.size(list) >= per_page * page;
    }

    public void clear() {
        lastMessage = null;
        list.clear();
        page = 1;
    }

    protected BaseListFragment6<?> baseListFragment6;

    public void initData() {
        final String urlKey = getKey(page, per_page);
        final boolean isFirstGetACache = urlKey != null;
        if (isFirstGetACache) {
            //先尝试获取缓存数据
            lastCacheList = DataUtil.getInstance().getCacheList(urlKey, getTClass());
            loadList(lastCacheList);
        }
        getApi(SheepApp.getInstance().getNetComponent().getApiService())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        lastMessage = baseMessage;
                        boolean isNewData = DataUtil.getInstance().isNewData(urlKey);
                        if (isNewData || !isFirstGetACache) {
                            if (isFirstGetACache)
                                ListUtil.removeAll(list, lastCacheList);
                            List<T> newList = baseMessage.getDatas(getTClass());
                            loadList(newList);
                        } else {
                            notifyDataSetChanged();
                        }
                    }

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

    private void notifyDataSetChanged() {
        if (baseListFragment6 != null)
            baseListFragment6.notifyDataSetChanged(page, hasMore());
    }

    protected List<T> newList;

    protected void loadList(List<T> addList) {
        ListUtil.addAllItem(list, addList);
        newList = addList;
        notifyDataSetChanged();
    }

    public void setLoadMore(boolean loadMore) {
        this.loadMore = loadMore;
    }

    public boolean hasPosition(int position){
        return ListUtil.hasIndex(list, position);
    }

    public T getItem(int position){
        return ListUtil.getItem(list, position);
    }
}
