package com.adealink.weparty.order.dialog.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.isViewValid
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.module.order.data.OrderDiscountInfo
import com.adealink.weparty.module.order.data.getFinalPrice
import com.adealink.weparty.module.order.data.observeDiscount
import com.adealink.weparty.module.order.data.unObserveDiscount
import com.adealink.weparty.module.profile.data.UserPlaymateSkill
import com.adealink.weparty.order.R
import com.adealink.weparty.order.databinding.LayoutCreateOrderCategoryItemBinding
import com.adealink.weparty.util.formatNumber
import com.adealink.weparty.R as APP_R

data class CategoryListItemData(
    val data: UserPlaymateSkill,
    var selected: Boolean
)

class CategoryItemViewBinder(
    val owner: LifecycleOwner,
    val listener: CategoryChooseListener
) :
    ItemViewBinder<CategoryListItemData, CategoryItemViewBinder.ViewHolder>() {

    override fun onBindViewHolder(
        holder: ViewHolder,
        item: CategoryListItemData,
    ) {
        holder.update(item)
    }

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): ViewHolder {
        return ViewHolder(
            LayoutCreateOrderCategoryItemBinding.inflate(
                inflater,
                parent,
                false
            )
        ).also {
            it.initView()
        }
    }

    override fun onViewAttachedToWindow(holder: ViewHolder) {
        super.onViewAttachedToWindow(holder)
        observeDiscount(owner, holder.discountObserver)
    }

    override fun onViewDetachedFromWindow(holder: ViewHolder) {
        super.onViewDetachedFromWindow(holder)
        unObserveDiscount(holder.discountObserver)
    }

    inner class ViewHolder(
        binding: LayoutCreateOrderCategoryItemBinding,
    ) : BindingViewHolder<LayoutCreateOrderCategoryItemBinding>(binding) {
        private var item: CategoryListItemData? = null

        fun initView() {
            binding.root.onClick {
                val item = this.item ?: return@onClick
                listener.onChoose(item, layoutPosition)
            }
        }

        fun update(item: CategoryListItemData) {
            this.item = item

            if (item.selected) {
                binding.root.setBackgroundResource(R.drawable.create_order_category_item_selected_bg)
            } else {
                binding.root.setBackgroundResource(R.drawable.create_order_category_item_bg)
            }
            binding.ivIcon.setImageUrl(item.data.icon)
            binding.tvTitle.text = item.data.name
            binding.vPrice.setPrice(item.data.price, item.data.unit)

            updatePrice()
        }

        val discountObserver = object : Observer<OrderDiscountInfo?> {

            override fun onChanged(value: OrderDiscountInfo?) {
                updatePrice()
            }
        }

        private fun updatePrice() {
            if (!isViewValid()) {
                return
            }
            val finalPrice = getFinalPrice(item?.data?.price ?: 0f)
            binding.vDiscount.show(finalPrice.hasDiscount())
            binding.vDiscount.text = getCompatString(
                APP_R.string.common_price_discount,
                formatNumber(finalPrice.discountPercent, true)
            )
        }
    }

    interface CategoryChooseListener {
        fun onChoose(data: CategoryListItemData, positon: Int)
    }
}