package com.yidian.shenghuoquan.bsetting.ui.setting

import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import androidx.activity.viewModels
import com.yidian.bcommon.mvvm.BaseMvvmActivity
import com.yidian.bcommon.utils.InputFilterUtil
import com.yidian.bcommon.utils.ToastUtils
import com.yidian.shenghuoquan.bsetting.R
import com.yidian.shenghuoquan.bsetting.databinding.ActivityFeedbackBinding

/**
 * author: yinjiacheng
 * date: 7/16/21 10:24 PM
 * description: 用户反馈
 */
class FeedbackActivity : BaseMvvmActivity<ActivityFeedbackBinding, FeedbackViewModel>(), TextWatcher {

  override val layoutId: Int = R.layout.activity_feedback
  override val vm: FeedbackViewModel by viewModels()

  override fun initView() {
    // 限制最多输入100字 禁止输入Emoji
    binding.etInput.filters = arrayOf(InputFilter.LengthFilter(100), InputFilterUtil.getEmojiFilter())
    binding.etInput.addTextChangedListener(this)
    subscribeData()
  }

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

  }

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

  }

  override fun afterTextChanged(s: Editable?) {
    // 字数统计
    s?.length?.let { vm.feedbackWordCount.value = it }
  }

  private fun subscribeData() {
    vm.commitSuccessEvent.observe(this, {
      ToastUtils.showShort("提交成功")
      finish()
    })
    vm.commitFailureEvent.observe(this, {

    })
    vm.feedbackContentEmptyEvent.observe(this, {
      ToastUtils.showShort("您还没有填写反馈意见呦")
    })
  }
}
