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

import android.os.Bundle;
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.OrderListRes;
import com.android.chmo.http.service.OrderService;
import com.android.chmo.model.OrderInfo;
import com.android.chmo.ui.adpater.OrderAdapter;
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/4.
 */

public class OrderFragment extends BaseFragment implements RefreshListView.RefreshListener{
    @BindView(R.id.refreshList)
    RefreshListView refreshListView;
    @BindView(R.id.empty)
    EmptyView emptyView;

    private OrderAdapter orderAdapter;

    private int pageNo = 1;
    private List<OrderInfo> mList = new ArrayList<>();
    private boolean isInit = false;

    private String orderState;

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

    @Override
    public void initView() {
        Bundle args = getArguments();
        if (null != args) {
            orderState = args.getString("state");
        }

        refreshListView.setRefreshListener(this);
        refreshListView.getListView().setDividerHeight(15);
        orderAdapter = new OrderAdapter(getBaseActivity());
        refreshListView.setAdapter(orderAdapter);
        orderAdapter.setRefreshCallback(new OrderAdapter.RefreshCallback() {
            @Override
            public void onRefresh() {
                loadOrder(1);
            }
        });
        if (orderState.equals("all")) {
            isInit = true;
            refreshListView.autoRefresh();
        }
    }


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

    @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.getOrderList(orderState, page, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                refreshListView.finishRefresh();
                OrderListRes res = new Gson().fromJson(result, OrderListRes.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();
            }
        });
    }
}
