package com.android.chmo.ui.activity.order;

import android.view.View;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.constant.Constants;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.VideoOrderRes;
import com.android.chmo.http.service.OrderService;
import com.android.chmo.ui.activity.model.VideoOrderInfo;
import com.android.chmo.ui.adpater.VideoOrderAdapter;
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/8/20.
 */

public class VideoOrderActivity extends BaseActivity implements RefreshListView.RefreshListener{

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

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

    private VideoOrderAdapter orderAdapter;

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


    @Override
    protected int getContentResId() {
        return R.layout.activity_video_order;
    }

    @Override
    public void initView() {
        refreshListView.setRefreshListener(this);
        refreshListView.getListView().setDividerHeight(15);
        orderAdapter = new VideoOrderAdapter(this);
        refreshListView.setAdapter(orderAdapter);

        refreshListView.autoRefresh();
    }

    @Override
    public void onRefresh() {
        this.loadOrder(1);
    }

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



    private void loadOrder(final int page) {
        OrderService.getVideoOrder(page, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                refreshListView.finishRefresh();
                VideoOrderRes res = new Gson().fromJson(result, VideoOrderRes.class);
                if(res.data != null) {
                    if(page == 1) {
                        mList.clear();
                    }
                    mList.addAll(res.data);
                    pageNo = page;
                    orderAdapter.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();
            }
        });
    }
}
