package com.tenlionsoft.oamodule.activity.meeting;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

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.MeetingMineApplyAdapter;
import com.tenlionsoft.oamodule.beans.MeetingRecordListBean;
import com.tenlionsoft.oamodule.beans.MeetingRoomRecordBean;
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 io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * 作者: Adam
 * 日期: 2022年04月15日15:26:46
 * 邮箱: itgaojian@163.com
 * 描述: 我发起的会议列表
 */
@Route(path = PathConfig.PATH_MODULE_OA_ACTIVITY_MEETING_MINE_APPLY)
public class MineMeetingListActivity extends BaseActivity {

    @BindView(R2.id.rlv_content)
    RecyclerView mRlvContent;
    @BindView(R2.id.srl_content)
    SmartRefreshLayout mSrlContent;
    @BindView(R2.id.pb_loading)
    ProgressBar mPbLoading;
    @BindView(R2.id.iv_empty_data)
    ImageView mIvEmptyData;
    @BindView(R2.id.tv_error_hint)
    TextView mTvErrorHint;
    @BindView(R2.id.ll_hint)
    LinearLayout mLlHint;
    @BindView(R2.id.et_base_search_cancel)
    EditText mEtBaseSearchCancel;
    @BindView(R2.id.tv_base_search_cancel)
    TextView mTvBaseSearchCancel;
    @BindView(R2.id.ll_base_search_cancel)
    LinearLayout mLlBaseSearchCancel;
    private MeetingMineApplyAdapter mAdapter;
    private List<MeetingRoomRecordBean> mDatas;
    private int mCurrentPage = 1;
    private String mKeywords = "";

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

    @Override
    public void initData() {
        ButterKnife.bind(this);
        mTvBaseTitle.setText("我的会议");
        refreshView(STATE_LOAD_SUCCESS);

        mSrlContent.setOnLoadMoreListener(v -> doLoadMore());
        mSrlContent.setOnRefreshListener(v -> doRefresh());
        mDatas = new ArrayList<>();
        mAdapter = new MeetingMineApplyAdapter(mActivity, mDatas);
        mRlvContent.setLayoutManager(new LinearLayoutManager(mActivity));
        mRlvContent.setAdapter(mAdapter);
        mAdapter.addOnItemClickListener(rowsBean -> ARouter.getInstance()
            .build(PathConfig.PATH_MODULE_OA_ACTIVITY_MEETING_DETAIL)
            .withString("id", rowsBean.getMeetingId())
            .navigation(mActivity, 12));
        refreshStateView(LionActions.STATE_LOAD);
        doRefresh();

    }

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

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

    /**
     * 获取文档列表
     */
    private void getDocumentList(int currentPage) {
        mCurrentPage = currentPage;
        RetrofitManager.getInstance()
            .create(OAApi.class)
            .getMineApplyMeetingList(mKeywords, mCurrentPage + "", UserLgUtils.getToken())
            .compose(RxTransformer.getTransformer())
            .subscribe(new Observer<MeetingRecordListBean>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull MeetingRecordListBean listBean) {
                    if (listBean.getRows() != null && listBean.getRows().size() > 0) {
                        ++mCurrentPage;
                        if (currentPage == 1) {
                            mDatas.clear();
                            mDatas.addAll(listBean.getRows());
                        } else {
                            mDatas.addAll(listBean.getRows());
                        }
                        refreshStateView(LionActions.STATE_SUCCESS);
                        mAdapter.notifyDataSetChanged();
                        mSrlContent.finishRefresh();
                        if (mDatas.size() >= listBean.getTotal()) {
                            mSrlContent.finishLoadMore();
                            mSrlContent.setNoMoreData(true);
                        } else {
                            mSrlContent.finishLoadMore();
                            mSrlContent.setNoMoreData(false);
                        }
                    } else {
                        mSrlContent.finishRefresh();
                        mSrlContent.finishLoadMore();
                        if (currentPage > 1) {
                            mSrlContent.setNoMoreData(true);
                        } else {
                            if (TextUtils.isEmpty(mKeywords)) {
                                //无数据
                                refreshStateView(LionActions.STATE_EMPTY);
                            } else {
                                refreshStateView(LionActions.STATE_EMPTY);
                                ToastUtils.show("未查询到相关内容");
                            }
                        }
                    }
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    mSrlContent.finishLoadMore();
                    mSrlContent.finishRefresh();
                    ExceptionHandler.handleException(e);
                    refreshStateView(LionActions.STATE_ERROR);
                }

                @Override
                public void onComplete() {

                }
            });
    }

    private void refreshStateView(int state) {
        switch (state) {
            case LionActions.STATE_LOAD:
                mRlvContent.setVisibility(View.GONE);
                mLlHint.setVisibility(View.VISIBLE);
                mPbLoading.setVisibility(View.VISIBLE);
                mIvEmptyData.setVisibility(View.GONE);
                mTvErrorHint.setVisibility(View.VISIBLE);
                mTvErrorHint.setText("加载中...");
                break;
            case LionActions.STATE_EMPTY:
                mRlvContent.setVisibility(View.GONE);
                mLlHint.setVisibility(View.VISIBLE);
                mPbLoading.setVisibility(View.GONE);
                mIvEmptyData.setVisibility(View.VISIBLE);
                mIvEmptyData.setImageResource(R.drawable.ic_empty_data);
                mTvErrorHint.setVisibility(View.VISIBLE);
                mTvErrorHint.setText("暂无数据");
                break;
            case LionActions.STATE_ERROR:
                mRlvContent.setVisibility(View.GONE);
                mLlHint.setVisibility(View.VISIBLE);
                mPbLoading.setVisibility(View.GONE);
                mIvEmptyData.setVisibility(View.VISIBLE);
                mIvEmptyData.setImageResource(R.drawable.ic_load_error);
                mTvErrorHint.setVisibility(View.VISIBLE);
                mTvErrorHint.setText("加载失败,请稍后重试");
                break;
            case LionActions.STATE_SUCCESS:
                mRlvContent.setVisibility(View.VISIBLE);
                mLlHint.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    protected void doSearchByTitle() {

    }

    @Override
    protected void clearSearch() {

    }
}