package com.tjzhxx.union.fragment;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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.NewAdapter;
import com.tjzhxx.union.entity.BaseResponse;
import com.tjzhxx.union.entity.NewBean;
import com.tjzhxx.union.entity.request.NewListRequest;
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.BaseFragment;
import com.tjzhxx.union.system.WebviewActivity;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

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

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

/**
 * 描述:
 *
 * @author yhx
 * @create 2021/9/13  11:32 PM
 */
public class NewListFragment extends BaseFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private NewAdapter mAdapter;
    private List<NewBean> newBeans = new ArrayList<>();
    private UnionServices services;
    private int pageNo = 1, stepSize = 10;
    private boolean isHasMore = false;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_new_list, container, false);
        ButterKnife.bind(this, view);
        services = RetrofitUtils.createApi(UnionServices.class, ConstDefine.HttpAdress);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new NewAdapter(getActivity(), newBeans);
        mAdapter.setOnItemClickListener(new NewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, NewBean entity) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), WebviewActivity.class);
                intent.putExtra("url", entity.getWxurl());
                intent.putExtra("title", "工会新闻");
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(mAdapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                pageNo = 1;
                loadDate();
            }
        });

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

    //加载新闻
    private void loadDate() {
        NewListRequest request = new NewListRequest();
        request.setPageNum(pageNo);
        request.setPageSize(stepSize);
        services.querynewlist(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse<List<NewBean>>>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new DefaultSubscriber<BaseResponse<List<NewBean>>>(getActivity()) {
                    @Override
                    public void success(BaseResponse<List<NewBean>> value) {
                        if (value.getResultData() != null) {
                            List<NewBean> beanList = value.getResultData();
                            if (beanList != null) {
                                if (pageNo == 1) {
                                    newBeans.clear();
                                }
                                if (beanList.size() == stepSize) {
                                    isHasMore = true;
                                    pageNo++;
                                } else {
                                    isHasMore = false;
                                }
                                newBeans.addAll(beanList);
                            } else {
                                isHasMore = false;
                            }
                        } else {
                            showSnackBar("加载失败");
                        }
                        mAdapter.notifyDataSetChanged();
                        refreshComplete(refreshLayout, isHasMore);
                    }

                });
    }
}
