package com.sheep.gamegroup.view.activity;

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

import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.GameTaskOrderEnty;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.view.adapter.AdpGameTaskOrderList;
import com.sheep.gamegroup.view.customview.RefreshLayout;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

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

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

/**
 * Created by ljy on 2018/6/15.
 */

public class GameTaskOrderListAct extends BaseActivity {
    @BindView(R.id.withdrawal_listview)
    ListView withdrawalListview;
    @BindView(R.id.swipe_container)
    RefreshLayout swipe_container;
    @BindView(R.id.empty_view)
    View empty_view;
    private Activity activity;

    private int page = 1;//页数
    private int per_page = DataUtil.PER_PAGE;
    private ArrayList<GameTaskOrderEnty> listData = new ArrayList<>();
    private AdpGameTaskOrderList adp;
    @Override
    protected int getLayoutId() {
        return R.layout.withdrawal_list;
    }

    @Override
    public void initView() {
        activity = this;
        TitleBarUtils.getInstance()
                .setShowOrHide(this, true)
                .setTitle(this, "订单管理")
                .setTitleFinish(this);
        EventBus.getDefault().register(this);
        swipe_container.setFooterView(this, withdrawalListview, R.layout.listview_footer);
        adp = new AdpGameTaskOrderList(activity , listData);
        withdrawalListview.setAdapter(adp);

        swipe_container.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });

        swipe_container.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                swipe_container.setLoading(false);
                if(listData != null && listData.size() >= per_page*page){
                    page += 1;
                    initData();
                }else {

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            swipe_container.setRefreshing(false);
                        }
                    },1000);
                }

            }
        });

    }

    private void refreshData() {
        listData.clear();
        page = 1;
        initData();
    }


    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        empty_view.setVisibility(View.INVISIBLE);
        SheepApp.getInstance().getNetComponent()
                .getApiService()
                .getConsumptionOrders(page, per_page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(activity) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<GameTaskOrderEnty> arrayList = baseMessage.getDatas(GameTaskOrderEnty.class);
                        if(arrayList != null && arrayList.size()>0){
                            listData.addAll(arrayList);
                        }
                        notifyDataSetChanged();
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        notifyDataSetChanged();
                        G.showToast(baseMessage.getMsg()+"");
                    }
                });
    }
    private void notifyDataSetChanged(){
        if(swipe_container == null || empty_view == null){
            return;
        }
        CommonUtil.getInstance().updateEmptyView(empty_view, listData.isEmpty());
        hideProgress();
        swipe_container.setRefreshing(false);
        adp.notifyDataSetChanged();
    }

    @Subscribe
    public void onEventMainThread(BigEvent event) {
        switch (event.getEventTypes()) {
            case ONFRESH_GAMETASK_ORDER_LIST_PAGE:
                refreshData();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
