package com.adealink.weparty.profile.me.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.viewModels
import com.adealink.frame.router.Router
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.module.share.Share
import com.adealink.weparty.profile.databinding.LayoutMeShareBinding
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory

class MeShareComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutMeShareBinding
) : ViewComponent(lifecycleOwner) {

    private val profileViewModel by viewModels<ProfileViewModel>(
        ownerProducer = { viewModelStoreOwner },
        factoryProducer = { ProfileViewModelFactory() }
    )

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

    private fun initView() {
//        val qrCodeText = getCompatString(R.string.profile_generate_qr_code)
//        binding.tvQrCode.text = SpannableStringBuilder(qrCodeText).apply {
//            findAndSetSpan(
//                CenterImageSpan(getCompatDrawable(R.drawable.profile_share_qr_code_tag_ic).apply {
//                    setBounds(0, 0, 16.dp(), 16.dp())
//                }),
//                IMG_TAG
//            )
//        }
//
//        val shareFriendText = getCompatString(R.string.profile_share_with_friends)
//        binding.tvShareFriend.text = SpannableStringBuilder(shareFriendText).apply {
//            findAndSetSpan(
//                CenterImageSpan(getCompatDrawable(R.drawable.profile_share_friends_tag_ic).apply {
//                    setBounds(0, 0, 16.dp(), 16.dp())
//                }),
//                IMG_TAG
//            )
//        }
    }

    private fun observeViewModel() {
        profileViewModel.userInfoLD.observe(viewLifecycleOwner) { user ->
            updateHeader(user)
        }
    }

    private fun loadData() {
        updateHeader(ProfileModule.getMyUserInfo())
    }

    private fun updateHeader(userInfo: UserInfo?) {
    }


    private fun generateQRCode() {
        Router.getRouterInstance<BottomDialogFragment>(Share.QRCodeShare.PATH)
            ?.show(fragmentManager, Share.QRCodeShare.PATH)
    }

    private fun clickShare() {
        activity?.let { act ->
            Router.build(act, Share.Poster.PATH).start()
        }
    }

    companion object{
        private const val IMG_TAG = "[IMG]"
    }

}