package com.atmob.keyboard_android.ui.popup.guide

import android.content.Context
import android.view.View
import android.view.Window
import android.widget.TextView
import com.atmob.keyboard_android.R
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.ui.popup.base.BasePopupDialog
import com.atmob.keyboard_android.util.ViewLocationUtil
import com.blankj.utilcode.util.ConvertUtils

/**
 * 键盘引导弹窗
 */
class KeyboardGuideDialog(context: Context, hostWindow: Window) :
    BasePopupDialog(context, hostWindow) {
    private lateinit var vRoot: View
    private lateinit var vFakeTargetView: View
    private lateinit var vKeyText: TextView

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

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

    override fun onInflaterViewId(): Int {
        return R.layout.layout_keyboard_guide
    }

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

    override fun onBindView(view: View) {
        super.onBindView(view)
        vRoot.click {
            // 关闭弹窗
            dismiss()
            // 结束引导
            mOnActionCallback?.onFinishGuide()
        }
        render()
    }

    override fun isCanOutsideTouchable(): Boolean {
        // 禁止点击外部区域关闭弹窗，自定义点击事件来实现关闭
        return false
    }

    override fun onSetupDialogFrameSize(
        context: Context,
        windowWidth: Int,
        windowHeight: Int
    ): IntArray {
        return intArrayOf(
            windowWidth,
            // 键盘的高度
            context.resources.getDimensionPixelSize(R.dimen.keyboard_height)
        )
    }

    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
    )

    interface OnActionCallback {
        /**
         * 结束引导时回调
         */
        fun onFinishGuide()
    }

    /**
     * 绑定目标View
     */
    fun bindTargetView(targetView: View, dataModel: AiKeyboardKeyModel): KeyboardGuideDialog {
        // 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()

        return this
    }

    /**
     * 绑定操作回调
     */
    fun bindOnActionCallback(callback: OnActionCallback): KeyboardGuideDialog {
        this.mOnActionCallback = callback
        return this
    }
}