package com.cooleshow.student.ui.main;

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

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.constanst.Constants;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.fragment.BaseFragment;
import com.cooleshow.base.widgets.EmptyViewLayout;
import com.cooleshow.student.R;
import com.cooleshow.student.adapter.HomeTalentStyleAdapter;
import com.cooleshow.student.bean.HomeStyleBean;
import com.cooleshow.student.databinding.FragmentHomeTalentLayoutBinding;
import com.cooleshow.student.ui.live.LiveRoomActivity;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
 * Author by pq, Date on 2022/8/30.
 */
public class TalentStyleFragment extends BaseFragment<FragmentHomeTalentLayoutBinding> {
    RecyclerView recyclerView;
    private HomeTalentStyleAdapter mTalentStyleAdapter;
    private boolean hasNext = true;
    private int currentPage = 1;
    private OnEventListener onEventListener;

    @Override
    protected FragmentHomeTalentLayoutBinding getLayoutView() {
        return FragmentHomeTalentLayoutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView(View rootView) {
        recyclerView = mViewBinding.recyclerView;
    }

    @Override
    protected void initData() {
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        mTalentStyleAdapter = new HomeTalentStyleAdapter();
        EmptyViewLayout emptyViewLayout = new EmptyViewLayout(getContext());
        emptyViewLayout.setContent(com.cooleshow.base.R.drawable.icon_empty_content, "暂无内容");
        mTalentStyleAdapter.setEmptyView(emptyViewLayout);
        recyclerView.setAdapter(mTalentStyleAdapter);
        mTalentStyleAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (position < mTalentStyleAdapter.getData().size()) {
                    HomeStyleBean.RowsBean item = mTalentStyleAdapter.getData().get(position);
                    if (TextUtils.equals(item.liveStatus, Constants.ON_LIVING_TAG)) {
                        LiveRoomActivity.startLiveRoomActivity(getContext(), item.roomUid);
                    } else {
                        ARouter.getInstance()
                                .build(RouterPath.WebCenter.ACTIVITY_HTML)
                                .withString(WebConstants.WEB_URL, WebConstants.STUDENT_TEACHER_HOME + item.userId)
                                .navigation();
                    }
                }
            }
        });
        mTalentStyleAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                //上拉加载
                if (hasNext) {
                    currentPage++;
                    queryMoreData();
                } else {
                    mTalentStyleAdapter.getLoadMoreModule().loadMoreEnd();
                }
            }
        });
    }

    public void refresh() {
        if (onEventListener != null) {
            currentPage = 1;
            onEventListener.getStyleData(currentPage);
        }
    }

    private void queryMoreData() {
        if (onEventListener != null) {
            onEventListener.getStyleData(currentPage);
        }
    }

    public void setData(int page, HomeStyleBean data) {
        if (mTalentStyleAdapter != null) {
            if (page == 1) {
                mTalentStyleAdapter.getData().clear();
                mTalentStyleAdapter.notifyDataSetChanged();
                if (data != null && data.rows != null && data.rows.size() > 0) {
                    checkHasNext(data.rows.size());
                    mTalentStyleAdapter.setNewInstance(data.rows);
                } else {
                    //showEmpty
                }
            } else {
                if (data != null && data.rows != null && data.rows.size() > 0) {
                    mTalentStyleAdapter.getLoadMoreModule().loadMoreComplete();
                    checkHasNext(data.rows.size());
                    mTalentStyleAdapter.addData(data.rows);
                } else {
                    mTalentStyleAdapter.getLoadMoreModule().loadMoreEnd();
                }
            }
        }
    }

    private void checkHasNext(int dataSize) {
        hasNext = dataSize >= Constants.DEFAULT_DATA_SIZE;
    }

    public void setOnEventListener(OnEventListener onEventListener) {
        this.onEventListener = onEventListener;
    }

    public interface OnEventListener {
        void getStyleData(int page);
    }
}
