package com.atmob.keyboard_android.util.bridge.util

import java.util.concurrent.CopyOnWriteArrayList

/**
 * 插件初始化器
 */
object PluginInitializer {
    /**
     * 是否初始化完毕
     */
    @Volatile
    private var isPluginInitComplete: Boolean = false

    /**
     * 监听器
     */
    private val mPluginInitListenerList = CopyOnWriteArrayList<PluginInitListener>()

    /**
     * 插件初始化完毕回调
     */
    interface PluginInitListener {
        fun onPluginInitComplete()
    }

    /**
     * 添加监听
     */
    fun addPluginInitListener(listener: PluginInitListener) {
        // 已经初始化完毕，直接回调
        if (isPluginInitComplete) {
            listener.onPluginInitComplete()
        } else {
            // 未初始化，添加到监听器
            mPluginInitListenerList.add(listener)
        }
    }

    /**
     * 移除监听
     */
    fun removePluginInitListener(listener: PluginInitListener) {
        mPluginInitListenerList.remove(listener)
    }

    /**
     * 通知初始化完毕
     */
    fun notifyInitComplete() {
        isPluginInitComplete = true
        for (listener in mPluginInitListenerList) {
            listener.onPluginInitComplete()
        }
    }

    /**
     * 是否初始化完成
     */
    fun isPluginInitComplete(): Boolean {
        return isPluginInitComplete
    }
}