package com.atmob.keyboard_android.component.page.impl

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import com.atmob.keyboard_android.R
import com.atmob.keyboard_android.component.ComponentConstant
import com.atmob.keyboard_android.component.annotation.Component
import com.atmob.keyboard_android.component.base.BaseUIComponent
import com.atmob.keyboard_android.component.base.animator.ComponentAnimator
import com.atmob.keyboard_android.component.base.animator.impl.TransitionAnimator
import com.atmob.keyboard_android.component.page.ILoginPageComponent
import com.atmob.keyboard_android.constant.Constants
import com.atmob.keyboard_android.ext.click
import com.atmob.keyboard_android.ext.loadResDrawableToCorner
import com.atmob.keyboard_android.util.KeyboardHolder
import com.atmob.keyboard_android.util.bridge.FlutterBridgeManager

/**
 * 登录页组件
 */
@Component(route = ComponentConstant.ROUTE_LOGIN)
class LoginPageComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent<ILoginPageComponent>(context, attrs, defStyleAttr), ILoginPageComponent {
    private lateinit var vBackBtn: View
    private lateinit var vAppIcon: ImageView
    private lateinit var vLoginBtn: View

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

    override fun findView(view: View) {
        vBackBtn = view.findViewById(R.id.back_btn)
        vAppIcon = view.findViewById(R.id.app_icon)
        vLoginBtn = view.findViewById(R.id.login_btn)
    }

    override fun bindView(view: View) {
        vBackBtn.click {
            // 关闭
            hide()
        }
        vAppIcon.apply {
            loadResDrawableToCorner(R.mipmap.ic_app_icon, 46f, 0)
        }
        vLoginBtn.click {
            // 跳转到登录页，并关闭自身
            FlutterBridgeManager.jump2LoginPage()
            hide()
        }
        setData()
    }

    private fun setData() {
    }

    override fun onComponentShow() {
        super.onComponentShow()
        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()?.updateLoginPageShowing(true)
    }

    override fun onComponentHide() {
        super.onComponentHide()
        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()?.updateLoginPageShowing(false)
    }

    override fun getComponentAnimator(): ComponentAnimator {
        return if (Constants.COMPONENT_ANIMATOR_ENABLE) {
            TransitionAnimator()
        } else {
            super.getComponentAnimator()
        }
    }

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