package com.wisewoods.eduandroid.ui.adapter;

import android.graphics.Color;
import android.text.TextUtils;

import com.wisewoods.base.app.UserManager;
import com.wisewoods.base.base.BaseVH;
import com.wisewoods.base.bean.Role;
import com.wisewoods.base.bean.StudentList;
import com.wisewoods.base.page.BasePageAdapter;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.app.ImgManager;

import org.jetbrains.annotations.NotNull;

public class StudentAdapter extends BasePageAdapter<StudentList.DataBean> {

    private boolean isReport;

    public StudentAdapter() {
        this(false);
    }

    public StudentAdapter(boolean isReport) {
        super(R.layout.item_recycler_student);
        this.isReport = isReport;
        addChildClickViewIds(R.id.ll_sleep, R.id.tv_leave, R.id.tv_phone);
    }

    @Override
    protected void convert(@NotNull BaseVH baseVH, StudentList.DataBean dataBean) {
        String tg = "";
        if (TextUtils.isEmpty(dataBean.teamGroupName)) {
            tg = (dataBean.teamName != null ? dataBean.teamName : "") +
                    (dataBean.groupName != null ? dataBean.groupName : "");
        } else {
            tg = dataBean.teamGroupName;
        }

        String info = ImgManager.getSex(dataBean.sex) + "  " + (dataBean.nationName != null ? dataBean.nationName : "未知")
                + "  " + tg;

        baseVH.setText(R.id.tv_name, dataBean.name)
                .setText(R.id.tv_class_job, dataBean.teamJobName)
                .setGone(R.id.tv_class_job, TextUtils.isEmpty(dataBean.teamJobName))
                .setText(R.id.tv_phone, dataBean.phone)
                .setText(R.id.tv_check, dataBean.groupType == 1 ? "无" : dataBean.stuSignMsg)
                .setGone(R.id.tv_phone, TextUtils.isEmpty(dataBean.phone))
                .setText(R.id.tv_info, info)
                .setText(R.id.tv_job, TextUtils.isEmpty(dataBean.jobName) ? "无" : dataBean.jobName)
                .setText(R.id.tv_pb_student, dataBean.getStuP())
                .setText(R.id.tv_join, dataBean.getStuFraction())
                .setTextColor(R.id.tv_join, dataBean.getStuFraction().equals("未参加") ?
                        Color.parseColor("#FF4632") : Color.parseColor("#347FFF"))
                .setVisible(R.id.tv_score, dataBean.getStuFraction().equals("已参加"))
                .setText(R.id.tv_score, dataBean.stuFraction + "分");
        baseVH.setText(R.id.tv_register, dataBean.isReport == 1 ? "已报到" : "未报到")
                .setText(R.id.tv_register_time, dataBean.isReport == 1 ? dataBean.reportTime : "")
                .setTextColor(R.id.tv_register, dataBean.isReport == 1 ?
                        Color.parseColor("#347FFF") : Color.parseColor("#FF4632"))
                .setTextColor(R.id.tv_check, "未签到".equals(dataBean.stuSignMsg) ?
                        Color.parseColor("#FF4632") : Color.parseColor("#347FFF"))
                .setProgress(R.id.pb_student, (int) (dataBean.studyProcess * 100))
        ;

        initUI(baseVH);
    }

    private void initUI(BaseVH baseVH) {
        Role role = UserManager.getUser().getRole();
        switch (role) {
            case ORGANIZER:
            case UNDERTAKER:
                baseVH.setGone(R.id.ll_student_logic, true)
                        .setGone(R.id.ll_check_data, false)
                        .setGone(R.id.tv_leave, true)
                        .setGone(R.id.ll_report, true)
                ;
                break;
            case EDUCATION:
                baseVH.setGone(R.id.ll_student_logic, true)
                        .setGone(R.id.ll_check_data, true)
                        .setGone(R.id.tv_leave, true)
                        .setGone(R.id.ll_report, true)
                ;
                break;
            case TRAIN:
                if (isReport) {
                    baseVH.setGone(R.id.ll_student_logic, true)
                            .setGone(R.id.ll_check_data, true)
                            .setGone(R.id.tv_leave, true)
                            .setGone(R.id.ll_report, false)
                    ;
                } else {
                    baseVH.setGone(R.id.ll_student_logic, false)
                            .setGone(R.id.ll_check_data, true)
                            .setGone(R.id.tv_leave, false)
                            .setGone(R.id.ll_report, true);
                }
                break;
            case TEACHER:
                baseVH.setGone(R.id.ll_student_logic, true)
                        .setGone(R.id.ll_check_data, true)
                        .setGone(R.id.tv_leave, true)
                        .setGone(R.id.ll_report, true);
                break;
        }
    }
}
