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

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import com.atmob.keyboard_android.R
import com.atmob.keyboard_android.component.base.BaseUIComponent
import com.atmob.keyboard_android.component.child.IPasteBarComponent
import com.atmob.keyboard_android.enums.HelpMode
import com.atmob.keyboard_android.ext.click
import com.atmob.keyboard_android.ext.setGone
import com.atmob.keyboard_android.ext.setVisible
import com.atmob.keyboard_android.util.KeyboardHolder

/**
 * 粘贴内容栏
 */
class PasteBarComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent<IPasteBarComponent>(context, attrs, defStyleAttr), IPasteBarComponent {
    private lateinit var vTipSymbol: View
    private lateinit var vTip: TextView
    private lateinit var vClear: View

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

    override fun findView(view: View) {
        vTipSymbol = view.findViewById(R.id.tip_symbol)
        vTip = view.findViewById(R.id.tip)
        vClear = view.findViewById(R.id.clear)
    }

    override fun bindView(view: View) {
        vClear.click {
            // 清空用户粘贴的内容
            KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()?.updateUserClipboardData("")
        }
        setData()
    }

    private fun setData() {
        KeyboardHolder.getKeyboardService()?.run {
            // 监听用户的剪切板复制内容
            getKeyboardViewModel().userClipboardData.observe(getLifecycleOwner()) { userClipboardData ->
                render(userClipboardData)
            }
            // 监听帮助模式切换
            getKeyboardViewModel().helpMode.observe(getLifecycleOwner()) {
                val userClipboardData = getKeyboardViewModel().userClipboardData.value ?: ""
                if (userClipboardData.isBlank()) {
                    updatePasteTip()
                }
            }
        }
    }

    /**
     * 渲染
     *
     * @param userClipboardData 用户的剪切板数据
     */
    private fun render(userClipboardData: String) {
        // 没有复制内容，显示前面的标识图标和提示文案，隐藏清除按钮
        if (userClipboardData.isBlank()) {
            vTipSymbol.setVisible()
            updatePasteTip()
            vClear.setGone()
        } else {
            // 有复制内容，显示用户复制的内容和清除内容图标，隐藏前面的标识图标
            vTipSymbol.setGone()
            vTip.text = userClipboardData
            vClear.setVisible()
        }
    }

    /**
     * 根据帮助模式，更新提示文案
     */
    private fun updatePasteTip() {
        val helpMode = KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()?.helpMode?.value
        if (HelpMode.HELP_CHAT == helpMode) {
            // 帮聊
            vTip.text = context.resources.getString(R.string.paste_tip)
        } else if (HelpMode.TEACH_YOU_SAY == helpMode) {
            // 教你说
            vTip.text = context.resources.getString(R.string.paste_tip2)
        }
    }

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