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

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.atmob.keyboard_android.R
import com.atmob.keyboard_android.component.ComponentMediator
import com.atmob.keyboard_android.component.base.BaseUIComponent
import com.atmob.keyboard_android.component.child.IToolBarComponent
import com.atmob.keyboard_android.component.root.IKeyboardRootComponent
import com.atmob.keyboard_android.enums.KeyboardGlobalType
import com.atmob.keyboard_android.ext.click
import com.atmob.keyboard_android.ext.setGone
import com.atmob.keyboard_android.keyboard.InputMethodPickerActivity
import com.atmob.keyboard_android.util.KeyboardHolder
import com.atmob.keyboard_android.util.UserInfoHelper
import com.atmob.keyboard_android.util.bridge.FlutterBridgeManager

/**
 * 工具类组件
 */
class ToolBarComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent<IToolBarComponent>(context, attrs, defStyleAttr), IToolBarComponent {
    private lateinit var vIcon: View
    private lateinit var vPinyinSwitchBtn: ImageView
    private lateinit var vKeyboardSwitchBtn: View
    private lateinit var vIntimacyLayout: View
    private lateinit var vIntimacyValue: TextView

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

    override fun findView(view: View) {
        vIcon = view.findViewById(R.id.icon)
        vPinyinSwitchBtn = view.findViewById(R.id.pinyin_switch_btn)
        vKeyboardSwitchBtn = view.findViewById(R.id.keyboard_switch_btn)
        vIntimacyLayout = view.findViewById(R.id.intimacy_layout)
        vIntimacyValue = view.findViewById(R.id.intimacy_value)
    }

    override fun bindView(view: View) {
        // TODO: hezihao，拼音键盘还没实现，先隐藏切换入口
        vPinyinSwitchBtn.setGone()

        vIcon.click {
            // 切换到设置页
            val rootComponent =
                ComponentMediator.findComponent<IKeyboardRootComponent>(IKeyboardRootComponent::class.java)
            rootComponent?.switchSettingPage()
        }
//        vIcon.longClick {
//            // 测试权限申请提示弹窗
//            // PermissionDialogUtil.showPermissionDialog(context)
//
//            // 测试Vip页面
//            val keyboardRootComponent =
//                ComponentMediator.findComponent(IKeyboardRootComponent::class.java)
//            keyboardRootComponent?.switchVipPage()
//            true
//        }
        vPinyinSwitchBtn.click {
            // 切换拼音键盘或AI键盘
            KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()?.run {
                // 当前为Ai键盘，则切换为拼音键盘
                val newGlobalType =
                    if (keyboardGlobalType.value == KeyboardGlobalType.AI_KEYBOARD) {
                        KeyboardGlobalType.PINYIN_KEYBOARD
                    } else {
                        // 当前为拼音键盘，则切换为Ai键盘
                        KeyboardGlobalType.AI_KEYBOARD
                    }
                updateKeyboardGlobalType(newGlobalType)
            }
        }
        vKeyboardSwitchBtn.click {
            // 打开系统输入法选择器
            InputMethodPickerActivity.start(context)
        }
        vIntimacyLayout.click {
            // 点击亲密度布局，跳转去亲密度页
            UserInfoHelper.checkLogin(nextAction = {
                FlutterBridgeManager.jump2IntimacyPage()
            })
        }

        setupViewModel()
    }

    @SuppressLint("SetTextI18n")
    private fun setupViewModel() {
        KeyboardHolder.getKeyboardService()?.run {
            // 监听全局键盘类型变化，切换拼音切换图标
            getKeyboardViewModel().keyboardGlobalType.observe(getLifecycleOwner()) { currentGlobalType ->
                val switchIconResId = if (currentGlobalType == KeyboardGlobalType.PINYIN_KEYBOARD) {
                    R.mipmap.ic_pinyin_btn_selected
                } else {
                    R.mipmap.ic_pinyin_btn_normal
                }
                vPinyinSwitchBtn.setImageResource(switchIconResId)
            }
            // 监听当前键盘信息，更新亲密度布局
            getKeyboardViewModel().currentKeyboardInfo.observe(getLifecycleOwner()) {
                vIntimacyValue.text = "${it.intimacy ?: 0}%"
            }
        }
    }

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