package com.jishisongfu.duobibi.floating

import com.jishisongfu.duobibi.service.PriceBotAccessibilityService
import java.lang.ref.WeakReference

/**
 * 在两个 Service 之间做最薄的耦合 —— 浮窗服务通过这个 holder 拿到无障碍服务实例,
 * 调用其 [PriceBotAccessibilityService.captureCurrentTree],并读取最近一次进入前台的包名。
 *
 * 同 APK 同进程默认共享类加载器,静态引用直接可用。
 */
object AccessibilityServiceHolder {

    @Volatile
    private var ref: WeakReference<PriceBotAccessibilityService>? = null

    @Volatile
    var lastForegroundPackage: String? = null
        private set

    fun attach(service: PriceBotAccessibilityService) {
        ref = WeakReference(service)
    }

    fun detach() {
        ref = null
    }

    fun get(): PriceBotAccessibilityService? = ref?.get()

    fun updateForegroundPackage(pkg: String) {
        lastForegroundPackage = pkg
    }
}
