package com.adealink.weparty.order.dialog

import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextWatcher
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.commonui.widget.EvaluateView
import com.adealink.weparty.order.R
import com.adealink.weparty.order.data.UserOrderData
import com.adealink.weparty.order.databinding.DialogEvaluateOrderBinding
import com.adealink.weparty.R as APP_R

class EvaluateOrderDialog : BottomDialogFragment(R.layout.dialog_evaluate_order) {

    companion object {
        private const val MAX_INPUT_COUNT = 200
    }

    private val binding by viewBinding(DialogEvaluateOrderBinding::bind)
    private var orderInfo: UserOrderData? = null

    private var listener: OnEvaluateListener? = null

    fun setOrderInfo(data: UserOrderData?) {
        this.orderInfo = data
    }

    fun setListener(listener: OnEvaluateListener?) {
        this.listener = listener
    }

    override fun initViews() {
        super.initViews()
        binding.ivAvatar.setImageUrl(orderInfo?.avatar)

        binding.vEvaluate.setListener(object : EvaluateView.OnEvaluateListener {
            override fun onEvaluate(score: Int) {
                updateEvaluate(score)
            }
        })
        updateEvaluate(binding.vEvaluate.getScore())

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

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

            @SuppressLint("SetTextI18n")
            override fun afterTextChanged(s: Editable?) {
                val input = s?.trim()?.toString()
                if (input.isNullOrEmpty()) {
                    binding.btnSubmit.isEnabled = false
                    binding.tvInputCount.text = "0/${MAX_INPUT_COUNT}"
                } else {
                    binding.btnSubmit.isEnabled = true
                    binding.tvInputCount.text = "${input.length}/${MAX_INPUT_COUNT}"
                }
            }
        })

        binding.btnSubmit.onClick {
            listener?.onEvaluate(
                binding.vEvaluate.getScore(),
                binding.etInput.text?.trim()?.toString() ?: ""
            )
            dismiss()
        }
    }

    private fun updateEvaluate(star: Int) {
        if (star > 0) {
            binding.vEvaluateLeft.setTextColor(getCompatColor(APP_R.color.color_FFCDCFD9))
            binding.vEvaluateRight.setTextColor(getCompatColor(APP_R.color.color_FFCDCFD9))
        } else {
            binding.vEvaluateLeft.setTextColor(getCompatColor(APP_R.color.color_CC1D2129))
            binding.vEvaluateRight.setTextColor(getCompatColor(APP_R.color.color_CC1D2129))
        }
    }


    interface OnEvaluateListener {
        fun onEvaluate(star: Int, comment: String)
    }
}