package com.sheep.gamegroup.view.fragment;

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

import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.Welfare;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.SysAppUtil;
import com.sheep.gamegroup.view.adapter.GiftCenterAdapter;
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.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 我的福利--我的礼包
 * Created by realicing on 2018/8/28.
 * realicing@sina.com
 */
public class FgtMyGift extends BaseFragment {
    @Override
    public int getLayoutId() {
        return R.layout.net_empty_fresh_list_not_wrap;
    }

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

    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.empty_view)
    View empty_view;
    @BindView(R.id.title)
    View title;
    @BindView(R.id.check_net_ll)
    View check_net_ll;
    @BindView(R.id.view_list)
    RecyclerView view_list;

    public void initView() {
        activity = getActivity();
        title.setVisibility(View.GONE);
        check_net_ll.setVisibility(View.GONE);

    }

    private Activity activity;
    private List<Welfare> list = ListUtil.emptyList();
    public void initListener() {
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
                refresh.setRefreshing(false);
            }
        });

        view_list.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
        view_list.setAdapter(new GiftCenterAdapter(activity, list));
    }


    public void initData() {
        empty_view.setVisibility(View.INVISIBLE);
        SysAppUtil.checkNet(new Action1<Integer>() {
            @Override
            public void call(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);
                }
            }
        });
        SheepApp.getInstance().getNetComponent().getApiService().getFindAppReservationList(1, 100)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<Welfare> newList = baseMessage.getDataList(Welfare.class);
                        list.clear();
                        ListUtil.addAll(list, newList);
                        if(!list.isEmpty())
                            list.add(null);//底部圆角
                        notifyDataSetChanged();
                    }

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


    private void notifyDataSetChanged() {
        CommonUtil.getInstance().updateEmptyView(empty_view, list.isEmpty());
        refresh.setRefreshing(false);
        view_list.getAdapter().notifyDataSetChanged();
    }
}
