package com.cooleshow.teacher.ui.course;

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

import com.alibaba.android.arouter.facade.annotation.Route;
import com.cooleshow.base.presenter.BasePresenter;
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.teacher.R;
import com.cooleshow.teacher.bean.SparringCourseCommentBean;
import com.cooleshow.teacher.bean.SparringCourseHomeworkBean;
import com.cooleshow.teacher.constants.CourseConstants;
import com.cooleshow.teacher.contract.SparringCourseContract;
import com.cooleshow.teacher.contract.SparringCourseDetailContract;
import com.cooleshow.teacher.databinding.ActivitySparringCourseDetailLayoutBinding;
import com.cooleshow.teacher.presenter.course.SparringCourseDetailPresenter;
import com.cooleshow.teacher.widgets.CourseSetCommentDialog;
import com.cooleshow.teacher.widgets.HomeworkVideoThumbnailView;
import com.cooleshow.usercenter.helper.UserHelper;

import androidx.annotation.NonNull;
import androidx.viewbinding.ViewBinding;

/**
 * Author by pq, Date on 2022/4/27.
 */
@Route(path = RouterPath.CourseCenter.SPARRING_COURSE_DETAIL)
public class SparringCourseDetailActivity extends BaseMVPActivity<ActivitySparringCourseDetailLayoutBinding, SparringCourseDetailPresenter> implements SparringCourseDetailContract.SparringCourseDetailView, View.OnClickListener {
    public static final String COURSE_ID = "course_id";
    public static final String COURSE_GROUP_ID = "course_group_id";
    public static final String STUDENT_ID = "studentId";
    private String mCourseId;
    private String mCourseGroupId;
    private String studentId;
    private CourseSetCommentDialog mSetCommentDialog;

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

    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "课程详情");
        mCourseId = getIntent().getStringExtra(COURSE_ID);
        mCourseGroupId = getIntent().getStringExtra(COURSE_GROUP_ID);
        studentId = getIntent().getStringExtra(STUDENT_ID);
        if (TextUtils.isEmpty(mCourseId)
                || TextUtils.isEmpty(mCourseGroupId)
                || TextUtils.isEmpty(studentId)) {
            finish();
            return;
        }
        viewBinding.tvSetHomework.setOnClickListener(this);
        viewBinding.tvHomeworkSetComment.setOnClickListener(this);
        viewBinding.tvSetTeacherComment.setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        refresh();
    }

    private void refresh() {
        if (presenter != null) {
            presenter.getSparringCourseHomework(mCourseId);
            presenter.getSparringCourseComment(mCourseId, mCourseGroupId, studentId);
        }
    }

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

    /**
     * 获取课程详情成功
     *
     * @param infoBean
     */
    @Override
    public void getSparringCourseHomeworkSuccess(SparringCourseHomeworkBean infoBean) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (infoBean == null) {
            return;
        }
        //时间
        viewBinding.tvCourseTime.setText(infoBean.classDate);
        //学生头像
        GlideUtils.INSTANCE.loadImage(this, infoBean.studentAvatar, viewBinding.ivAvatar, R.drawable.icon_teacher_default_head);
        //title
        viewBinding.tvTitle.setText(UserHelper.getStudentName(infoBean.studentName, studentId));
        //subjectName
        viewBinding.tvCourseName.setText(infoBean.subjectName);

        String status = infoBean.courseStatus;
        if (TextUtils.equals(CourseConstants.COURSE_STATUS_NOT_START, status)) {
            //课程未开始
            viewBinding.tvCourseStatus.setText(getString(R.string.course_not_start_str));
            viewBinding.tvCourseStatus.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_ff802c));
        }
        if (TextUtils.equals(CourseConstants.COURSE_STATUS_ING, status)) {
            //课程进行中
            viewBinding.tvCourseStatus.setText(R.string.course_ing_str);
            viewBinding.tvCourseStatus.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
        }
        if (TextUtils.equals(CourseConstants.COURSE_STATUS_COMPLETE, status)) {
            //课程已结束
            viewBinding.tvCourseStatus.setText(R.string.course_is_completed);
            viewBinding.tvCourseStatus.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_999999));
        }
        //课后作业
        handleHomeworkData(infoBean);
        //作业视频
        handleHomeworkStudentSubmit(infoBean);
        //作业点评
        handleHomeworkComment(infoBean);
    }

    /**
     * 作业点评
     */
    private void handleHomeworkComment(SparringCourseHomeworkBean bean) {
        //tv_homework_comment_content
        if (bean == null) {
            return;
        }
        if (!TextUtils.isEmpty(bean.teacherReplied)) {
            //老师已点评作业
            viewBinding.tvHomeworkCommentEmptyText.setVisibility(View.GONE);
            viewBinding.tvHomeworkSetComment.setVisibility(View.GONE);
            viewBinding.tvHomeworkCommentContent.setVisibility(View.VISIBLE);
            viewBinding.tvHomeworkCommentContent.setText(bean.teacherReplied);
        } else {
            viewBinding.tvHomeworkCommentEmptyText.setVisibility(View.VISIBLE);
            viewBinding.tvHomeworkSetComment.setVisibility(View.VISIBLE);
            viewBinding.tvHomeworkCommentContent.setVisibility(View.GONE);
        }
    }

    /**
     * 学生提交的作业
     *
     * @param bean
     */
    private void handleHomeworkStudentSubmit(SparringCourseHomeworkBean bean) {
        if (bean == null) {
            return;
        }
        //tv_homework_video_empty_text
        if (bean.submitHomework == CourseConstants.COURSE_HOMEWORK_STU_SUBMIT) {
            //学员已提交课后作业
            viewBinding.tvHomeworkVideoEmptyText.setVisibility(View.GONE);
            viewBinding.llHomeworkVideoContent.removeAllViews();
            addVideoThumbnailView(bean.studentAttachments);
        } else {
            //学员未提交课后作业
            viewBinding.tvHomeworkVideoEmptyText.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 添加视频缩略图
     *
     * @param videoUrls
     */
    private void addVideoThumbnailView(String videoUrls) {
        try {
            if (!TextUtils.isEmpty(videoUrls)) {
                String[] results = TextUtils.split(videoUrls, ",");
                for (int i = 0; i < results.length; i++) {
                    HomeworkVideoThumbnailView videoThumbnailView = new HomeworkVideoThumbnailView(SparringCourseDetailActivity.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();
        }
    }

    /**
     * 课后作业
     */
    private void handleHomeworkData(SparringCourseHomeworkBean bean) {
        if (bean == null) {
            return;
        }
        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);
        }
    }

    private void handleTeacherComment() {

    }


    @Override
    public void getSparringCourseCommentSuccess(SparringCourseCommentBean commentBean) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (commentBean != null && !TextUtils.isEmpty(commentBean.teacherReplied)) {
            //老师已评价 tv_set_teacher_comment
            viewBinding.tvSetTeacherComment.setVisibility(View.GONE);
            viewBinding.tvTeacherAppraisalEmptyText.setVisibility(View.GONE);
            viewBinding.tvTeacherAppraisalContent.setVisibility(View.VISIBLE);
            viewBinding.tvTeacherAppraisalContent.setText(commentBean.teacherReplied);
        } else {
            //老师未评价
            viewBinding.tvSetTeacherComment.setVisibility(View.VISIBLE);
            viewBinding.tvTeacherAppraisalEmptyText.setVisibility(View.VISIBLE);
            viewBinding.tvTeacherAppraisalContent.setVisibility(View.GONE);
        }

        if (commentBean != null && !TextUtils.isEmpty(commentBean.studentReplied)) {
            //学生已评价 tv_stu_comment_content
            //评分星级
            viewBinding.ratingBarForStudent.setVisibility(View.VISIBLE);
            viewBinding.ratingBarForStudent.setRating(commentBean.score);

            viewBinding.tvStuCommentEmptyText.setVisibility(View.GONE);
            viewBinding.tvStuCommentContent.setVisibility(View.VISIBLE);
            viewBinding.tvStuCommentContent.setText(commentBean.studentReplied);
        } else {
            //学员未评价
            viewBinding.ratingBarForStudent.setVisibility(View.GONE);
            viewBinding.tvStuCommentEmptyText.setVisibility(View.VISIBLE);
            viewBinding.tvStuCommentContent.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_set_homework) {
            //布置作业
            showSetCommentDialog(CourseSetCommentDialog.TYPE_SET_HOMEWORK);
            return;
        }
        if (v.getId() == R.id.tv_homework_set_comment) {
            //点评作业
            showSetCommentDialog(CourseSetCommentDialog.TYPE_SET_HOMEWORK_COMMENT);
            return;
        }

        if (v.getId() == R.id.tv_set_teacher_comment) {
            //提交老师评价
            showSetCommentDialog(CourseSetCommentDialog.TYPE_SET_TEACHER_COMMENT);
            return;
        }
    }

    private void showSetCommentDialog(int typeSetTeacherComment) {
        if (mSetCommentDialog == null) {
            mSetCommentDialog = new CourseSetCommentDialog(this);
            mSetCommentDialog.setOnSubmitClickListener(new CourseSetCommentDialog.OnSubmitClickListener() {
                @Override
                public void onSubmit(String content,int type) {
                    //提交
                    if (type == CourseSetCommentDialog.TYPE_SET_TEACHER_COMMENT) {
                        //提交老师点评
                        presenter.submitSparringCourseComment(content,mCourseId,mCourseGroupId,studentId);
                    }
                    if (type == CourseSetCommentDialog.TYPE_SET_HOMEWORK) {
                        //提交布置作业
                        presenter.submitSparringCourseHomework(content,mCourseId);
                    }
                    if (type == CourseSetCommentDialog.TYPE_SET_HOMEWORK_COMMENT) {
                        //提交作业点评
                        presenter.submitSparringCourseHomeworkComment(content,mCourseId);
                    }
                }
            });
        }
        if (!mSetCommentDialog.isShowing()) {
            mSetCommentDialog.show();
        }
        mSetCommentDialog.showMethod(typeSetTeacherComment);
    }

    @Override
    public void getSparringCourseCommentError() {
        //获取课程详情评价信息失败
    }

    /**
     * 提交老师评价成功
     */
    @Override
    public void submitSparringCourseTeacherCommentSuccess() {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        refresh();
    }

    /**
     * 提交布置作业成功
     */
    @Override
    public void submitSparringCourseHomeworkSuccess() {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        refresh();
    }

    /**
     * 提交作业评价成功
     */
    @Override
    public void submitSparringCourseHomeworkCommentSuccess() {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        refresh();
    }


    @Override
    public void getSparringCourseHomeworkError() {
        //获取课程详情作业信息失败
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSetCommentDialog != null) {
            mSetCommentDialog.unbind();
        }
    }
}
