package com.adealink.weparty.order.dialog

import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextWatcher
import android.util.TypedValue
import androidx.fragment.app.viewModels
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.text.setMaxLength
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.commonui.widget.EvaluateView
import com.adealink.weparty.constant.ORDER_COMMENT_MAX_LENGTH
import com.adealink.weparty.module.order.data.OrderDetailInfo
import com.adealink.weparty.order.R
import com.adealink.weparty.order.databinding.DialogEvaluateOrderBinding
import com.adealink.weparty.order.viewmodel.OrderViewModelFactory
import com.adealink.weparty.order.viewmodel.UserOrderViewModel
import com.adealink.weparty.util.QMUIKeyboardHelper
import com.adealink.weparty.util.formatStar
import com.adealink.weparty.R as APP_R

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


    private val binding by viewBinding(DialogEvaluateOrderBinding::bind)

    private val viewModel by viewModels<UserOrderViewModel> { OrderViewModelFactory() }
    private var orderInfo: OrderDetailInfo? = null

    private var listener: OnEvaluateListener? = null

    fun setOrderInfo(data: OrderDetailInfo?) {
        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: Float) {
                updateEvaluate(score)
            }
        })
        updateEvaluate(binding.vEvaluate.getScore())

        binding.etInput.setMaxLength(ORDER_COMMENT_MAX_LENGTH)
        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/${ORDER_COMMENT_MAX_LENGTH}"
                } else {
                    binding.btnSubmit.isEnabled = true
                    binding.tvInputCount.text = "${input.length}/${ORDER_COMMENT_MAX_LENGTH}"
                }
            }
        })
        binding.btnSubmit.isEnabled = false
        binding.btnSubmit.onClick {
            submit()
        }
    }

    private fun updateEvaluate(star: Float) {
        if (star > 0f) {
            binding.vEvaluateLeft.setTextColor(getCompatColor(APP_R.color.color_CC1D2129))
            binding.vEvaluateRight.setTextColor(getCompatColor(APP_R.color.color_CC1D2129))
            binding.vEvaluateRight.text = formatStar(star)
            binding.vEvaluateRight.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
        } else {
            binding.vEvaluateLeft.setTextColor(getCompatColor(APP_R.color.color_FFCDCFD9))
            binding.vEvaluateRight.setTextColor(getCompatColor(APP_R.color.color_FFCDCFD9))
            binding.vEvaluateRight.text = getCompatString(R.string.order_evaluate_right_title)
            binding.vEvaluateRight.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)
        }
    }

    private fun submit() {
        val orderId = orderInfo?.orderId
        if (orderId.isNullOrEmpty()) {
            return
        }
        val star = binding.vEvaluate.getScore()
        if (star == 0f) {
            showToast(R.string.order_evaluate_star_is_null)
            return
        }
        val comment = binding.etInput.text?.trim()?.toString() ?: ""
        showLoading()
        viewModel.evaluateOrder(orderId, star, comment)
            .observe(viewLifecycleOwner) { rlt ->
                dismissLoading()
                when (rlt) {
                    is Rlt.Failed -> {
                        showFailedToast(rlt)
                    }

                    is Rlt.Success -> {
                        listener?.onEvaluate(star, comment)
                        dismiss()
                    }
                }
            }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.etInput.clearFocus()
        QMUIKeyboardHelper.hideKeyboard(binding.etInput)
    }

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