package com.cooleshow.teacher.ui.score;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

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

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.constanst.Constants;
import com.cooleshow.base.ui.fragment.BaseMVPFragment;
import com.cooleshow.base.utils.helper.WebStartHelper;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.MineScoreAdapter;
import com.cooleshow.teacher.bean.MusicSheetListBean;
import com.cooleshow.teacher.constants.TeacherInfoConstants;
import com.cooleshow.teacher.contract.MineScoreContract;
import com.cooleshow.teacher.databinding.FragmentMineScoreBinding;
import com.cooleshow.teacher.presenter.score.MineScorePresenter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

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

/**
 * 创建日期：2022/5/20 17:00
 *
 * @author Ryan
 * 类说明：
 */
public class MineScoreFragment extends BaseMVPFragment<FragmentMineScoreBinding, MineScorePresenter> implements MineScoreContract.MineScoreView, View.OnClickListener {

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_upload_score:
                WebStartHelper.startUploadSheetMusicPage();
                break;
        }
    }

    public static MineScoreFragment newInstance(String auditStatus,String musicianAuthStatus) {
        Bundle args = new Bundle();
        MineScoreFragment fragment = new MineScoreFragment();
        args.putString("auditStatus", auditStatus);
        args.putString("musicianAuthStatus",musicianAuthStatus);
        fragment.setArguments(args);
        return fragment;
    }

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

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

    private String auditStatus = "";
    private String musicianAuthStatus="";
    private MineScoreAdapter mineScoreAdapter;
    List<MusicSheetListBean.RowsBean> dataList = new ArrayList<>();

    @Override
    protected void initView(View rootView) {
        auditStatus = getArguments().getString("auditStatus");
        musicianAuthStatus=getArguments().getString("musicianAuthStatus");
        RecyclerView rvAddress = mViewBinding.recyclerView;
        mViewBinding.llUploadScore.setOnClickListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        rvAddress.setLayoutManager(manager);
        mineScoreAdapter = new MineScoreAdapter(dataList);
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View emptyLayout = inflater.inflate(R.layout.layout_empty_conent, null);
        ImageView im_empty_logo=emptyLayout.findViewById(R.id.im_empty_logo);
        im_empty_logo.setBackgroundResource(R.drawable.icon_empty_score);
        TextView tv_empty_hint=emptyLayout.findViewById(R.id.tv_empty_hint);
        tv_empty_hint.setText("暂无收藏~");
        mineScoreAdapter.setEmptyView(emptyLayout);
        rvAddress.setAdapter(mineScoreAdapter);
        switch (musicianAuthStatus){
            case TeacherInfoConstants.MUSICIAN_STATUS_PASS:
                mViewBinding.llContent.setVisibility(View.VISIBLE);
                mViewBinding.clEmpty.setVisibility(View.GONE);
                break;
            case TeacherInfoConstants.MUSICIAN_STATUS_DOING:
                mViewBinding.llContent.setVisibility(View.GONE);
                mViewBinding.clEmpty.setVisibility(View.VISIBLE);
                mViewBinding.tvEmptyHint.setText("您已提交认证申请\n清耐心等待审核结果~");
                break;
            case TeacherInfoConstants.MUSICIAN_STATUS_UNPAALY:
                mViewBinding.llContent.setVisibility(View.GONE);
                mViewBinding.clEmpty.setVisibility(View.VISIBLE);
                mViewBinding.tvEmptyHint.setText("您还未提交认证申请");
                break;
            case TeacherInfoConstants.MUSICIAN_STATUS_UNPASS:
                mViewBinding.llContent.setVisibility(View.GONE);
                mViewBinding.clEmpty.setVisibility(View.VISIBLE);
                mViewBinding.tvEmptyHint.setText("您已提交认证申请未通过\n清重新提交~");
                break;
        }

        mineScoreAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

            }
        });

    }

    private int currentPage;

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

    private boolean hasNext = true;

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

    private void queryScore(boolean isLoading) {
        presenter.queryPageCourseGroup(isLoading, currentPage, auditStatus);
    }

    @Override
    public void queryMusicListSuccess(int page, MusicSheetListBean data) {
        if (isDetached()) {
            return;
        }
        if (data != null) {
            if (page == 1) {
                dataList.clear();
                mViewBinding.refreshLayout.finishRefresh();
                if (data.rows != null && data.rows.size() > 0) {
                    checkHasNext(data.rows.size());
                    dataList.addAll(data.rows);
                }
                mineScoreAdapter.notifyDataSetChanged();
            } else {
                if (data.rows != null && data.rows.size() > 0) {
                    mineScoreAdapter.getLoadMoreModule().loadMoreComplete();
                    checkHasNext(data.rows.size());
                } else {
                    mineScoreAdapter.getLoadMoreModule().loadMoreEnd();
                }
            }
        }

    }

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