package com.adealink.weparty.module.room.base

import android.content.Intent
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.base.fastLazy
import com.adealink.frame.util.removeUiCallbacks
import com.adealink.frame.util.runOnUiThread
import com.adealink.weparty.mvvm.view.FragmentViewComponent

/**
 * 房间基础组件
 * 1. 延时loadData, observeViewModel()
 */
abstract class BaseRoomComp(
    lifecycleOwner: LifecycleOwner,
) : FragmentViewComponent(lifecycleOwner) {

    private val immediatelyLoad by fastLazy {
        Runnable {
            importantLoad()
        }
    }

    private val delayLoad by fastLazy {
        Runnable {
            delayLoad()
        }
    }


    override fun onCreate() {
        super.onCreate()
        initViews()
        observeViewModel()
        runOnUiThread(immediatelyLoad)
        runOnUiThread(delayLoad, 500L)
    }

    /**
     * initViews 直接操作控件会破坏组件懒加载机制,导致控件懒加载失效
     * 需要封装[com.adealink.weparty.commonui.layout.IDynamicView]
     */
    open fun initViews() {
    }

    private fun importantLoad() {
        loadData()
    }

    open fun loadData() {
    }

    open fun observeViewModel() {
    }

    private fun delayLoad() {
        delayLoadData()
    }

    open fun delayLoadData() {
    }

    override fun onDestroy() {
        super.onDestroy()
        removeUiCallbacks(immediatelyLoad)
        removeUiCallbacks(delayLoad)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        runOnUiThread(immediatelyLoad)
        runOnUiThread(delayLoad, 500L)
    }

}