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.IKeyboardGuideComponent
import com.atmob.keyboard_android.ext.click
import com.atmob.keyboard_android.ext.measureView
import com.atmob.keyboard_android.model.AiKeyboardKeyModel
import com.atmob.keyboard_android.util.ViewLocationUtil
import com.blankj.utilcode.util.ConvertUtils

/**
 * 键盘引导组件实现
 */
class KeyboardGuideComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent<IKeyboardGuideComponent>(context, attrs, defStyleAttr),
    IKeyboardGuideComponent {
    private lateinit var vRoot: View
    private lateinit var vFakeTargetView: View
    private lateinit var vKeyText: TextView

    /**
     * 目标View的位置、大小信息
     */
    private var mTargetViewInfo: TargetViewInfo? = null

    /**
     * 操作回调
     */
    private var mOnActionCallback: IKeyboardGuideComponent.OnActionCallback? = null

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

    override fun findView(view: View) {
        vRoot = view.findViewById(R.id.root)
        vFakeTargetView = view.findViewById(R.id.fake_target_view)
        vKeyText = view.findViewById(R.id.key_text)
    }

    override fun bindView(view: View) {
        vRoot.click {
            // 隐藏
            hide()
            // 结束引导
            mOnActionCallback?.onFinishGuide()
        }
        render()
    }

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

    private fun render() {
        // 更新内容文字
        mTargetViewInfo?.let {
            vKeyText.text = it.dataModel.text
        }
        // 更新位置
        updateFakeTargetViewLocation()
    }

    /**
     * 更新假的目标View的位置
     */
    private fun updateFakeTargetViewLocation() {
        mTargetViewInfo?.let {
            // 设置假目标View的位置
            vFakeTargetView.translationX = it.x.toFloat()
            vFakeTargetView.translationY = it.y.toFloat()
            // 设置假目标View的大小
            vFakeTargetView.apply {
                layoutParams.apply {
                    // 不知道为什么，获取到的实际的宽度，会比显示的小一点点，高度却是正常的，所以故意加回一点宽度来处理
                    width = it.width + ConvertUtils.dp2px(6f)
                    height = it.height
                }
                requestLayout()
            }
        }
    }

    /**
     * 目标View的信息
     */
    data class TargetViewInfo(
        val x: Int,
        val y: Int,
        val width: Int,
        val height: Int,
        // 数据信息
        val dataModel: AiKeyboardKeyModel
    )

    override fun bindGuideData(
        targetView: View,
        dataModel: AiKeyboardKeyModel,
        finishCallback: IKeyboardGuideComponent.OnActionCallback
    ) {
        // View在Window中的位置
        val viewLocationInWindow = ViewLocationUtil.getViewLocationInWindow(targetView)
        // View的宽高
        val viewSize = measureView(targetView)

        // 封装数据
        mTargetViewInfo = TargetViewInfo(
            x = viewLocationInWindow[0],
            y = viewLocationInWindow[1],
            width = viewSize.first,
            height = viewSize.second,
            dataModel = dataModel
        )

        // 重新渲染
        render()
        this.mOnActionCallback = finishCallback
    }
}