package com.adealink.weparty.profile.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.viewModels
import com.adealink.frame.util.removeUiCallbacks
import com.adealink.frame.util.runOnUiThread
import com.adealink.weparty.module.playmate.event.NotifyPlaymateWelcomeEvent
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory

class ProfileEventComp(
    lifecycleOwner: LifecycleOwner,
) : ViewComponent(lifecycleOwner) {

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

    private val notifyWelcome = object : Runnable {
        override fun run() {
            val userInfo = profileViewModel.userInfoLD.value ?: return
            if (userInfo.uid != ProfileModule.getMyUid() && userInfo.isPlaymate()) {
                NotifyPlaymateWelcomeEvent().apply {
                    uid to userInfo.uid
                }.send()
            }
        }
    }

    override fun onCreate() {
        super.onCreate()
        runOnUiThread(notifyWelcome, 3000)
    }

    override fun onDestroy() {
        super.onDestroy()
        removeUiCallbacks(notifyWelcome)
    }

}