package com.cooleshow.teacher.adapter;

import android.media.Image;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
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.MusicSheetUtils;
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;
import androidx.constraintlayout.widget.ConstraintLayout;

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


    public HomePageSheetMusicAdapter() {
        super(R.layout.item_home_page_sheet_music_layout);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, HomePageSheetMusicListBean.RowsBean bean) {
        //曲谱名称
        holder.setText(R.id.tv_name, bean.musicSheetName);
        //音乐人
        holder.setText(R.id.tv_upload_author, String.format("作曲：%s", bean.composer));
        //创建人
        ImageView iv_img = holder.getView(R.id.im_music_mark);
        if (!TextUtils.isEmpty(bean.titleImg)) {
            GlideUtils.INSTANCE.loadImage(getContext(), bean.titleImg, iv_img, R.drawable.icon_music_mark);
        } else {
            iv_img.setImageResource(R.drawable.icon_music_mark);
        }

        LinearLayout ll_tag_container = holder.getView(R.id.ll_tag_container);
        ll_tag_container.removeAllViews();
        if (!TextUtils.isEmpty(bean.paymentType)) {
            String[] split = bean.paymentType.split(",");
            if (split != null && split.length > 0) {
                for (int i = 0; i < split.length; i++) {
                    MusicSheetUtils.createTag(getContext(), ll_tag_container, split[i]);
                }
            }
        }

        //subject
        try {
            LinearLayout ll_subject_container = holder.getView(R.id.ll_music_subject);
            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();
        }

        View view_line = holder.getView(R.id.view_line);
        int position = holder.getLayoutPosition();
        int size = getData().size();
        view_line.setVisibility(position == size - 1 ? View.INVISIBLE : View.VISIBLE);
        ConstraintLayout cs_root = holder.getView(R.id.cs_root);
        if (size == 1) {
            cs_root.setBackgroundResource(com.cooleshow.base.R.drawable.shape_10dp_white);
        } else {
            if (position == 0) {
                cs_root.setBackgroundResource(com.cooleshow.base.R.drawable.bg_white_top_10dp);
            } else if (position == size - 1) {
                cs_root.setBackgroundResource(com.cooleshow.base.R.drawable.bg_white_bottom_10dp);
            } else {
                cs_root.setBackgroundColor(getContext().getResources().getColor(R.color.white));
            }
        }

        //精品标记
        ImageView iv_good_tag = holder.getView(R.id.iv_good_tag);
        if (TextUtils.equals(bean.exquisiteFlag, "1")) {
            iv_good_tag.setVisibility(View.VISIBLE);
        } else {
            iv_good_tag.setVisibility(View.GONE);
        }

        //专辑曲目标记
        View iv_album_tag=holder.getView(R.id.iv_album_tag);
        MusicSheetUtils.addAlbumTag(bean.albumNums,iv_album_tag);
    }

    @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);
        textView.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
        textView.setBackgroundResource(com.cooleshow.base.R.drawable.shape_effbf9_9dp);
        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);
    }
}
