package com.cooleshow.teacher.ui.course;

import android.graphics.Color;
import android.os.Bundle;
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.helper.QMUIStatusBarHelper;
import com.cooleshow.base.widgets.VideoThumbnailView;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.PianoRoomCourseHomeworkBean;
import com.cooleshow.teacher.constants.CourseConstants;
import com.cooleshow.teacher.contract.PianoRoomCourseHomeworkContract;
import com.cooleshow.teacher.databinding.ActivityPianoCourseHomeworkLayoutBinding;
import com.cooleshow.teacher.presenter.homework.PianoRoomCourseHomeWorkPresenter;
import com.cooleshow.teacher.widgets.CourseSetCommentDialog;
import com.cooleshow.usercenter.helper.UserHelper;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * Author by pq, Date on 2022/6/24.
 * 琴房课作业详情页面
 *
 * 暂时不需要@AssignHomeworkActivity
 */
@Route(path = RouterPath.CourseCenter.PIANO_ROOM_COURSE_HOMEWORK)
public class PianoRoomCourseHomeWorkActivity extends BaseMVPActivity<ActivityPianoCourseHomeworkLayoutBinding, PianoRoomCourseHomeWorkPresenter> implements PianoRoomCourseHomeworkContract.PianoRoomCourseHomeworkView, View.OnClickListener {
    public static final String COURSE_ID = "course_id";
    public static final String STUDENT_ID = "studentId";

    private String mCourseId;
    private String studentId;
    private CourseSetCommentDialog mSetCommentDialog;

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

    @Override
    protected void initView() {
        mCourseId = getIntent().getStringExtra(COURSE_ID);
        studentId = getIntent().getStringExtra(STUDENT_ID);
        if (TextUtils.isEmpty(mCourseId) || TextUtils.isEmpty(studentId)) {
            finish();
            return;
        }
        viewBinding.tvSetHomework.setOnClickListener(this);
        viewBinding.tvHomeworkSetComment.setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "作业详情");
        viewBinding.toolbarInclude.toolbar.setBackgroundColor(Color.WHITE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        initListener();
    }

    private void initListener() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

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

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

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

    @Override
    public void getCourseHomeworkInfoSuccess(PianoRoomCourseHomeworkBean infoBean) {
        if (!checkActivityExist()) {
            return;
        }
        if (infoBean == null) {
            return;
        }
        //学生头像
        viewBinding.ivAvatar.setImageResource(R.drawable.icon_piano_room_course);
        //title
        viewBinding.tvTitle.setText(UserHelper.getStudentName(infoBean.studentName, studentId));
        //subjectName
        viewBinding.tvCourseName.setText(infoBean.subjectName);
        //课后作业
        handleHomeworkData(infoBean);
        //作业视频
        handleHomeworkStudentSubmit(infoBean);
        //作业点评
        handleHomeworkComment(infoBean);
    }

    /**
     * 课后作业
     */
    private void handleHomeworkData(PianoRoomCourseHomeworkBean 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);
        }
    }

    /**
     * 学生提交的作业
     *
     * @param bean
     */
    private void handleHomeworkStudentSubmit(PianoRoomCourseHomeworkBean 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++) {
                    VideoThumbnailView videoThumbnailView = new VideoThumbnailView(PianoRoomCourseHomeWorkActivity.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 handleHomeworkComment(PianoRoomCourseHomeworkBean 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);
        }
    }

    /**
     * 提交布置作业成功
     */
    @Override
    public void submitCourseHomeworkSuccess() {
        if (!checkActivityExist()) {
            return;
        }
        refresh();
    }

    @Override
    public void submitCourseHomeworkCommentSuccess() {
        if (!checkActivityExist()) {
            return;
        }
        refresh();
    }


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

    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_HOMEWORK) {
                        //提交布置作业
                        presenter.submitCourseHomework(content, mCourseId);
                    }
                    if (type == CourseSetCommentDialog.TYPE_SET_HOMEWORK_COMMENT) {
                        //提交作业点评
                        presenter.submitCourseHomeworkComment(content, mCourseId, studentId);
                    }
                }
            });
        }
        if (!mSetCommentDialog.isShowing()) {
            mSetCommentDialog.show();
        }
        mSetCommentDialog.showMethod(typeSetTeacherComment);
    }
}
