package com.atmob.keyboard_android.component.root

import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.atmob.keyboard_android.R
import com.atmob.keyboard_android.component.base.BaseUIComponent
import com.atmob.keyboard_android.component.base.IUIComponent
import com.atmob.keyboard_android.component.base.RouteComponent
import com.atmob.keyboard_android.component.base.interceptor.RouteCallbackAdapter
import com.atmob.keyboard_android.component.child.IKeyBoardContainerComponent
import com.atmob.keyboard_android.component.child.IKeyboardGuideComponent
import com.atmob.keyboard_android.component.child.IKeyboardGuideComponent.OnActionCallback
import com.atmob.keyboard_android.component.child.IKeyboardSelectComponent
import com.atmob.keyboard_android.component.child.ISettingComponent
import com.atmob.keyboard_android.component.page.ILoginPageComponent
import com.atmob.keyboard_android.component.page.IVipPageComponent
import com.atmob.keyboard_android.model.AiKeyboardKeyModel

/**
 * 键盘根组件
 */
class KeyboardRootComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent<IKeyboardRootComponent>(context, attrs, defStyleAttr), IKeyboardRootComponent {
    private lateinit var vRouteComponent: RouteComponent

    override fun onInflateViewId(): Int {
        return R.layout.component_keyboard_root
    }

    override fun findView(view: View) {
        vRouteComponent = view.findViewById(R.id.root_route_component)
    }

    override fun bindView(view: View) {
    }

    override fun getComponentInterfaceClazz(): Class<IKeyboardRootComponent> {
        return IKeyboardRootComponent::class.java
    }

    override fun switchVipPage() {
        vRouteComponent.routeChildComponent(IVipPageComponent::class.java)
    }

    override fun switchLoginPage() {
        vRouteComponent.routeChildComponent(ILoginPageComponent::class.java)
    }

    override fun switchSettingPage() {
        vRouteComponent.routeChildComponent(ISettingComponent::class.java)
    }

    override fun switchKeyboardSelectPage() {
        vRouteComponent.routeChildComponent(IKeyboardSelectComponent::class.java)
    }

    override fun switchKeyBoardPage() {
        vRouteComponent.routeChildComponent(IKeyBoardContainerComponent::class.java)
    }

    override fun showKeyboardGuideDialog(
        targetView: View,
        dataModel: AiKeyboardKeyModel,
        finishCallback: OnActionCallback
    ) {
        vRouteComponent.routeChildComponent(
            IKeyboardGuideComponent::class.java,
            mutex = false,
            object : RouteCallbackAdapter() {
                override fun onFound(targetComponent: IUIComponent) {
                    super.onFound(targetComponent)
                    // 绑定引导数据
                    (targetComponent as IKeyboardGuideComponent).bindGuideData(
                        targetView,
                        dataModel,
                        finishCallback
                    )
                }
            })
    }
}