package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.NewbieTask;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.view.adapter.NewbieTaskAdp;
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.gamegroup.absBase.BaseActivity;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

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

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

/**
 * 新手任务
 * Created by realicing on 2018/4/18.
 */

public class ActNewbieTaskList extends BaseActivity {
    @BindView(R.id.withdrawal_listview)
    ListView withdrawalListview;
    @BindView(R.id.swipe_container)
    RefreshLayout swipe_container;

    private Activity activity;
    private NewbieTaskAdp adp;
    private List<NewbieTask> etyList = new ArrayList<>();
    private List<NewbieTask> newbieTaskList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.act_newbie_task_list;
    }

    @Override
    public void initView() {
        activity = this;
        TitleBarUtils.getInstance()
                .setTitle(this, "任务中心")
                .setTitleFinish(this);
        swipe_container.setFooterView(this, withdrawalListview, R.layout.listview_footer);
        adp = new NewbieTaskAdp(activity, etyList);
        withdrawalListview.setAdapter(adp);
        swipe_container.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
    }

    @Override
    public void initListener() {

    }

    private void setRefresh(){
        if(swipe_container.isRefreshing()){
            return;
        }
        swipe_container.setRefreshing(true);
        initData();
    }
    @Override
    public void initData() {
        etyList.clear();
        SheepApp.getInstance().getNetComponent().getApiService().getNewbieTask()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(activity) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        swipe_container.setRefreshing(false);
                        newbieTaskList = baseMessage.getDatas(NewbieTask.class);
                        etyList.addAll(newbieTaskList);
                        adp.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        swipe_container.setRefreshing(false);
                        etyList.addAll(newbieTaskList);
                        adp.notifyDataSetChanged();
                    }
                });
    }
    private boolean isFirst = true;
    @Override
    protected void onResume() {
        super.onResume();
        if(isFirst){
            isFirst = false;
        } else {
            setRefresh();
        }
    }
}
