package com.sheep.gamegroup.view.fragment;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.sheep.gamegroup.absBase.AbsObserver;
import com.sheep.gamegroup.absBase.ILoadMore;
import com.sheep.gamegroup.absBase.IRefresh;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.SysAppUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;

import java.util.List;

import butterknife.BindView;
import rx.functions.Action1;

/**
 * Created by realicing on 2018/9/3.
 * realicing@sina.com
 * 自己可以进行刷新与加载更多，使用com.jcodecraeer.xrecyclerview.XRecyclerView来实现，子类自己实现数据
 */
public abstract class BaseListFragment<T> extends BaseFragment implements IRefresh, ILoadMore {
    @Override
    public int getLayoutId() {
        return R.layout.net_empty_xrecycler;
    }

    protected Activity activity;

    @Override
    public void onViewCreated() {
        activity = getActivity();
        initView();
        initListener();
        switch (refreshDataType()) {
            case REFRESH_ON_CREATE:
                ViewUtil.refreshXrv(view_list);
                break;
            case REFRESH_ON_RESUME:
                break;
            case REFRESH_ON_YOURSELF:
            default:
                notifyDataSetChanged();
                break;
        }
    }

    public static final int REFRESH_ON_CREATE = 0;
    public static final int REFRESH_ON_RESUME = 1;
    public static final int REFRESH_ON_YOURSELF = -1;
    @BindView(R.id.empty_view)
    protected View empty_view;
    @BindView(R.id.check_net_ll)
    protected View check_net_ll;
    @BindView(R.id.view_list)
    protected XRecyclerView view_list;

    protected BaseMessage lastMessage;//最后一个网络获取的结果
    protected int page = 1;//页数
    protected int per_page = DataUtil.PER_PAGE;
    private boolean loadMore;

    public void initView() {

    }

    protected View bottomLine;

    public void initListener() {
        view_list.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }

            @Override
            public void onLoadMore() {
                loadMoreData();
            }
        });
        bottomLine = ViewUtil.setBottomLine(view_list, getNoMoreCallBack());
        view_list.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
        view_list.setAdapter(getAdapter());
    }

    public void loadMoreData() {
        if (!loadMore) {
            loadMore = true;
            if (hasMore()) {
                page += 1;
                initNetAndData();
            } else {
                view_list.setNoMore(true);
                setNoMore(true);
            }
        } else {
            view_list.loadMoreComplete();
        }
    }
    //还有数据没有获取
    protected boolean hasMore() {
        if(lastMessage != null && lastMessage.getTotal() > 0){
            return lastMessage.getTotal() > ListUtil.size(list);
        }
        return ListUtil.size(list) >= per_page * page;
    }

    public void setNoMore(boolean noMore) {
        this.noMore = noMore;
    }

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

    public void initNetAndData() {
        SysAppUtil.checkNet(new AbsObserver<Integer>() {
            @Override
            public void onNext(Integer result) {
                if (result != 0) {//无网络
                    if (check_net_ll != null)
                        check_net_ll.setVisibility(View.VISIBLE);
                } else {
                    if (check_net_ll != null)
                        check_net_ll.setVisibility(View.GONE);
                }
            }
        });
        initData();
    }

    public Action1<String> getNoMoreCallBack() {
        return null;
    }


    @Override
    public void onResume() {
        super.onResume();
        if (refreshDataType() == REFRESH_ON_RESUME) {
            ViewUtil.refreshXrv(view_list);
        }
    }

    /**
     * 在哪里初始化数据：0 onViewCreated后， 1 onResume后， -1 不初始化
     *
     * @return
     */
    public int refreshDataType() {
        return REFRESH_ON_CREATE;
    }

    public void clear() {
        lastMessage = null;
        setNoMore(false);
        empty_view.setVisibility(View.INVISIBLE);
        list.clear();
        page = 1;
    }

    public void refreshData() {
        clear();
        view_list.getAdapter().notifyDataSetChanged();
        initNetAndData();
    }

    public void notifyDataSetChanged() {
        if (view_list == null || empty_view == null){
            return;
        }
        loadMore = false;
        updateEmptyView();
        if (page == 1) {
            view_list.refreshComplete();
        } else {
            view_list.loadMoreComplete();
        }
        view_list.getAdapter().notifyDataSetChanged();
        notifyData();
        view_list.getFootView().setVisibility(view_list.getAdapter().getItemCount() == 0 ? View.GONE : View.VISIBLE);
    }

    protected void updateEmptyView() {
        CommonUtil.getInstance().updateEmptyView(empty_view, list.isEmpty());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // any time,when you finish your activity or fragment,call this below
        if (view_list != null) {
            view_list.destroy(); // this will totally release XR's memory
            view_list = null;
        }
    }


    private boolean noMore = false;

    public boolean isNoMore() {
        return noMore;
    }

    protected abstract RecyclerView.Adapter getAdapter();

    //获取数据
    public abstract void initData();
    //刷新数据的回调，子类可以实现自己的需求
    public void notifyData(){

    }
}
