package com.cooleshow.student.ui.course;

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.alibaba.android.arouter.launcher.ARouter;
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.UiUtils;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.student.R;
import com.cooleshow.student.bean.PianoRoomCourseHomeworkBean;
import com.cooleshow.student.constants.CourseConstants;
import com.cooleshow.student.contract.PianoRoomCourseDetailContract;
import com.cooleshow.student.databinding.ActivityPianoRoomCourseDetailLayoutBinding;
import com.cooleshow.student.presenter.course.PianoRoomCourseDetailPresenter;
import com.cooleshow.base.widgets.VideoThumbnailView;
import com.cooleshow.student.widgets.helper.CourseHelper;
import com.daya.live_teaching.utils.helper.CourseEnterHelper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.rong.imkit.utils.helper.OpenChatHelper;

/**
 * Author by pq, Date on 2022/4/27.
 */

@Route(path = RouterPath.CourseCenter.PIANO_ROOM_COURSE_DETAIL)
public class PianoRoomCourseDetailActivity extends BaseMVPActivity<ActivityPianoRoomCourseDetailLayoutBinding, PianoRoomCourseDetailPresenter> implements PianoRoomCourseDetailContract.PianoRoomCourseDetailView, View.OnClickListener {
    public static final String COURSE_ID = "course_id";
    private String mCourseId;
    private PianoRoomCourseHomeworkBean mInfoBean;
    private boolean teacherIsReply = false;//老师是否已点评作业


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

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

    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "课程详情");
        mCourseId = getIntent().getStringExtra(COURSE_ID);
        if (TextUtils.isEmpty(mCourseId)) {
            finish();
            return;
        }
        viewBinding.ivGoChat.setOnClickListener(this);
        viewBinding.tvEnterRoom.setOnClickListener(this);
        viewBinding.tvEditHomework.setOnClickListener(this);
    }

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

    }

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

    private void refresh() {
        if (presenter != null) {
            presenter.getPianoRoomCourseInfo(mCourseId);
        }
    }

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

    /**
     * 获取课程详情成功
     *
     * @param infoBean
     */
    @Override
    public void getPianoRoomCourseSuccess(PianoRoomCourseHomeworkBean infoBean) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (infoBean == null) {
            return;
        }
        this.mInfoBean = infoBean;
        //时间
        viewBinding.tvCourseTime.setText(UiUtils.getCourseTimeString(infoBean.startTime, infoBean.endTime));
        //title
        viewBinding.tvTitle.setText(infoBean.courseGroupName);
        //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));
            viewBinding.tvEnterRoom.setVisibility(View.GONE);
            viewBinding.bottomView.setVisibility(View.GONE);
        }
        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));
            viewBinding.tvEnterRoom.setVisibility(View.VISIBLE);
            viewBinding.bottomView.setVisibility(View.VISIBLE);
        }
        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));
            viewBinding.tvEnterRoom.setVisibility(View.GONE);
            viewBinding.bottomView.setVisibility(View.GONE);
        }
        //课后作业
        handleHomeworkData(infoBean);
        //作业视频
        handleHomeworkStudentSubmit(infoBean);
        //作业点评
        handleHomeworkComment(infoBean);
    }

    @Override
    public void getPianoRoomCourseHomeworkError() {

    }

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

    /**
     * 学生提交的作业
     *
     * @param bean
     */
    private void handleHomeworkStudentSubmit(PianoRoomCourseHomeworkBean bean) {
        if (bean == null) {
            return;
        }
        if (!TextUtils.equals(CourseConstants.COURSE_STATUS_COMPLETE, bean.courseStatus)) {
            viewBinding.tvHomeworkVideoEmptyText.setVisibility(View.VISIBLE);
            viewBinding.tvHomeworkVideoEmptyText.setText("课程结束后才可提交作业");
            viewBinding.tvEditHomework.setVisibility(View.GONE);
            return;
        }
        if (bean.submitHomework == CourseConstants.COURSE_HOMEWORK_STU_SUBMIT) {
            //学员已提交课后作业
            if (bean.homeworkExpire == CourseConstants.COURSE_HOMEWORK_NO_EXPIRED) {
                viewBinding.tvHomeworkVideoEmptyText.setVisibility(View.GONE);
                viewBinding.llHomeworkVideoContent.removeAllViews();
                addVideoThumbnailView(bean.studentAttachments);
            } else {
                viewBinding.llHomeworkVideoContent.setVisibility(View.GONE);
                viewBinding.tvHomeworkVideoEmptyText.setVisibility(View.VISIBLE);
                viewBinding.tvHomeworkVideoEmptyText.setText("作业已过期无法提交和查看！");
            }

            if (teacherIsReply || bean.homeworkExpire == CourseConstants.COURSE_HOMEWORK_SET) {
                viewBinding.tvEditHomework.setVisibility(View.GONE);
            } else {
                viewBinding.tvEditHomework.setVisibility(View.VISIBLE);
            }
        } else {
            //学员未提交课后作业
            if (bean.decorateHomework == CourseConstants.COURSE_HOMEWORK_SET) {
                //有课后作业才显示编辑按钮
                viewBinding.tvEditHomework.setVisibility(View.VISIBLE);
                viewBinding.tvHomeworkVideoEmptyText.setText("您尚未上传作业视频");
            } else {
                viewBinding.tvEditHomework.setVisibility(View.GONE);
                viewBinding.tvHomeworkVideoEmptyText.setText("课程结束之后可上传视频作业");
            }
            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(PianoRoomCourseDetailActivity.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(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.GONE);
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_go_chat) {
            //跳转聊天
            if (mInfoBean != null) {
                OpenChatHelper.goGroupChat(PianoRoomCourseDetailActivity.this, mInfoBean.imGroupId, "");
            }
            return;
        }
        if (v.getId() == R.id.tv_enter_room) {
            //进入教室
            if (mInfoBean != null) {
                CourseEnterHelper.start(this, mCourseId, mInfoBean.subjectName);
            }
            return;
        }

        if (v.getId() == R.id.tv_edit_homework) {
            //跳转课后作业
            if (TextUtils.isEmpty(mCourseId)) {
                return;
            }
            ARouter.getInstance().build(RouterPath.WorkCenter.STUDENT_HOMEWORK_DETAIL)
                    .withString("course_id", mCourseId)
                    .navigation();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
