package com.atmob.keyboard_android.util.jump

import android.content.Intent
import com.atmob.keyboard_android.constant.FlutterHostConstants
import com.atmob.keyboard_android.constant.PluginConfig
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

/**
 * Flutter页面跳转工具类
 */
class JumpHostAppPageUtil private constructor() {
    companion object {
        /**
         * Flutter的路由跳转方法
         */
        private const val FLUTTER_JUMP_APP_PAGE_METHOD = "jumpAppPage"

        /**
         * 跳转到Flutter页面
         */
        fun jumpAppPage(intent: Intent, engine: FlutterEngine) {
            jumpAppPage(IntentUtil.intentToMap(intent), engine)
        }

        /**
         * 跳转到Flutter页面
         */
        fun jumpAppPage(params: Map<String, Any?>, engine: FlutterEngine) {
            // 忽略非自己定义的页面跳转
            val jumpSource = params[FlutterHostConstants.KEY_JUMP_SOURCE]?.toString() ?: ""
            if (jumpSource.isBlank()) {
                return
            }

            val methodChannel = MethodChannel(
                engine.dartExecutor.binaryMessenger,
                PluginConfig.FLUTTER_METHOD_CHANNEL_NAME
            )
            methodChannel.invokeMethod(FLUTTER_JUMP_APP_PAGE_METHOD, params)
        }
    }
}