package com.adealink.weparty.profile.comp

import android.annotation.SuppressLint
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.util.copyToClipBoard
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.profile.databinding.LayoutUserProfileHeaderBinding
import com.adealink.weparty.util.formatDistanceStr
import com.adealink.weparty.util.formatStar
import com.adealink.weparty.R as APP_R

class ProfileHeaderComp(
    lifecycleOwner: LifecycleOwner,
    private val userUid: String,
    private val binding: LayoutUserProfileHeaderBinding,
) : BaseProfileViewComp(lifecycleOwner, userUid) {

    override fun initView() {
        binding.tvUserId.onClick {
            copyUserId()
        }
        binding.ivCopyUserId.onClick {
            copyUserId()
        }
    }

    override fun observeViewModel() {
        profileViewModel.userInfoLD.observe(viewLifecycleOwner) {
            updateUI(it)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun updateUI(userInfo: UserInfo?) {
        binding.ivBg.setImageUrl(userInfo?.avatar)
        binding.tvName.text = userInfo?.nickName
        binding.vGender.setGender(userInfo?.gender)
        binding.vGender.setAge(userInfo?.age)
//        if (isSelf) {
//            binding.ivLocation.gone()
//            binding.tvLocation.gone()
//        } else {
//            binding.ivLocation.show()
//            binding.tvLocation.show()
//            binding.tvLocation.text = formatDistanceStr(userInfo?.distance ?: 0f)
//        }

        binding.tvUserId.text = "ID ${userInfo?.uid}"
        binding.tvFans.text =
            getCompatString(APP_R.string.common_fans_num, userInfo?.fansCount ?: "")
        if (userInfo?.voiceBar.isNullOrEmpty() || !userInfo.isPlaymate()) {
            binding.vSound.gone()
        } else {
            binding.vSound.show()
            binding.vSound.setSoundUrl(userInfo.voiceBar)
        }

        if (userInfo?.isPlaymate() == true) {
            binding.ivEvaluateStar.show()
            binding.tvEvaluate.show()
            binding.tvEvaluate.text = formatStar(userInfo.star)
        } else {
            binding.ivEvaluateStar.gone()
            binding.tvEvaluate.gone()
        }
    }

    private fun copyUserId() {
        val userInfo = profileViewModel.userInfoLD.value
        val userIdStr = userInfo?.uid
        if (userIdStr.isNullOrEmpty()) {
            return
        }
        copyToClipBoard(userIdStr, userIdStr)
        showToast(getCompatString(APP_R.string.common_copy_success))
    }
}