package com.kfzs.duanduan.fragment;

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

import com.alibaba.fastjson.JSON;
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.view.adapter.CreditCardProgressQueryAdp;
import com.sheep.gamegroup.view.customview.RefreshLayout;
import com.sheep.jiuyan.samllsheep.BuildConfig;
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 butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.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;
    @Override
    public void onViewCreated() {
        ButterKnife.bind(this, mView);
        swipe_container.setFooterView(getActivity(), credit_card_task_list, R.layout.listview_footer);
        adapter = new CreditCardProgressQueryAdp(getActivity(), 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>(getContext()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<CreditCardProgressQuery> newList = baseMessage.getDatas(CreditCardProgressQuery.class);
                        list.addAll(newList);
                        notifyDataSetChanged();
                        if(BuildConfig.DEBUG)
                            System.out.println("baseMessage onNext "+ JSON.toJSONString(baseMessage));
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        if(BuildConfig.DEBUG)
                            System.out.println("baseMessage onError "+JSON.toJSONString(baseMessage));
//                                G.showToast(TextUtils.isEmpty(baseMessage.getMsg()) ? "服务器错误，请稍候再试" : baseMessage.getMsg());
                        notifyDataSetChanged();
                    }
                });
    }

    private void notifyDataSetChanged() {
        empty_view.setVisibility(list.isEmpty() ? View.VISIBLE : View.INVISIBLE);
        adapter.notifyDataSetChanged();
        swipe_container.setRefreshing(false);
    }
}
