package com.tenlionsoft.oamodule.activity.document;

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.LionActions;
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.DocumentAdapter;
import com.tenlionsoft.oamodule.beans.DocumentListBean;
import com.tenlionsoft.oamodule.net.OAApi;

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

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月11日14:21:30
 * 邮箱: itgaojian@163.com
 * 描述: 文档中心
 */
@Route(path = PathConfig.PATH_MODULE_OA_ACTIVITY_DOCUMENT)
public class DocumentCenterActivity extends BaseActivity {

    @BindView(R2.id.rlv_content)
    RecyclerView mRlvContent;
    @BindView(R2.id.srl_content)
    SmartRefreshLayout mSrlContent;
    private Unbinder mBind;
    private List<DocumentListBean.RowsBean> mDatas;
    private DocumentAdapter mAdapter;
    private int mCurrentPage = 1;
    private String mKeywords = "";

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

    @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_DOCUMENT_ADD)
            .navigation());
        mDatas = new ArrayList<>();
        mAdapter = new DocumentAdapter(mActivity, mDatas);
        mRlvContent.setLayoutManager(new LinearLayoutManager(mActivity));
        mRlvContent.setAdapter(mAdapter);
        mAdapter.addOnItemClickListener(rowsBean -> ARouter.getInstance()
            .build(PathConfig.PATH_MODULE_OA_ACTIVITY_DOCUMENT_DETAIL)
            .withString("id", rowsBean.getFileUploadId())
            .navigation(mActivity, LionActions.REQUEST_CODE_AC));

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


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

                }

                @Override
                public void onNext(@NonNull DocumentListBean 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() {

                }
            });
    }

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

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


    @Override
    protected void doSearchByTitle() {

    }

    @Override
    protected void clearSearch() {

    }
}