package com.cooleshow.teacher.adapter;

import android.text.TextUtils;
import android.util.Log;
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 androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
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.utils.GlideUtils;
import com.cooleshow.base.utils.MusicSheetUtils;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.MineLiveCourseListBean;
import com.cooleshow.teacher.bean.MusicSheetListBean;
import com.cooleshow.teacher.ui.score.MineScoreActivity;

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

/**
 * 创建日期：2022/5/20 17:19
 *
 * @author Ryan
 * 类说明：
 */
public class MineScoreAdapter extends BaseQuickAdapter<MusicSheetListBean.RowsBean, BaseViewHolder> implements LoadMoreModule {
    public String scoreStatus = "";

    public MineScoreAdapter(String auditStatus) {
        super(R.layout.layout_mine_score_item);
        this.scoreStatus = auditStatus;
        if (TextUtils.equals(scoreStatus, MineScoreActivity.TYPE_PASS)) {
            addChildClickViewIds(R.id.tv_status_tip);
        }
    }


    @Override
    protected void convert(BaseViewHolder helper, MusicSheetListBean.RowsBean item) {
        ImageView im_music_mark = helper.getView(R.id.im_music_mark);
        LinearLayout ll_tag_container = helper.getView(R.id.ll_tag_container);
        TextView tv_name = helper.getView(R.id.tv_name);
        TextView tv_author_name = helper.getView(R.id.tv_author);
        TextView tv_upload_author = helper.getView(R.id.tv_upload_author);

        LinearLayout ll_music_subject = helper.getView(R.id.ll_music_subject);

        if (TextUtils.equals(scoreStatus, MineScoreActivity.TYPE_BUY)) {
            //作者
            tv_author_name.setText(String.format("-%s", item.composer));
            //上传人
            tv_upload_author.setText(String.format("上传者：%s", item.addName));
        } else {
            //作者
            tv_author_name.setText("");
            //上传人
            tv_upload_author.setText(String.format("作曲：%s", item.composer));
        }
        TextView tv_status_tip = helper.getView(R.id.tv_status_tip);
        View iv_play = helper.getView(R.id.iv_play);
        if (TextUtils.equals(scoreStatus, MineScoreActivity.TYPE_PASS)) {
            tv_status_tip.setText("下架");
            tv_status_tip.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_008ae0));
            tv_status_tip.setVisibility(View.VISIBLE);
            iv_play.setVisibility(View.GONE);
            tv_status_tip.setCompoundDrawablesWithIntrinsicBounds(null, null, getContext().getResources().getDrawable(com.cooleshow.base.R.drawable.icon_arrow_right_small_blue), null);
        } else if (TextUtils.equals(scoreStatus, MineScoreActivity.TYPE_DOING)) {
            tv_status_tip.setText("审核中");
            tv_status_tip.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
            tv_status_tip.setVisibility(View.VISIBLE);
            iv_play.setVisibility(View.GONE);
            tv_status_tip.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        } else if (TextUtils.equals(scoreStatus, MineScoreActivity.TYPE_UNPASS)) {
            tv_status_tip.setText("编辑");
            tv_status_tip.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_ff4e19));
            tv_status_tip.setVisibility(View.VISIBLE);
            iv_play.setVisibility(View.GONE);
            tv_status_tip.setCompoundDrawablesWithIntrinsicBounds(null, null, getContext().getResources().getDrawable(com.cooleshow.base.R.drawable.icon_arrow_right_small_orange), null);
        } else if (TextUtils.equals(scoreStatus, MineScoreActivity.TYPE_OUT_SALE)) {
            tv_status_tip.setText("编辑");
            tv_status_tip.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_ff4e19));
            tv_status_tip.setVisibility(View.VISIBLE);
            iv_play.setVisibility(View.GONE);
            tv_status_tip.setCompoundDrawablesWithIntrinsicBounds(null, null, getContext().getResources().getDrawable(com.cooleshow.base.R.drawable.icon_arrow_right_small_orange), null);
        } else {
            iv_play.setVisibility(View.VISIBLE);
            tv_status_tip.setVisibility(View.GONE);
        }

        //icon
        if (!TextUtils.isEmpty(item.titleImg)) {
            GlideUtils.INSTANCE.loadImage(getContext(), item.titleImg, im_music_mark, R.drawable.icon_music_mark);
        } else {
            im_music_mark.setImageResource(R.drawable.icon_music_mark);
        }

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

        tv_name.setText(item.musicSheetName);

        //subject
        ll_music_subject.removeAllViews();
        if (!TextUtils.isEmpty(item.subjectNames)) {
            ll_music_subject.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(item.subjectNames)) {
                String[] split = item.subjectNames.split(",");
                if (split != null && split.length > 0) {
                    for (int i = 0; i < split.length; i++) {
                        createSubjectView(ll_music_subject, split[i]);
                    }
                }
            }
        } else {
            ll_music_subject.setVisibility(View.GONE);
        }
        View view_line = helper.getView(R.id.view_line);
        int position = helper.getLayoutPosition();
        int size = getData().size();
        view_line.setVisibility(position == size - 1 ? View.INVISIBLE : View.VISIBLE);
        ConstraintLayout cs_root = helper.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 = helper.getView(R.id.iv_good_tag);
        if (TextUtils.equals(item.exquisiteFlag, "1")) {
            iv_good_tag.setVisibility(View.VISIBLE);
        } else {
            iv_good_tag.setVisibility(View.GONE);
        }
    }

    private void createSubjectView(ViewGroup parent, String text) {
        TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(com.cooleshow.base.R.layout.view_subject_item_layout, parent, 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 (parent.getChildCount() >= 1) {
            params.setMargins(SizeUtils.dp2px(4), 0, 0, 0);
        }
        parent.addView(textView, params);
    }

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

}
