package com.cooleshow.teacher.ui.course;

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

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.base.widgets.EmptyViewLayout;
import com.cooleshow.teacher.CourseHelper;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.PianoRoomCourseStudentListAdapter;
import com.cooleshow.teacher.bean.PianoRoomCourseInfoBean;
import com.cooleshow.teacher.constants.CourseConstants;
import com.cooleshow.teacher.constants.TeacherConfig;
import com.cooleshow.teacher.contract.PianoRoomCourseDetailContract;
import com.cooleshow.teacher.databinding.ActivityPianoRoomDetailLayoutBinding;
import com.cooleshow.teacher.presenter.course.PianoRoomDetailPresenter;
import com.cooleshow.teacher.widgets.CourseSetCommentDialog;
import com.daya.live_teaching.utils.helper.CourseEnterHelper;

import java.util.List;

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

/**
 * Author by pq, Date on 2022/6/24.
 * 琴房课详情页面
 */
@Route(path = RouterPath.CourseCenter.PIANO_ROOM_COURSE_DETAIL)
public class PianoRoomCourseDetailActivity extends BaseMVPActivity<ActivityPianoRoomDetailLayoutBinding, PianoRoomDetailPresenter> implements PianoRoomCourseDetailContract.PianoRoomCourseDetailView, View.OnClickListener {
    public static final String COURSE_ID = "course_id";

    private String mCourseId;
    private PianoRoomCourseStudentListAdapter mAdapter;
    private CourseSetCommentDialog mSetCommentDialog;
    private PianoRoomCourseInfoBean mInfoBean;

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

    @Override
    protected void initView() {
        mCourseId = getIntent().getStringExtra(COURSE_ID);
        if (TextUtils.isEmpty(mCourseId)) {
            finish();
            return;
        }

        viewBinding.tvSetHomework.setOnClickListener(this);
        viewBinding.ivGoChat.setOnClickListener(this);
        viewBinding.tvEnterRoom.setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "课程详情");
        viewBinding.toolbarInclude.toolbar.setBackgroundColor(Color.WHITE);
        EmptyViewLayout emptyViewLayout = new EmptyViewLayout(this);
        emptyViewLayout.setContent(com.cooleshow.base.R.drawable.icon_empty_course, "暂无学员");
        mAdapter = new PianoRoomCourseStudentListAdapter(R.layout.item_piano_room_student_layout);
        mAdapter.setEmptyView(emptyViewLayout);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        viewBinding.recyclerView.setLayoutManager(linearLayoutManager);
        viewBinding.recyclerView.setAdapter(mAdapter);
        initListener();
    }

    private void initListener() {
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (mInfoBean == null) {
                return;
            }
            if (mInfoBean.decorateHomework == CourseConstants.COURSE_HOMEWORK_NO_SET) {
                ToastUtil.getInstance().showShort("您还未布置作业");
                return;
            }
            if (position < mAdapter.getData().size()) {
                PianoRoomCourseInfoBean.StudentHomeworkListBean.RecordsBean recordsBean = mAdapter.getData().get(position);
                ARouter.getInstance().build(RouterPath.WorkCenter.TEACHER_WORK_ASSIGN_HOMEWORK)
                        .withString(TeacherConfig.STUDENT_ID, recordsBean.studentId)
                        .withString(TeacherConfig.COURSE_ID, mCourseId)
                        .navigation();
            }
        });
    }

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

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

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

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

    @Override
    public void getCourseInfoSuccess(PianoRoomCourseInfoBean infoBean) {
        if (!checkActivityExist()) {
            return;
        }
        if (infoBean == null) {
            return;
        }
        mInfoBean = infoBean;
        //时间
        viewBinding.tvCourseTime.setText(UiUtils.getCourseTimeString(infoBean.startTime, infoBean.endTime));
        //title
        viewBinding.tvTitle.setText(infoBean.courseGroupName);
        //subjectName
        viewBinding.tvCourseName.setText(infoBean.subjectName);
        //学生人数
        viewBinding.tvCoursePeopleNum.setText(String.format("%s人", infoBean.studentNum));
        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);
        //学生列表数据
        handleStudentList(infoBean);
    }

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

    /**
     * 绑定学生列表数据
     *
     * @param infoBean
     */
    private void handleStudentList(PianoRoomCourseInfoBean infoBean) {
        if (mAdapter != null && infoBean != null && infoBean.studentHomeworkList != null) {
            mAdapter.setHomeWordStatus(infoBean.decorateHomework);
            List<PianoRoomCourseInfoBean.StudentHomeworkListBean.RecordsBean> records = infoBean.studentHomeworkList.records;
            if (records != null && records.size() > 0) {
                mAdapter.setNewInstance(records);
            } else {
                mAdapter.notifyDataSetChanged();
            }
        }
    }


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

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

    private void showSetCommentDialog() {
        if (mInfoBean == null) {
            return;
        }
        if (!TextUtils.equals(mInfoBean.courseStatus, CourseConstants.COURSE_FILTER_COMPLETED)) {
            ToastUtil.getInstance().showShort("课程结束之后才可以布置作业哦~");
            return;
        }
        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 (!mSetCommentDialog.isShowing()) {
            mSetCommentDialog.show();
        }
        mSetCommentDialog.showMethod(CourseSetCommentDialog.TYPE_SET_HOMEWORK);
    }
}
