package com.android.chmo.ui.fragment;

import android.view.View;

import com.android.chmo.R;
import com.android.chmo.base.BaseFragment;
import com.android.chmo.constant.Constants;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.BrokerListRes;
import com.android.chmo.http.service.ModelService;
import com.android.chmo.model.BrokerInfo;
import com.android.chmo.ui.adpater.BrokerAdapter;
import com.android.chmo.ui.view.EmptyView;
import com.android.chmo.ui.view.RefreshListView;
import com.google.gson.Gson;

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

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/31.
 */

public class BrokerFragment extends BaseFragment implements RefreshListView.RefreshListener{

    @BindView(R.id.refreshList)
    RefreshListView refreshListView;

    @BindView(R.id.empty)
    EmptyView emptyView;

    private BrokerAdapter brokerAdapter;

    private List<BrokerInfo> mList = new ArrayList<>();
    private int pageNo = 1;

    private boolean isInit = false;

    @Override
    public int getLayoutRes() {
        return R.layout.page_broker;
    }

    @Override
    public void initView() {
        refreshListView.setRefreshListener(this);
        refreshListView.getListView().setDividerHeight(10);

        brokerAdapter = new BrokerAdapter(getBaseActivity());
        refreshListView.setAdapter(brokerAdapter);

    }

    @Override
    public void onVisible() {
        if(!isInit) {
            if(refreshListView != null) {
                refreshListView.autoRefresh();
            }
            isInit = true;
        }
    }

    @Override
    public void onRefresh() {
        loadBroker(1);
    }

    @Override
    public void onLoadMore() {
        refreshListView.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadBroker(pageNo + 1);
            }
        }, 500);
    }




    private void loadBroker(final int page) {
        ModelService.getBrokerList(page, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                refreshListView.finishRefresh();
                BrokerListRes res = new Gson().fromJson(result, BrokerListRes.class);
                if(res.data != null) {
                    pageNo = page;
                    if(page == 1) {
                        mList.clear();
                    }
                    mList.addAll(res.data);
                    brokerAdapter.setList(mList);

                    if(res.data.size() == Constants.PAGE_SIZE) {
                        refreshListView.setEnableLoadMore(true);
                    }else {
                        refreshListView.setNoMoreData();
                    }
                }
                emptyView.setVisibility(mList.size() ==0 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onFailure(String error) {
                refreshListView.finishRefresh();
            }
        });
    }

}
