package com.service_intaleq

import android.app.Application
import android.content.Intent
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.plugin.common.MethodChannel

class MyApplication : Application() {
    companion object {
        lateinit var instance: MyApplication
            private set

        val flutterEngine: FlutterEngine by lazy {
            FlutterEngine(instance).apply {
                dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
            }
        }
    }

    override fun onCreate() {
        super.onCreate()
        instance = this

        MethodChannel(
                        flutterEngine.dartExecutor.binaryMessenger,
                        "com.service_intaleq/app_lifecycle"
                )
                .setMethodCallHandler { call, result ->
                    if (call.method == "bringAppToForeground") {
                        bringAppToForeground()
                        result.success(null)
                    } else {
                        result.notImplemented()
                    }
                }
    }

    private fun bringAppToForeground() {
        val intent = Intent(applicationContext, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
        startActivity(intent)
    }
}
