package com.tjzhxx.union.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.tjzhxx.union.R;
import com.tjzhxx.union.adapter.WelfareAdapter;
import com.tjzhxx.union.entity.BaseResponse;
import com.tjzhxx.union.entity.WelfareResponse;
import com.tjzhxx.union.entity.request.WelfareListRequest;
import com.tjzhxx.union.public_store.ConstDefine;
import com.tjzhxx.union.public_store.retrofit.RetrofitUtils;
import com.tjzhxx.union.public_store.retrofit.UnionServices;
import com.tjzhxx.union.public_store.rx.DefaultSubscriber;
import com.tjzhxx.union.system.BaseActivity;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

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

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

/**
 * 描述:
 *
 * @author yhx
 * @create 2021/10/18  11:47 AM
 */
public class WelfareListActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private UnionServices services;
    private int pageNo = 1, stepSize = 10;
    private boolean isHasMore = false;
    private List<WelfareResponse> list = new ArrayList<>();
    private WelfareAdapter adapter;
    @Override
    public int getContentViewLayoutID() {
        return R.layout.activity_welfare_list;
    }

    @Override
    public void bindViewAndEvent(Bundle savedInstanceState) {
        initTitle("会员福利");
        services = RetrofitUtils.createApi(UnionServices.class, ConstDefine.HttpAdress);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new WelfareAdapter(this, list);
        adapter.setOnItemClickListener(new WelfareAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, WelfareResponse entity) {
                if (entity.getStatus() != 1){
                    Intent intent = new Intent(WelfareListActivity.this,WelfareSignatureSureActivity.class);
                    intent.putExtra("wfid",entity.getWfinfo().getId());
                    startActivity(intent);
                }
            }
        });
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                pageNo = 1;
                loadDate();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                loadDate();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshLayout.autoRefresh();
    }

    private void loadDate() {
        WelfareListRequest request = new WelfareListRequest();
        request.setPage(pageNo);
        request.setSize(stepSize);
        request.setWid(4);
        services.getalllistbywid(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse<List<WelfareResponse>>>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new DefaultSubscriber<BaseResponse<List<WelfareResponse>>>(this) {
                    @Override
                    public void success(BaseResponse<List<WelfareResponse>> value) {
                        if (value.getData() != null) {
                            List<WelfareResponse> beanList = value.getData();
                            if (beanList != null) {
                                if (pageNo == 1) {
                                    list.clear();
                                }
                                if (beanList.size() == stepSize) {
                                    isHasMore = true;
                                    pageNo++;
                                } else {
                                    isHasMore = false;
                                }
                                list.addAll(beanList);
                            } else {
                                isHasMore = false;
                            }
                        } else {
                            showSnackBar("加载失败");
                        }
                        adapter.notifyDataSetChanged();
                        refreshComplete(refreshLayout, isHasMore);
                    }
                });
    }

}
