package com.adealink.weparty.playmate.detail.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.R
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.module.im.IM
import com.adealink.weparty.module.order.Order
import com.adealink.weparty.module.order.data.discountLD
import com.adealink.weparty.module.order.data.getFinalPrice
import com.adealink.weparty.module.playmate.Playmate
import com.adealink.weparty.module.playmate.data.PlaymateDetailData
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.playmate.databinding.LayoutPlaymateDetailBottomBinding
import com.adealink.weparty.playmate.detail.dialog.PlaymateDetailSettingDialog
import com.adealink.weparty.playmate.detail.viewmodel.PlaymateDetailViewModel
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory
import com.adealink.weparty.util.formatNumber
import com.adealink.weparty.R as APP_R

class PlaymateDetailBottomComp(
    lifecycleOwner: LifecycleOwner,
    private val binding: LayoutPlaymateDetailBottomBinding,
) : ViewComponent(lifecycleOwner) {

    private val detailViewModel by activityViewModels<PlaymateDetailViewModel> { PlaymateViewModelFactory() }
    private var data: PlaymateDetailData? = null

    override fun onCreate() {
        super.onCreate()
        initView()
        observeViewModel()
    }

    private fun initView() {
        binding.root.setPadding(
            16.dp(),
            16.dp(),
            16.dp(),
            getCompatDimension(R.dimen.main_bottom_bar_margin_bottom).toInt()
        )

        binding.vOther.btnOrder.onClick {
            createOrder()
        }
        binding.vOther.btnChat.onClick {
            chat()
        }

        binding.vSelf.btnEdit.onClick {
            clickEdit()
        }
        binding.vSelf.btnSetting.onClick {
            clickSetting()
        }
    }

    private fun observeViewModel() {
        detailViewModel.detailLD.observe(viewLifecycleOwner) {
            this@PlaymateDetailBottomComp.data = it
            if (it?.uid == ProfileModule.getMyUid()) {
                binding.root.show()
                binding.vSelf.root.show()
                binding.vOther.root.gone()
                updateSelfBottom()
                return@observe
            }
            binding.root.show()
            binding.vSelf.root.gone()
            binding.vOther.root.show()
            updateOtherBottom()
        }
        discountLD.observe(viewLifecycleOwner) {
            updateOtherBottom()
        }
    }

    private fun updateSelfBottom() {
        val data = data
        if (data == null) {
            binding.vSelf.vPrice.gone()
        } else {
            binding.vSelf.vPrice.show()
            binding.vSelf.vPrice.setPrice(data.price, data.unit)
        }
    }

    private fun updateOtherBottom() {
        val data = data
        val finalPrice = getFinalPrice(data?.price)
        if (data == null) {
            binding.vOther.vPrice.gone()
            return
        }
        if (finalPrice.hasDiscount()) {
            binding.vOther.vPriceDiscount.show()
            binding.vOther.vPriceDiscount.text = getCompatString(
                APP_R.string.common_price_discount_new_only,
                formatNumber(finalPrice.discountPercent, true)
            )
            binding.vOther.vPrice.show()
            binding.vOther.vPrice.setPrice(finalPrice.discountPrice, data.unit)
        } else {
            binding.vOther.vPriceDiscount.gone()
            binding.vOther.vPrice.show()
            binding.vOther.vPrice.setPrice(data.price, data.unit)
        }

    }

    private fun createOrder() {
        val data = detailViewModel.detailLD.value ?: return
        val act = activity ?: return
        Router.build(act, Order.Create.PATH)
            .putExtra(Order.Create.EXTRA_PLAYMATE, data)
            .start()
    }

    private fun chat() {
        val act = activity ?: return
        val data = detailViewModel.detailLD.value ?: return

        Router.build(act, IM.Session.PATH)
            .putExtra(IM.Session.EXTRA_CHAT_ID, data.uid)
            .putExtra(IM.Session.EXTRA_CHAT_PLAYMATE_ID, data.playmateId)
            .start()
    }

    private fun clickEdit() {
        val data = detailViewModel.detailLD.value ?: return
        val act = activity ?: return
        Router.build(act, Playmate.PlaymateSetting.PATH)
            .putExtra(Playmate.PlaymateSetting.EXTRA_PLAYMATE, data)
            .start()
    }

    private fun clickSetting() {
        val data = detailViewModel.detailLD.value ?: return
        PlaymateDetailSettingDialog().apply {
            setPlaymateData(data)
        }.show(fragmentManager, "PlaymateDetailSettingDialog")
    }

}