package com.atmob.keyboard_android.component.child.impl

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.animator.ComponentAnimator
import com.atmob.keyboard_android.component.base.animator.impl.TransitionAnimator
import com.atmob.keyboard_android.component.child.ISettingComponent
import com.atmob.keyboard_android.constant.Constants
import com.atmob.keyboard_android.enums.host.HostMainTab
import com.atmob.keyboard_android.ext.click
import com.atmob.keyboard_android.util.KeyboardHolder
import com.atmob.keyboard_android.util.bridge.FlutterBridgeManager

/**
 * 设置页
 */
class SettingComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent<ISettingComponent>(context, attrs, defStyleAttr), ISettingComponent {
    private lateinit var vBackBtn: View
    private lateinit var vSettingCustomPersonLayout: View
    private lateinit var vSettingMarketLayout: View
    private lateinit var vSettingUnlockVipLayout: View

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

    override fun findView(view: View) {
        vBackBtn = view.findViewById(R.id.back_btn)
        vSettingCustomPersonLayout = view.findViewById(R.id.setting_custom_person_layout)
        vSettingMarketLayout = view.findViewById(R.id.setting_market_layout)
        vSettingUnlockVipLayout = view.findViewById(R.id.setting_unlock_vip_layout)
    }

    override fun bindView(view: View) {
        vBackBtn.click {
            // 关闭
            hide()
        }
        // 定制人设
        vSettingCustomPersonLayout.click {
            FlutterBridgeManager.jump2CustomCharacterPage()
        }
        // 人设市场
        vSettingMarketLayout.click {
            FlutterBridgeManager.jump2MainPage(HostMainTab.CHARACTER)
        }
        // 解锁会员
        vSettingUnlockVipLayout.click {
            FlutterBridgeManager.jump2VipStore()
        }

        setupViewModel()
    }

    /**
     * 配置ViewModel
     */
    private fun setupViewModel() {
        KeyboardHolder.getKeyboardService()?.run {
            // 监听键盘模式切换
            getKeyboardViewModel().helpMode.observe(getLifecycleOwner()) {
                // 如果自己已显示，则隐藏自己
                if (getKeyboardViewModel().settingPageShowing.value == true) {
                    hide()
                }
            }
        }
    }

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

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

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

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