package com.daya.live_teaching.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.daya.live_teaching.R;
import com.daya.live_teaching.model.ClassMember;
import com.daya.live_teaching.ui.base.BaseRecyclerAdapter;
import com.daya.live_teaching.ui.base.BaseViewHolder;

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

/**
 * Description:
 * Copyright  : Copyright (c) 2019
 * Company    : 大雅乐盟
 * Author     : r
 * Date       : 2020/6/9 10:43
 */
public class AccompanimentListAdapter extends BaseRecyclerAdapter<AccompanimentListAdapter.ViewHolder> {

    private Context mContext;
    List<ClassMember.ScheduleStudentMusicScoresBean> menberList = new ArrayList<>();
    private String musicScoreAccompanimentId;

    public AccompanimentListAdapter(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflater = LayoutInflater.from(mContext).inflate(R.layout.layout_accompaniment_list_item, parent, false);
        return new ViewHolder(inflater);
    }

    @Override
    public int getItemCount() {
        return menberList.size();
    }

    public void setData(List<ClassMember.ScheduleStudentMusicScoresBean> classMembers, String musicScoreAccompanimentId) {
        this.musicScoreAccompanimentId = musicScoreAccompanimentId;
        this.menberList.clear();
        this.menberList.addAll(classMembers);
        notifyDataSetChanged();
    }

    public class ViewHolder extends BaseViewHolder {
        ImageView ivType;
        TextView tvName;
        TextView tvStatus;

        public ViewHolder(View itemView) {
            super(itemView);
            ivType = itemView.findViewById(R.id.iv_type);
            tvName = itemView.findViewById(R.id.tv_name);
            tvStatus = itemView.findViewById(R.id.tv_status);
        }

        @Override
        protected void initSubViewClick(int tagPosition) {
            super.initSubViewClick(tagPosition);
            ivType.setOnClickListener(this);
            ivType.setTag(tagPosition);
            tvStatus.setOnClickListener(this);
            tvStatus.setTag(tagPosition);
        }

        @Override
        protected void onBind(int position) {
            ClassMember.ScheduleStudentMusicScoresBean musicScoresBean = menberList.get(position);
            tvName.setText(musicScoresBean.getMusicScoreName());
            if (!TextUtils.isEmpty(musicScoresBean.getUrl()) && !TextUtils.isEmpty(musicScoresBean.getMp3Url())) {
                //原生或者伴奏
                if (musicScoresBean.getIsAccompaniment() == 0) {
                    ivType.setImageResource(R.drawable.ic_live_teaching_acoustic);
                } else {
                    ivType.setImageResource(R.drawable.ic_live_teaching_accompaniment);
                }
            } else if (!TextUtils.isEmpty(musicScoresBean.getUrl())) {
                ivType.setImageResource(R.drawable.ic_live_teaching_acoustic);
            } else if (!TextUtils.isEmpty(musicScoresBean.getMp3Url())) {
                ivType.setImageResource(R.drawable.ic_live_teaching_accompaniment);
            }


            if (!TextUtils.isEmpty(musicScoreAccompanimentId) && musicScoreAccompanimentId.equals(musicScoresBean.getMusicScoreAccompanimentId())) {
                tvName.setTextColor(mContext.getResources().getColor(R.color.red_eb6363));
            } else {
                tvName.setTextColor(mContext.getResources().getColor(com.cooleshow.base.R.color.white));
            }
            if (musicScoresBean.getDownStatus() == 1) {
                tvStatus.setText("已下载");
                tvStatus.setTextColor(mContext.getResources().getColor(com.cooleshow.base.R.color.color_666666));
            } else if (musicScoresBean.getDownStatus() == 2) {
                tvStatus.setText("下载失败");
                tvStatus.setTextColor(mContext.getResources().getColor(com.cooleshow.base.R.color.common_red));
            } else {
                tvStatus.setText("下载中");
                tvStatus.setTextColor(mContext.getResources().getColor(R.color.live_text_green));
            }
        }
    }


}
