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

import android.annotation.SuppressLint
import android.os.Build
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.TypefaceSpan
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.ext.findAndSetSpan
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.frame.util.copyToClipBoard
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.DEFAULT_FONT_BOLD
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.ext.sp
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.module.playmate.data.PlaymateDetailData
import com.adealink.weparty.playmate.R
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.formatDistanceStr
import com.adealink.weparty.util.formatStar
import com.adealink.weparty.util.goImagePreviewActivity
import com.adealink.weparty.R as APP_R

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.tvUserId.onClick {
            copyUserId()
        }
        binding.ivCopyUserId.onClick {
            copyUserId()
        }
    }

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

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

        binding.tvName.text = data?.nickname
        binding.tvUserId.text = "ID ${data?.uid ?: ""}"
        binding.vGender.setGender(data?.gender)
        binding.vGender.setAge(data?.age ?: 0)

//        if (data?.distance == null) {
//            binding.clLocation.gone()
//        } else {
//            binding.clLocation.show()
//            binding.tvLocation.text = formatDistanceStr(data.distance)
//        }
        val languageStr = data?.languageNames?.joinToString(separator = "/")
        if (languageStr.isNullOrEmpty()) {
            binding.clLanguage.gone()
        } else {
            binding.clLanguage.show()
            binding.tvLanguage.text = languageStr
        }
        binding.tvEvaluate.text = formatStar(data?.star)

        val orderCountStr = data?.orderCount?.toString() ?: "0"
        val text = getCompatString(R.string.playmate_order_count, orderCountStr)
        binding.tvOrderCount.text = SpannableStringBuilder(text).apply {
            findAndSetSpan(
                AbsoluteSizeSpan(12.sp()),
                orderCountStr
            )
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                findAndSetSpan(
                    TypefaceSpan(DEFAULT_FONT_BOLD),
                    orderCountStr
                )
            }
        }
    }

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

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

}