package com.cooleshow.teacher.adapter;

import android.media.Image;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.BaseLoadMoreModule;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cooleshow.base.constanst.SheetMusicConstants;
import com.cooleshow.base.presenter.view.BaseView;
import com.cooleshow.base.utils.GlideUtils;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.HomePageSheetMusicListBean;

import androidx.annotation.NonNull;

/**
 * Author by pq, Date on 2022/5/10.
 */
public class HomePageSheetMusicAdapter extends BaseQuickAdapter<HomePageSheetMusicListBean.RowsBean, BaseViewHolder> implements LoadMoreModule {


    public HomePageSheetMusicAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, HomePageSheetMusicListBean.RowsBean bean) {
        //曲谱名称
        holder.setText(R.id.tv_title, bean.musicSheetName);
        //音乐人
        holder.setText(R.id.tv_author, bean.composer);
        //创建人
        holder.setText(R.id.tv_nickname, bean.addName);
        ImageView cir_avatar = holder.getView(R.id.cir_avatar);
        GlideUtils.INSTANCE.loadImage(getContext(), bean.addUserAvatar, cir_avatar, R.drawable.icon_teacher_default_head);

        ImageView iv_enter = holder.getView(R.id.iv_enter);
        if (TextUtils.equals(bean.chargeType, SheetMusicConstants.SHEET_MUSIC_CHARGE_TYPE_VIP)) {
            iv_enter.setImageResource(com.cooleshow.base.R.drawable.icon_music_vip);
        } else if (TextUtils.equals(bean.chargeType, SheetMusicConstants.SHEET_MUSIC_CHARGE_TYPE_CHARGE)) {
            iv_enter.setImageResource(com.cooleshow.base.R.drawable.icon_music_order);
        } else {
            iv_enter.setImageResource(com.cooleshow.base.R.drawable.icon_music_free);
        }

        //subject
        try {
            LinearLayout ll_subject_container = holder.getView(R.id.ll_subject_container);
            ll_subject_container.removeAllViews();
            String[] subjectNames = UiUtils.splitSubjectName(bean.subjectNames);
            for (int i = 0; i < subjectNames.length; i++) {
                createSubjectView(ll_subject_container, subjectNames[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @NonNull
    @Override
    public BaseLoadMoreModule addLoadMoreModule(@NonNull BaseQuickAdapter<?, ?> baseQuickAdapter) {
        return new BaseLoadMoreModule(baseQuickAdapter);
    }

    private void createSubjectView(ViewGroup container, String text) {
        TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(com.cooleshow.base.R.layout.view_subject_item_layout, container, false);
        textView.setText(text);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if (container.getChildCount() >= 1) {
            params.setMargins(SizeUtils.dp2px(4), 0, 0, 0);
        }
        container.addView(textView, params);
    }
}
