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

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.frame.util.naviBarHeight
import com.adealink.frame.util.onClick
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.IMModule
import com.adealink.weparty.playmate.data.PlaymateDetailRes
import com.adealink.weparty.playmate.databinding.LayoutPlaymateDetailBottomBinding
import com.adealink.weparty.playmate.detail.viewmodel.PlaymateDetailViewModel
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory

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

    private val detailViewModel by activityViewModels<PlaymateDetailViewModel> { PlaymateViewModelFactory() }

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

    private fun initView() {
        binding.root.setPadding(16.dp(), 16.dp(), 16.dp(), activity.naviBarHeight() + 24.dp())
        binding.btnOrder.onClick {
            createOrder()
        }
        binding.btnChat.onClick {
            chat()
        }
    }

    private fun observeViewModel() {
        detailViewModel.detailLD.observe(viewLifecycleOwner) {
            updateBottom(it)
        }
    }

    private fun updateBottom(data: PlaymateDetailRes?) {
        if (data == null) {
            binding.vPrice.gone()
        } else {
            binding.vPrice.show()
            binding.vPrice.setPrice(data.price, data.unit)
        }
    }

    private fun createOrder() {

    }

    private fun chat() {
        val act = activity ?: return
        val uid = detailViewModel.detailLD.value?.uid ?: return
        IMModule.goSession(act, uid)
    }

}