package com.sheep.gamegroup.view.fragment;

import android.app.Activity;
import android.view.View;
import android.widget.ListView;

import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.CreditCardProgressQuery;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.view.adapter.CreditCardProgressQueryAdp;
import com.sheep.gamegroup.view.customview.RefreshLayout;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by realicing on 2018/4/24.
 * realicing@sina.com
 */
public class FgtCreditCardProgressQuery extends BaseFragment {
    @BindView(R.id.credit_card_task_list)
    ListView credit_card_task_list;
    @BindView(R.id.swipe_container)
    RefreshLayout swipe_container;
    @BindView(R.id.empty_view)
    View empty_view;
    @Override
    public int getLayoutId() {
        return R.layout.fgt_credit_card_task_list;
    }

    private List<CreditCardProgressQuery> list = new ArrayList<>();
    private CreditCardProgressQueryAdp adapter;
    private Activity activity;
    @Override
    public void onViewCreated() {
        activity = getActivity();
        swipe_container.setFooterView(activity, credit_card_task_list, R.layout.listview_footer);
        adapter = new CreditCardProgressQueryAdp(activity, list);
        credit_card_task_list.setAdapter(adapter);
        swipe_container.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
        initData();
    }

    public void initData() {
        list.clear();
        adapter.notifyDataSetChanged();
        empty_view.setVisibility(View.INVISIBLE);
        SheepApp.getInstance().getNetComponent().getApiService().getCreditCardScheduleList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<CreditCardProgressQuery> newList = baseMessage.getDatas(CreditCardProgressQuery.class);
                        list.addAll(newList);
                        notifyDataSetChanged();
                    }

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

    private void notifyDataSetChanged() {
        CommonUtil.getInstance().updateEmptyView(empty_view, list.isEmpty());
        adapter.notifyDataSetChanged();
        swipe_container.setRefreshing(false);
    }
}
