package com.duben.microtribe.ui.activitys

import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.RadioGroup
import com.duben.microtribe.R
import com.duben.microtribe.mvp.presenters.FeedbackPresenter
import com.duben.microtribe.mvp.views.FeedbackView
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.library.utils.nodoubleclick.AntiShake
import kotlinx.android.synthetic.main.activity_feekback.*
import kotlinx.android.synthetic.main.header_layout.*

/**
 * 描述：意见反馈
 * 作者：孟崔广
 * 时间：2023/7/7 19:19
 */
class FeedbackActivity : BaseActivity(), View.OnClickListener, FeedbackView {

    private val feedbackPresenter by lazy { FeedbackPresenter() }
    private var typeName = "界面"

    private val maxLength = 150
    private val minLength = 5

    override fun getContentViewLayoutID() = R.layout.activity_feekback

    override fun isApplyKitKatTranslucency() = false

    override fun initViewsAndEvents() {
        tv_title.text = "意见反馈"
        iv_left_icon.visibility = View.VISIBLE
        iv_left_icon.setImageResource(R.mipmap.ic_arrow_back)

        etFeedbackContent.isFocusable = true
        etFeedbackContent.isFocusableInTouchMode = true
        etFeedbackContent.requestFocus()

        initListener()
        feedbackPresenter.attachView(this)
    }

    private fun initListener() {
        iv_left_icon.setOnClickListener(this)
        btn_feedback_next.setOnClickListener(this)

        radioGroup.setOnCheckedChangeListener { _: RadioGroup?, checkedId: Int ->
            when (checkedId) {
                R.id.btn_opinion_type_1 -> typeName = "界面"
                R.id.btn_opinion_type_2 -> typeName = "体验"
                R.id.btn_opinion_type_3 -> typeName = "功能"
                R.id.btn_opinion_type_4 -> typeName = "其他"
            }
        }

        etFeedbackContent.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

            @SuppressLint("SetTextI18n")
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (s.toString().trim { it <= ' ' }.length > maxLength) {
                    etFeedbackContent.setText(s.toString().substring(0, maxLength))
                    etFeedbackContent.setSelection(maxLength)
                    showToast("您最多能输入150个字")
                }
                tvFeedbackCount.text = etFeedbackContent.text.toString().length.toString() + "/150"
            }

            override fun afterTextChanged(s: Editable) {}
        })
    }

    override fun onDestroy() {
        super.onDestroy()

        feedbackPresenter.detachView()
    }

    override fun onClick(v: View?) {
        if (AntiShake.check(v?.id)) return

        when (v?.id) {
            R.id.iv_left_icon -> finish()
            R.id.btn_feedback_next -> {
                val opinion = etFeedbackContent.text.toString().trim { it <= ' ' }
                if (TextUtils.isEmpty(opinion)) {
                    showToast("请输入反馈内容")
                    return
                }
                if (opinion.length < minLength) {
                    showToast("字数太少")
                    return
                }
                if (opinion.length > maxLength) {
                    showToast("字数太多")
                    return
                }

                feedbackPresenter.feedback(typeName, opinion)
            }
        }
    }

    override fun feedbackSuc() {
        showToast("提交成功")
        etFeedbackContent.setText("")
    }
}