package com.cooleshow.teacher.ui.work;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.GlideUtils;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.base.utils.TimeUtils;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.base.utils.VibrateUtils;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.base.widgets.VideoThumbnailView;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.SparringCourseHomeworkBean;
import com.cooleshow.teacher.constants.AssignHomeworkContract;
import com.cooleshow.teacher.constants.CourseConstants;
import com.cooleshow.teacher.constants.TeacherConfig;
import com.cooleshow.teacher.databinding.ActivityAssignHomeworkBinding;
import com.cooleshow.teacher.presenter.homework.AssignHomeworkPresenter;
import com.cooleshow.teacher.ui.course.SparringCourseDetailActivity;
import com.cooleshow.teacher.widgets.CourseSetCommentDialog;
import com.cooleshow.usercenter.helper.UserHelper;

/**
 * 创建日期：2022/5/31 15:26
 *
 * @author Ryan
 * 类说明：老师端陪练课布置作业
 */
@Route(path = RouterPath.WorkCenter.TEACHER_WORK_ASSIGN_HOMEWORK)
public class AssignHomeworkActivity extends BaseMVPActivity<ActivityAssignHomeworkBinding, AssignHomeworkPresenter> implements AssignHomeworkContract.AssignHomeworkView {
    private String mCourseId;
    private String studentId;
    private CourseSetCommentDialog mSetCommentDialog;

    @Override
    protected void initView() {
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        viewBinding.titleView.setLeftClickListener(v -> {
            finish();
        });
        mCourseId = getIntent().getStringExtra(TeacherConfig.COURSE_ID);
        studentId = getIntent().getStringExtra(TeacherConfig.STUDENT_ID);

        viewBinding.tvSetHomework.setOnClickListener(v -> {
            //布置作业
            showSetCommentDialog(CourseSetCommentDialog.TYPE_SET_HOMEWORK);
        });
        viewBinding.tvHomeworkSetComment.setOnClickListener(v -> {
            //点评作业
            showSetCommentDialog(CourseSetCommentDialog.TYPE_SET_HOMEWORK_COMMENT);
        });
    }

    private void showSetCommentDialog(int typeSetHomework) {
        if (mSetCommentDialog == null) {
            mSetCommentDialog = new CourseSetCommentDialog(this);
            mSetCommentDialog.setOnSubmitClickListener((content, type) -> {
                if (UiUtils.isFastClick()) {
                    return;
                }
                //提交
                if (type == CourseSetCommentDialog.TYPE_SET_HOMEWORK) {
                    //提交布置作业
                    presenter.submitSparringCourseHomework(content, mCourseId);
                }
                if (type == CourseSetCommentDialog.TYPE_SET_HOMEWORK_COMMENT) {
                    //提交作业点评
                    presenter.submitSparringCourseHomeworkComment(content, mCourseId, studentId);
                }
            });
        }
        if (!mSetCommentDialog.isShowing()) {
            mSetCommentDialog.show();
        }
        mSetCommentDialog.showMethod(typeSetHomework);
    }

    @Override
    public void initData() {
        super.initData();
        presenter.getCourseDetail(mCourseId, studentId);
    }

    @Override
    protected ActivityAssignHomeworkBinding getLayoutView() {
        return ActivityAssignHomeworkBinding.inflate(getLayoutInflater());
    }

    @Override
    protected AssignHomeworkPresenter createPresenter() {
        return new AssignHomeworkPresenter();
    }

    @Override
    public void getSparringCourseHomework(SparringCourseHomeworkBean bean) {
        if (bean == null) {
            return;
        }
        //subjectName
        if (TextUtils.equals(bean.courseType, CourseConstants.OTHER_COURSE)) {
            //陪练课
            GlideUtils.INSTANCE.loadImage(this, bean.studentAvatar, viewBinding.imHeader, R.drawable.icon_teacher_default_head);
            viewBinding.tvName.setText(bean.studentName);
        } else {
            //琴房课
            viewBinding.imHeader.setImageResource(R.drawable.icon_piano_room_course);
            viewBinding.tvName.setText(bean.courseGroupName);
        }
        viewBinding.tvCourseName.setText(bean.subjectName);

        viewBinding.tvDate.setText(UiUtils.getCourseTimeString(bean.startTime, bean.endTime));

        String status = bean.courseStatus;
        if (TextUtils.equals(CourseConstants.COURSE_STATUS_NOT_START, status)) {
            //课程未开始
            viewBinding.tvState.setText(getString(R.string.course_not_start_str));
            viewBinding.tvState.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_ff802c));
        }
        if (TextUtils.equals(CourseConstants.COURSE_STATUS_ING, status)) {
            //课程进行中
            viewBinding.tvState.setText(R.string.course_ing_str);
            viewBinding.tvState.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
        }
        if (TextUtils.equals(CourseConstants.COURSE_STATUS_COMPLETE, status)) {
            //课程已结束
            viewBinding.tvState.setText(R.string.course_is_completed);
            viewBinding.tvState.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_999999));
        }

        if (bean.decorateHomework == CourseConstants.COURSE_HOMEWORK_SET) {
            //已布置课后作业
            viewBinding.tvHomeworkEmptyText.setVisibility(View.GONE);
            viewBinding.tvHomeworkContent.setVisibility(View.VISIBLE);
            viewBinding.tvHomeworkContent.setText(bean.content);
            viewBinding.tvSetHomework.setVisibility(View.GONE);
        } else {
            //未布置课后作业
            viewBinding.tvHomeworkEmptyText.setVisibility(View.VISIBLE);
            viewBinding.tvHomeworkContent.setVisibility(View.GONE);
            viewBinding.tvSetHomework.setVisibility(View.VISIBLE);
        }
        //tv_homework_video_empty_text
        if (bean.submitHomework == CourseConstants.COURSE_HOMEWORK_STU_SUBMIT && bean.homeworkExpire == CourseConstants.COURSE_HOMEWORK_NOT_OVERDUE) {
            //学员已提交课后作业
            viewBinding.tvHomeworkVideoEmptyText.setVisibility(View.GONE);
            viewBinding.llHomeworkVideoContent.removeAllViews();
            viewBinding.homeWordVideoView.setVisibility(View.VISIBLE);
            addVideoThumbnailView(bean.studentAttachments);
        } else {
            //学员未提交课后作业 || 作业过期
            if (bean.decorateHomework == CourseConstants.COURSE_HOMEWORK_SET) {
                viewBinding.homeWordVideoView.setVisibility(View.VISIBLE);
            } else {
                viewBinding.homeWordVideoView.setVisibility(View.GONE);
            }
            viewBinding.tvHomeworkVideoEmptyText.setVisibility(View.VISIBLE);
            if (bean.homeworkExpire == CourseConstants.COURSE_HOMEWORK_OVERDUE) {
                viewBinding.tvHomeworkVideoEmptyText.setText("作业已过期无法查看！");
            } else {
                viewBinding.tvHomeworkVideoEmptyText.setText("学员尚未上传作业视频");
            }
        }
        //作业点评
        if (!TextUtils.isEmpty(bean.teacherReplied)) {
            //老师已点评作业
            viewBinding.tvHomeworkCommentEmptyText.setVisibility(View.GONE);
            viewBinding.tvHomeworkSetComment.setVisibility(View.GONE);
            viewBinding.commentView.setVisibility(View.VISIBLE);
            viewBinding.tvHomeworkCommentContent.setVisibility(View.VISIBLE);
            viewBinding.tvHomeworkCommentContent.setText(bean.teacherReplied);
        } else {
            if (bean.submitHomework == CourseConstants.COURSE_HOMEWORK_STU_SUBMIT) {
                viewBinding.commentView.setVisibility(View.VISIBLE);
            } else {
                viewBinding.commentView.setVisibility(View.GONE);
            }
            viewBinding.tvHomeworkCommentEmptyText.setVisibility(View.VISIBLE);
            viewBinding.tvHomeworkSetComment.setVisibility(View.VISIBLE);
            viewBinding.tvHomeworkCommentContent.setVisibility(View.GONE);
        }
    }

    private void addVideoThumbnailView(String videoUrls) {
        try {
            if (!TextUtils.isEmpty(videoUrls)) {
                String[] results = TextUtils.split(videoUrls, ",");
                for (int i = 0; i < results.length; i++) {
                    VideoThumbnailView videoThumbnailView = new VideoThumbnailView(this);
                    videoThumbnailView.setData(results[i]);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    int left = i == 0 ? 0 : SizeUtils.dp2px(6);
                    layoutParams.setMargins(left, 0, 0, 0);
                    layoutParams.weight = 1;
                    viewBinding.llHomeworkVideoContent.addView(videoThumbnailView, layoutParams);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            viewBinding.llHomeworkVideoContent.removeAllViews();
        }
    }

    @Override
    public void submitSparringCourseHomeworkSuccess() {
        presenter.getCourseDetail(mCourseId, studentId);
    }

    @Override
    public void submitSparringCourseHomeworkCommentSuccess() {
        presenter.getCourseDetail(mCourseId, studentId);
    }
}
