package com.tenlionsoft.oamodule.activity.car;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.tenlionsoft.baselib.constant.PathConfig;
import com.tenlionsoft.baselib.core.retrofit_net.RetrofitManager;
import com.tenlionsoft.baselib.core.retrofit_net.conver.RxTransformer;
import com.tenlionsoft.baselib.core.widget.base.BaseActivity;
import com.tenlionsoft.baselib.utils.ExceptionHandler;
import com.tenlionsoft.baselib.utils.UserLgUtils;
import com.tenlionsoft.oamodule.R;
import com.tenlionsoft.oamodule.R2;
import com.tenlionsoft.oamodule.adapter.ByCarRecordAdapter;
import com.tenlionsoft.oamodule.beans.ByCarRecordBean;
import com.tenlionsoft.oamodule.beans.ByCarRecordListBean;
import com.tenlionsoft.oamodule.net.OAApi;

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

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * 作者: Adam
 * 日期: 2022年04月12日17:44:37
 * 邮箱: itgaojian@163.com
 * 描述: 用车管理
 */
@Route(path = PathConfig.PATH_MODULE_OA_ACTIVITY_CAR)
public class ByCarListActivity extends BaseActivity {

    @BindView(R2.id.rlv_record)
    RecyclerView mRlvRecord;
    @BindView(R2.id.srl_content)
    SmartRefreshLayout mSrlContent;
    private Unbinder mBind;

    private List<ByCarRecordBean> mDatas;
    private ByCarRecordAdapter mAdapter;
    private int mCurrentPage = 1;
    private String mKeywords = "";

    @Override
    protected int setLayoutId() {
        return R.layout.activity_by_car_list;
    }

    @Override
    public void initData() {
        mBind = ButterKnife.bind(this);
        mTvBaseTitle.setText("用车申请");
        mTvPublish.setVisibility(View.VISIBLE);
        mTvPublish.setText("申请");
        mTvPublish.setOnClickListener(v -> ARouter.getInstance()
            .build(PathConfig.PATH_MODULE_OA_ACTIVITY_CAR_ADD)
            .navigation(mActivity, 12));

        mDatas = new ArrayList<>();
        mAdapter = new ByCarRecordAdapter(mActivity, mDatas);
        mRlvRecord.setLayoutManager(new LinearLayoutManager(mActivity));
        mRlvRecord.setAdapter(mAdapter);
        mAdapter.addOnItemClickListener(rowsBean -> ARouter.getInstance()
            .build(PathConfig.PATH_MODULE_OA_ACTIVITY_CAR_RECORD_DETAIL)
            .withString("id", rowsBean.getCarApplyId())
            .navigation(mActivity, 12));

        mSrlContent.setOnLoadMoreListener(v -> doLoadMore());
        mSrlContent.setOnRefreshListener(v -> doRefresh());
        doRefresh();
    }

    private void getDocumentList(int currentPage) {
        mCurrentPage = currentPage;
        RetrofitManager.getInstance()
            .create(OAApi.class)
            .getByCarRecordForMe(mKeywords, mCurrentPage + "", UserLgUtils.getToken())
            .compose(RxTransformer.getTransformer())
            .subscribe(new Observer<ByCarRecordListBean>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull ByCarRecordListBean listBean) {

                    if (listBean.getRows() != null && listBean.getRows().size() > 0) {
                        ++mCurrentPage;
                        if (currentPage == 1) {
                            mDatas.clear();
                            mDatas.addAll(listBean.getRows());
                        } else {
                            mDatas.addAll(listBean.getRows());
                        }
                        refreshView(STATE_LOAD_SUCCESS);
                        mAdapter.notifyDataSetChanged();
                        mSrlContent.finishRefresh();
                        if (mDatas.size() >= listBean.getTotal()) {
                            mSrlContent.finishLoadMore();
                            mSrlContent.setNoMoreData(true);
                        } else {
                            mSrlContent.finishLoadMore();
                            mSrlContent.setNoMoreData(false);
                        }
                    } else {
                        if (currentPage > 1) {
                            mSrlContent.finishLoadMore();
                            mSrlContent.setNoMoreData(true);
                        } else {
                            if (TextUtils.isEmpty(mKeywords)) {
                                //无数据
                                refreshView(STATE_LOAD_EMPTY);
                            } else {
                                refreshView(STATE_LOAD_EMPTY);
                                ToastUtils.show("未查询到相关内容");
                            }
                        }
                    }
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    mSrlContent.finishLoadMore();
                    mSrlContent.finishRefresh();
                    ExceptionHandler.handleException(e);
                    refreshView(STATE_LOAD_ERROR);
                }

                @Override
                public void onComplete() {

                }
            });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 12) {
            if (resultCode == RESULT_OK) {
                doRefresh();
            }
        }
    }

    //加载更多
    private void doLoadMore() {
        getDocumentList(mCurrentPage);
    }

    //刷新
    private void doRefresh() {
        mDatas.clear();
        mAdapter.setData(mDatas);
        getDocumentList(1);
    }

    @Override
    protected void doSearchByTitle() {

    }

    @Override
    protected void clearSearch() {

    }

}