package com.cooleshow.teacher.ui.homepage;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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.widgets.EmptyViewLayout;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.MineVideoCourseAdapter;
import com.cooleshow.teacher.bean.MineVideoCourseListBean;
import com.cooleshow.teacher.bean.TeacherUserInfo;
import com.cooleshow.teacher.bean.VideoCourseListBean;
import com.cooleshow.teacher.constants.TeacherInfoConstants;
import com.cooleshow.teacher.contract.VideoCoursePageContract;
import com.cooleshow.teacher.presenter.homePage.VideoCoursePagePresenter;
import com.cooleshow.base.ui.fragment.BaseMVPFragment;
import com.cooleshow.teacher.databinding.FragmentVideoCoursePageLayoutBinding;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

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

/**
 * Author by pq, Date on 2022/5/9.
 */
public class VideoCoursePageFragment extends BaseMVPFragment<FragmentVideoCoursePageLayoutBinding, VideoCoursePagePresenter> implements VideoCoursePageContract.VideoCoursePageView, View.OnClickListener {
    private MineVideoCourseAdapter mineVideoCourseAdapter;

    @Override
    protected void initView(View rootView) {
        mViewBinding.llCreateVideo.setOnClickListener(this);
        mViewBinding.tvOpen.setOnClickListener(this);
        RecyclerView rvAddress = mViewBinding.recyclerView;
        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        rvAddress.setLayoutManager(manager);
        mineVideoCourseAdapter = new MineVideoCourseAdapter();
        EmptyViewLayout mEmptyView = new EmptyViewLayout(getContext());
        mEmptyView.setContent(com.cooleshow.base.R.drawable.icon_empty_course, "暂无课程~");
        mineVideoCourseAdapter.setEmptyView(mEmptyView);
        rvAddress.setAdapter(mineVideoCourseAdapter);
        mineVideoCourseAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                VideoCourseListBean.RowsBean rowsBean = mineVideoCourseAdapter.getData().get(position);
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.TEACHER_VIDEO_DETAIL + "?groupId=" + rowsBean.id)
                        .navigation();
            }
        });

    }

    private int currentPage;

    @Override
    protected void initData() {
        mViewBinding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                queryCourse(true);
            }
        });
        mineVideoCourseAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                //上拉加载
                if (hasNext) {
                    currentPage++;
                    queryCourse(false);
                } else {
                    mineVideoCourseAdapter.getLoadMoreModule().loadMoreEnd();
                }
            }
        });

    }

    private boolean hasNext = true;

    /**
     * 检查是否还有下一页
     *
     * @param dataSize
     */
    private void checkHasNext(int dataSize) {
        hasNext = dataSize >= Constants.DEFAULT_DATA_SIZE;
    }

    private void queryCourse(boolean isLoading) {
        presenter.queryVideoCourseGroup(isLoading, currentPage);
    }

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

    @Override
    protected VideoCoursePagePresenter createPresenter() {
        return new VideoCoursePagePresenter();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_create_video:
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.TEACHER_VIDEO_CREATE)
                        .navigation();
                break;
            case R.id.tv_open:
                if (mViewBinding.tvOpen.getText().toString().equals("去认证")) {
                    ARouter.getInstance().build(RouterPath.WebCenter.ACTIVITY_HTML)
                            .withString(WebConstants.WEB_URL, WebConstants.TEACHER_CERT)
                            .navigation();
                } else if (mViewBinding.tvOpen.getText().toString().equals("立即开通")) {
                    ARouter.getInstance()
                            .build(RouterPath.WebCenter.ACTIVITY_HTML)
                            .withString(WebConstants.WEB_URL, WebConstants.TEACHER_OPEN_LIVE)
                            .navigation();
                }

                break;
        }
    }

    @Override
    public void queryVideoCourseGroupSuccess(int page, VideoCourseListBean data) {
        if (isDetached()) {
            return;
        }
        if (data != null) {
            if (page == 1) {
                mViewBinding.refreshLayout.finishRefresh();
                mineVideoCourseAdapter.getData().clear();
                mineVideoCourseAdapter.notifyDataSetChanged();
                if (data.rows != null && data.rows.size() > 0) {
                    checkHasNext(data.rows.size());
                    mineVideoCourseAdapter.setNewInstance(data.rows);
                }
            } else {
                if (data.rows != null && data.rows.size() > 0) {
                    mineVideoCourseAdapter.getLoadMoreModule().loadMoreComplete();
                    mineVideoCourseAdapter.addData(data.rows);
                    checkHasNext(data.rows.size());
                } else {
                    mineVideoCourseAdapter.getLoadMoreModule().loadMoreEnd();
                }
            }

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.getTeacherUserInfo();
    }

    @Override
    public void queryVideoCourseGroupError(int page) {
        if (isDetached()) {
            return;
        }
        if (page == 1) {
            mViewBinding.refreshLayout.finishRefresh();
        } else {
            if (mineVideoCourseAdapter != null) {
                currentPage--;
                mineVideoCourseAdapter.getLoadMoreModule().loadMoreFail();
            }
        }
    }

    @Override
    public void getTeacherInfoSuccess(TeacherUserInfo teacherUserInfo) {
        if (TextUtils.equals(teacherUserInfo.entryStatus, TeacherInfoConstants.ENTRY_STATUS_DOING)) {
            //入驻审核中
            mViewBinding.llContent.setVisibility(View.GONE);
            mViewBinding.clEmptyFlag.setVisibility(View.VISIBLE);
            mViewBinding.tvEmptyHint.setText("达人认证审核中！");
            mViewBinding.tvOpen.setVisibility(View.GONE);
        } else if (TextUtils.equals(teacherUserInfo.entryStatus, TeacherInfoConstants.ENTRY_STATUS_PASS)) {
            //入驻审核通过
            mViewBinding.llContent.setVisibility(View.VISIBLE);
            mViewBinding.clEmptyFlag.setVisibility(View.GONE);
            currentPage = 1;
            queryCourse(true);
        } else {
            //未通过或者未申请
            mViewBinding.llContent.setVisibility(View.GONE);
            mViewBinding.clEmptyFlag.setVisibility(View.VISIBLE);
            mViewBinding.tvEmptyHint.setText("您还没有完成达人认证，认证后才可创建直播课哦~");
            mViewBinding.tvOpen.setVisibility(View.VISIBLE);
            mViewBinding.tvOpen.setText("去认证");
        }

    }
}
