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

import android.annotation.SuppressLint
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.frame.util.onClick
import com.adealink.weparty.playmate.data.PlaymateDetailRes
import com.adealink.weparty.playmate.databinding.LayoutPlaymateDetailHeaderBinding
import com.adealink.weparty.playmate.detail.viewmodel.PlaymateDetailViewModel
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory
import com.adealink.weparty.util.goImagePreviewActivity

class PlaymateDetailHeaderComp(
    lifecycleOwner: LifecycleOwner,
    private val binding: LayoutPlaymateDetailHeaderBinding,
) : ViewComponent(lifecycleOwner) {

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

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

    private fun initView() {
        binding.ivBg.onClick {
            goBgPreview()
        }
        binding.tvEvaluate.onClick {
            showEvaluateDialog()
        }
    }

    private fun observeViewModel() {
        detailViewModel.detailLD.observe(viewLifecycleOwner) { data ->
            updateHeader(data)
        }
    }

    @SuppressLint("SetTextI18n")
    private fun updateHeader(data: PlaymateDetailRes?) {
        binding.ivBg.setImageUrl(data?.cover)

        binding.tvName.text = data?.nickname
        binding.tvUserId.text = "ID ${data?.uid ?: ""}"
        binding.vGender.setGender(data?.gender)
        binding.vGender.setAge(data?.age ?: 0)
        // TODO: 位置信息,语言从用户数据里获取
        binding.tvLocation.text = data?.nickname
        binding.tvLanguage.text = data?.nickname

        binding.tvEvaluate.text = data?.star?.toString() ?: ""
    }

    private fun goBgPreview() {
        val act = activity ?: return
        val cover = detailViewModel.detailLD.value?.cover
        if (cover.isNullOrEmpty()) {
            return
        }
        goImagePreviewActivity(
            act,
            arrayListOf(cover)
        )
    }

    private fun showEvaluateDialog() {

    }

}