package com.atmob.keyboard_android.util

import android.content.Intent
import com.atmob.keyboard_android.constant.FlutterHostConstants
import com.atmob.keyboard_android.util.activity.ActivityProvider
import com.atmob.keyboard_android.util.jump.JumpHostAppPageUtil
import com.blankj.utilcode.util.IntentUtils
import com.blankj.utilcode.util.Utils
import java.io.Serializable

/**
 * Flutter页面跳转工具类，插件调起某个Flutter页面
 */
class FlutterPageLaunchUtil private constructor() {
    companion object {
        /**
         * 只让Flutter进行页面跳转。不涉及原生部分
         */
        fun onlyFlutterJumpPage(
            path: String,
            args: Map<String, Serializable?> = mapOf<String, Serializable>(),
            offAll: Boolean = false
        ) {
            val flutterEngine = FlutterEngineHolder.getFlutterEngine()
            if (flutterEngine != null) {
                // 构建参数
                val params = buildParams(path, args, offAll)
                // 再通知Flutter进行页面跳转
                JumpHostAppPageUtil.jumpAppPage(params, flutterEngine)
            }
        }

        /**
         * 调起Flutter的某个页面
         *
         * @param path Flutter的页面路由
         * @param args 页面跳转参数
         * @param offAll 是否跳转到新页面后，并清除上层的所有页面
         */
        fun jumpFlutterPage(
            path: String,
            args: Map<String, Serializable?> = mapOf<String, Serializable>(),
            offAll: Boolean = false
        ) {
            val context = ContextUtil.getContext()
            val flutterEngine = FlutterEngineHolder.getFlutterEngine()

            // 构建参数
            val params = buildParams(path, args, offAll)

            // 如果Activity存在，直接通过MethodChannel跳转Flutter页面
            val currentActivity = ActivityProvider.get().currentActivity
            if (currentActivity != null && flutterEngine != null) {
                // 如果App在后台，则将App移动到前台
                if (AppMonitor.get().isAppBackground) {
                    ActivityUtil.moveToFront(context)
                }
                // 再通知Flutter进行页面跳转
                JumpHostAppPageUtil.jumpAppPage(params, flutterEngine)
            } else {
                // 如果没有Activity，则通过原生Activity跳转
                val intent = buildIntent(params, offAll)
                Utils.getApp().startActivity(intent)
            }
        }

        /**
         * 构建跳转参数
         */
        private fun buildParams(
            path: String,
            args: Map<String, Serializable?>,
            offAll: Boolean
        ): Map<String, Serializable?> {
            val params = mutableMapOf<String, Serializable?>()
            // 添加跳转来源
            params.put(
                FlutterHostConstants.KEY_JUMP_SOURCE,
                FlutterHostConstants.JUMP_SOURCE_ANDROID
            )
            // 添加Flutter路由路径
            params.put(FlutterHostConstants.KEY_PATH, path)
            // 添加跳转参数
            params.put(FlutterHostConstants.KEY_ARGS, JsonUtil.toJson(args))
            // 是否跳转到新页面后，并清除上层的所有页面
            params.put(FlutterHostConstants.OFF_ALL, offAll)
            return params
        }

        /**
         * 构建Intent
         */
        private fun buildIntent(
            params: Map<String, Serializable?> = mapOf<String, Serializable>(),
            offAll: Boolean = false
        ): Intent {
            val intent: Intent = IntentUtils.getLaunchAppIntent(Utils.getApp().packageName)
            val currentActivity = ActivityProvider.get().currentActivity
            if (currentActivity == null) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            if (offAll) {
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            }
            for (entry in params) {
                intent.putExtra(entry.key, entry.value)
            }
            return intent
        }
    }
}