package com.cooleshow.teacher.ui.homepage;

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

import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.cooleshow.base.constanst.Constants;
import com.cooleshow.base.ui.fragment.BaseMVPFragment;
import com.cooleshow.base.utils.helper.WebStartHelper;
import com.cooleshow.base.widgets.EmptyViewLayout;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.HomePageSheetMusicAdapter;
import com.cooleshow.teacher.bean.HomePageSheetMusicListBean;
import com.cooleshow.teacher.constants.TeacherInfoConstants;
import com.cooleshow.teacher.contract.SheetMusicPageContract;
import com.cooleshow.teacher.databinding.FragmentSheetMusicPageLayoutBinding;
import com.cooleshow.teacher.presenter.homePage.SheetMusicPagePresenter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

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

/**
 * Author by pq, Date on 2022/5/9.
 * 老师个人主页->曲谱列表
 */
public class SheetMusicPageFragment extends BaseMVPFragment<FragmentSheetMusicPageLayoutBinding, SheetMusicPagePresenter> implements SheetMusicPageContract.SheetMusicPageView, View.OnClickListener {

    private HomePageSheetMusicAdapter mAdapter;
    private int currentPage = 1;
    private boolean hasNext = true;
    private EmptyViewLayout mEmptyView;
    private View mCertTipView;
    private String musicPersonCertStatus = TeacherInfoConstants.MUSICIAN_STATUS_UNPAALY;

    @Override
    protected void initView(View rootView) {
        mViewBinding.viewUploadBg.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        mAdapter = new HomePageSheetMusicAdapter(R.layout.item_home_page_sheet_music_layout);
        mViewBinding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                queryList(true);
            }
        });

        mAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                //上拉加载
                if (hasNext) {
                    currentPage++;
                    queryList(false);
                } else {
                    mAdapter.getLoadMoreModule().loadMoreEnd();
                }
            }


        });
        mViewBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mViewBinding.recyclerView.setAdapter(mAdapter);

    }

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

    private void checkCertStatus() {
        if (mViewBinding == null) {
            return;
        }
        if (TextUtils.equals(musicPersonCertStatus, TeacherInfoConstants.MUSICIAN_STATUS_PASS)) {
            mViewBinding.groupContent.setVisibility(View.VISIBLE);
            queryList(true);
            if (mCertTipView != null) {
                mCertTipView.setVisibility(View.GONE);
            }
        } else {
            mViewBinding.groupContent.setVisibility(View.GONE);
            if (mCertTipView == null) {
                mCertTipView = mViewBinding.vsNoCert.inflate();
                mCertTipView.findViewById(R.id.tv_go_cert).setOnClickListener(this);
            } else {
                mCertTipView.setVisibility(View.VISIBLE);
            }
        }
    }

    private void queryList(boolean isShowLoading) {
        presenter.getSheetMusicList(isShowLoading, currentPage);
    }

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

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

    /**
     * 获取乐谱列表
     *
     * @param page
     * @param sheetMusicListBean
     */
    @Override
    public void getSheetMusicListSuccess(int page, HomePageSheetMusicListBean sheetMusicListBean) {
        if (isDetached()) {
            return;
        }
        if (sheetMusicListBean != null) {
            if (page == 1) {
                //第一页
                mViewBinding.refreshLayout.finishRefresh();
                if (mAdapter != null) {
                    mAdapter.getData().clear();
                    mAdapter.notifyDataSetChanged();
                    if (sheetMusicListBean.rows != null && sheetMusicListBean.rows.size() > 0) {
                        checkHasNext(sheetMusicListBean.rows.size());
                        mAdapter.setNewInstance(sheetMusicListBean.rows);
                    } else {
                        showEmptyView();
                    }
                }
            } else {
                //加载更多
                if (mAdapter != null) {
                    if (sheetMusicListBean.rows != null && sheetMusicListBean.rows.size() > 0) {
                        mAdapter.getLoadMoreModule().loadMoreComplete();
                        checkHasNext(sheetMusicListBean.rows.size());
                        mAdapter.addData(sheetMusicListBean.rows);
                    } else {
                        mAdapter.getLoadMoreModule().loadMoreEnd();
                    }
                }
            }
        }
    }

    private void showEmptyView() {
        if (mEmptyView == null) {
            mEmptyView = new EmptyViewLayout(getContext());
        }
        mEmptyView.setContent(com.cooleshow.base.R.drawable.icon_empty_content, "暂无曲谱~");
        mAdapter.setEmptyView(mEmptyView);
    }


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


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

    /**
     * 设置音乐人认证状态
     *
     * @param musicianAuthStatus
     */
    public void setMusicPersonCertStatus(String musicianAuthStatus) {
        if (!TextUtils.equals(this.musicPersonCertStatus, musicianAuthStatus)) {
            this.musicPersonCertStatus = musicianAuthStatus;
            checkCertStatus();
            return;
        }
//        this.musicPersonCertStatus = musicianAuthStatus;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_go_cert) {
            //去认证音乐人
            WebStartHelper.startMusicPersonCertPage();
            return;
        }
        if (v.getId() == R.id.view_upload_bg) {
            //上传乐谱
            WebStartHelper.startUploadSheetMusicPage();
        }
    }
}
