package com.cooleshow.teacher.ui.comment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

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.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.SparringCourseCommentBean;
import com.cooleshow.teacher.constants.CourseConstants;
import com.cooleshow.teacher.contract.CourseCommentDetailContract;
import com.cooleshow.teacher.databinding.ActivityCourseCommentDetailLayoutBinding;
import com.cooleshow.teacher.presenter.comment.CourseCommentDetailPresenter;
import com.cooleshow.teacher.widgets.CourseSetCommentDialog;

import androidx.annotation.Nullable;

/**
 * Author by pq, Date on 2022/5/5.
 * 老师端-陪练课详情
 */
@Route(path = RouterPath.CommentCenter.TEACHER_COURSE_COMMENT_DETAIL)
public class CourseCommentDetailActivity extends BaseMVPActivity<ActivityCourseCommentDetailLayoutBinding, CourseCommentDetailPresenter> implements CourseCommentDetailContract.CourseCommentDetailView, 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;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

    @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;
        }
    }

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

    private void initListener() {
        viewBinding.tvSubmit.setOnClickListener(this);
        viewBinding.tvSetTeacherComment.setOnClickListener(this);
    }

    private void refreshMainData() {
        presenter.queryCourseCommentDetail(mCourseId, mCourseGroupId, studentId);
    }


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

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

    private void showSetCommentDialog() {
        if (mSetCommentDialog == null) {
            mSetCommentDialog = new CourseSetCommentDialog(this);
            mSetCommentDialog.setOnSubmitClickListener((content, type) -> {
                //提交
                viewBinding.tvTeacherAppraisalEmptyText.setVisibility(View.GONE);
                viewBinding.tvTeacherAppraisalContent.setVisibility(View.VISIBLE);
                viewBinding.tvTeacherAppraisalContent.setText(content);
            });
        }
        if (!mSetCommentDialog.isShowing()) {
            mSetCommentDialog.show();
        }
        mSetCommentDialog.showMethod(CourseSetCommentDialog.TYPE_SET_TEACHER_COMMENT);
    }

    /**
     * 获取陪练课评价成功
     *
     * @param commentBean
     */
    @Override
    public void getSparringCourseCommentSuccess(SparringCourseCommentBean commentBean) {
        if (!checkActivityExist()) {
            return;
        }
        if (commentBean != null) {
            //头像
            GlideUtils.INSTANCE.loadImage(this, commentBean.avatar, viewBinding.ivAvatar, R.drawable.icon_teacher_default_head);
            //声部名称
            viewBinding.tvCourseName.setText(commentBean.subjectName);
            //学生名称
            viewBinding.tvTitle.setText(commentBean.userName);
            //开始时间
            viewBinding.tvTime.setText(UiUtils.getCourseTimeString(commentBean.startTime,commentBean.endTime));

            if (TextUtils.equals(CourseConstants.COURSE_STATUS_NOT_START, commentBean.status)) {
                //未开始
                viewBinding.tvCourseStatus.setText("未开始");
                viewBinding.tvCourseStatus.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_ff802c));
            }

            if (TextUtils.equals(CourseConstants.COURSE_STATUS_ING, commentBean.status)) {
                //进行中
                viewBinding.tvCourseStatus.setText("进行中");
                viewBinding.tvCourseStatus.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
            }

            if (TextUtils.equals(CourseConstants.COURSE_STATUS_COMPLETE, commentBean.status)) {
                //已结束
                viewBinding.tvCourseStatus.setText("已结束");
                viewBinding.tvCourseStatus.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_999999));
            }

            if (!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);
                viewBinding.tvSubmit.setVisibility(View.GONE);
            } else {
                //老师未评价
                viewBinding.tvSetTeacherComment.setVisibility(View.VISIBLE);
                viewBinding.tvTeacherAppraisalEmptyText.setVisibility(View.VISIBLE);
                viewBinding.tvTeacherAppraisalContent.setVisibility(View.GONE);
                viewBinding.tvSubmit.setVisibility(View.VISIBLE);
            }
        }
    }

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

    /**
     * 提交老师评价成功
     */
    @Override
    public void submitSparringCourseTeacherCommentSuccess() {
        if (!checkActivityExist()) {
            return;
        }
        ToastUtil.getInstance().showShort(getString(R.string.submit_comment_success));
        refreshMainData();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_set_teacher_comment) {
            //显示提交老师评价回复他创
            showSetCommentDialog();
        }

        if (v.getId() == R.id.tv_submit) {
            //提交老师点评
            String content = viewBinding.tvTeacherAppraisalContent.getText().toString().trim();
            if (TextUtils.isEmpty(content)) {
                return;
            }
            presenter.submitSparringCourseComment(content, mCourseId, mCourseGroupId, studentId);
        }
    }
}
