package com.cooleshow.teacher.adapter;

import android.text.TextUtils;
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.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.TimeUtils;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.HomeworkListBean;
import com.cooleshow.teacher.bean.MusicSheetListBean;
import com.cooleshow.teacher.constants.CourseConstants;

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

/**
 * 创建日期：2022/5/23 11:13
 *
 * @author Ryan
 * 类说明：
 */
public class HomeworkAdapter extends BaseQuickAdapter<HomeworkListBean.RowsBean, BaseViewHolder> implements LoadMoreModule {

    private String mDecorate;

    public HomeworkAdapter() {
        super(R.layout.layout_item_homework);
        addChildClickViewIds(R.id.iv_go_chat);
    }


    @Override
    protected void convert(BaseViewHolder helper, HomeworkListBean.RowsBean item) {
        if (item == null) {
            return;
        }
        TextView tv_state = helper.getView(R.id.tv_state);
        ImageView im_header = helper.getView(R.id.im_header);
        TextView tv_name = helper.getView(R.id.tv_name);
        RecyclerView rv_mark = helper.getView(R.id.rv_mark);
        if (TextUtils.equals(item.type, CourseConstants.PIANO_ROOM_COURSE)) {
            im_header.setImageResource(R.drawable.icon_piano_room_course);
            tv_name.setText(item.courseGroupName);
        } else {
            GlideUtils.INSTANCE.loadImage(getContext(), item.studentAvatar, im_header, R.drawable.icon_teacher_default_head);
            tv_name.setText(item.studentName);
        }

        if (TextUtils.equals(mDecorate, "YES")) {
            tv_state.setVisibility(View.VISIBLE);
            if (TextUtils.equals(item.homeworkStatus, "NOTCOMMIT")) {
                tv_state.setText("未提交");
                tv_state.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_ff802c));
            } else if (TextUtils.equals(item.homeworkStatus, "NOTREVIEW")) {
                tv_state.setText("未评价");
                tv_state.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
            } else if (TextUtils.equals(item.homeworkStatus, "REVIEWED")) {
                tv_state.setText("已点评");
                tv_state.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_999999));
            }
        } else {
            tv_state.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(item.subjectName)) {
            rv_mark.setVisibility(View.VISIBLE);
            List<String> markList = new ArrayList<>();
            if (item.subjectName.contains(",")) {
                String[] split = item.subjectName.split(",");
                for (String s : split) {
                    markList.add(s);
                }
            } else {
                markList.add(item.subjectName);
            }
            LinearLayoutManager manager = new LinearLayoutManager(getContext());
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rv_mark.setLayoutManager(manager);
            ItemMarkAdapter itemMarkAdapter = new ItemMarkAdapter(markList);
            rv_mark.setAdapter(itemMarkAdapter);
        } else {
            rv_mark.setVisibility(View.GONE);
        }

        helper.setText(R.id.tv_date, UiUtils.getCourseTimeString(item.startTime, item.endTime));

    }

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

    public void setDecorate(String decorate) {
        mDecorate = decorate;
    }
}
