package com.cooleshow.student.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.UiUtils;
import com.cooleshow.student.R;
import com.cooleshow.student.bean.HomeworkListBean;
import com.cooleshow.student.constants.CourseConstants;
import com.cooleshow.base.constanst.HomeWorkConstants;

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 {

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


    @Override
    protected void convert(BaseViewHolder helper, HomeworkListBean.RowsBean item) {
        TextView tv_date = helper.getView(R.id.tv_date);
        TextView tv_state = helper.getView(R.id.tv_state);
        TextView tv_join_state = helper.getView(R.id.tv_join_state);
        TextView tv_submit = helper.getView(R.id.tv_submit);

        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.OTHER_COURSE)) {
            //陪练课
            GlideUtils.INSTANCE.loadImage(getContext(), item.teacherAvatar, im_header, R.drawable.icon_teacher_default_head);
            tv_name.setText(item.teacherName);
        } else {
            //琴房课
            im_header.setImageResource(R.drawable.icon_piano_room_course);
            tv_name.setText(item.courseGroupName);
        }

        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);
        }
        tv_date.setText(UiUtils.getCourseTimeString(item.startTime, item.endTime));
        if (item.absenteeism == 0) {
            //到课
            tv_join_state.setText("到课");
            tv_join_state.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_999999));
        } else {
            //旷课
            tv_join_state.setText("旷课");
            tv_join_state.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_ff4e19));
        }

        if (TextUtils.equals(item.homeworkStatus, HomeWorkConstants.COURSE_HOMEWORK_UN_COMMIT)) {
            tv_submit.setText("去提交");
            tv_submit.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
            tv_submit.setBackgroundResource(R.drawable.bg_2dc7aa_border);
            tv_submit.setVisibility(View.VISIBLE);
        } else {
            tv_submit.setVisibility(View.GONE);
        }

        if (TextUtils.equals(item.homeworkStatus, HomeWorkConstants.COURSE_HOMEWORK_NOTREVIEW)) {
            tv_state.setVisibility(View.VISIBLE);
            tv_state.setText("未点评");
            tv_state.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
        } else if (TextUtils.equals(item.homeworkStatus, HomeWorkConstants.COURSE_HOMEWORK_REVIEWED)) {
            tv_state.setVisibility(View.VISIBLE);
            tv_state.setText("已点评");
            tv_state.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_999999));
        } else {
            tv_state.setVisibility(View.GONE);
        }
    }

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

}
