package com.atmob.keyboard_android.component

import com.atmob.keyboard_android.component.base.IUIComponent

/**
 * 组件协调者，也是组件中介，负责组件之间的通信和交互
 */
object ComponentMediator {
    /**
     * 组件Map，保存组件的实例
     */
    private val mComponentMap: MutableMap<Class<*>, IUIComponent> = mutableMapOf()

    /**
     * 注册组件
     */
    fun <T : IUIComponent> registerComponent(
        componentInterfaceClazz: Class<T>,
        component: IUIComponent
    ) {
        mComponentMap[componentInterfaceClazz] = component
    }

    /**
     * 取消注册组件
     */
    fun <T : IUIComponent> unRegisterComponent(componentInterfaceClazz: Class<T>) {
        mComponentMap.remove(componentInterfaceClazz)
    }

    /**
     * 查找组件
     */
    fun <T : IUIComponent> findComponent(componentInterfaceClazz: Class<T>): T? {
        return mComponentMap[componentInterfaceClazz] as T?
    }
}