package com.yidianling.course.coursePlay

import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import com.alibaba.android.arouter.facade.annotation.Route
import com.yidianling.common.tools.ToastUtil
import com.yidianling.course.CourseConstants
import com.yidianling.course.R
import com.yidianling.course.coursePlay.common.net.Command
import com.yidianling.course.coursePlay.common.net.RetrofitUtils
import com.yidianling.ydlcommon.base.BaseActivity
import com.yidianling.ydlcommon.h5.H5Params
import com.yidianling.ydlcommon.h5.NewH5Activity
import com.yidianling.ydlcommon.log.LogHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_course_commeny.*

/**
 * @author jiucheng
 * @描述:课程写评价页面
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/7/2
 */
@Route(path = "/course/comment")
class CourseCommentActivity : BaseActivity() {
    private var courseId: String? = null

    companion object {
        private const val COURSE_ID = "course_id"
        fun startActivity(activity: Activity, courseId: String) {
            var intent = Intent(activity, CourseCommentActivity::class.java)
            intent.putExtra(COURSE_ID, courseId)
            activity.startActivity(intent)
        }
    }

    override fun layoutResId(): Int {
        return R.layout.activity_course_commeny
    }

    override fun initDataAndEvent() {
        courseId = intent.getStringExtra(COURSE_ID)
        if (TextUtils.isEmpty(courseId)) {
            ToastUtil.toastShort("参数错误，请重试")
            return
        }
        iv_back.setOnClickListener {
            finish()
        }

        tv_left_menu.setOnClickListener {
            if (TextUtils.isEmpty(et_comment.text.toString())) {
                ToastUtil.toastShort("请输入评论内容")
                return@setOnClickListener
            }
            commitReply(et_comment.text.toString())
        }

        et_comment.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if (TextUtils.isEmpty(s)) {
                    tv_input_length.text = "0"
                    tv_input_length.setTextColor(resources.getColor(R.color.color_b3b3b3))
                } else {
                    tv_input_length.text = s!!.length.toString()
                    tv_input_length.setTextColor(resources.getColor(R.color.color_242424))
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
    }

    //提交评论
    private fun commitReply(content: String) {
        showProgressDialog(null)
        val cmd = Command.CourseCommitReply(courseId!!.toInt(), content)
        RetrofitUtils.commitCourseReply(cmd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                    dismissProgressDialog()
                    if (resp.code == 200) {
                        ToastUtil.toastShort(resp.msg)

                        val h5Params = H5Params(CourseConstants.COURSE_COMMENT_H5 + courseId, "全部评价")
                        NewH5Activity.start(this@CourseCommentActivity, h5Params)

                        this@CourseCommentActivity.finish()
                    } else {
                        ToastUtil.toastShort(resp.msg)
                        LogHelper.getInstance().writeLogSync("提交评论失败：" + resp.msg)
                    }
                }, { t ->
                    dismissProgressDialog()
                    RetrofitUtils.handleError(this, t)
                    LogHelper.getInstance().writeLogSync("提交评论失败：" + t.message)
                })
    }
}
