package com.adealink.weparty.playmate.detail.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.playmate.detail.viewmodel.PlaymateDetailViewModel
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory

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

    private val detailViewModel by viewModels<PlaymateDetailViewModel> { PlaymateViewModelFactory() }

    private val notifyWelcome = object : Runnable {
        override fun run() {
            val detail = detailViewModel.detailLD.value ?: return
            if (detail.uid != ProfileModule.getMyUid()) {
                NotifyPlaymateWelcomeEvent().apply {
                    uid to detail.uid
                }.send()
            }
        }
    }

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

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

}