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

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.frame.router.Router
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.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 PlaymateShareComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutMeShareBinding
) : ViewComponent(lifecycleOwner) {

    private val profileViewModel by activityViewModels<ProfileViewModel> {
        ProfileViewModelFactory()
    }

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

    private fun initView() {
        binding.root.onClick {
            generateQRCode()
        }
    }

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

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

    private fun updateUI(userInfo: UserInfo?) {
        userInfo ?: return
        if (userInfo.isPlaymate()) {
            binding.root.show()
        } else {
            binding.root.gone()
        }
    }


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

}