package com.tsystems.flutter_vosk_wakeword

import android.content.Intent
import androidx.annotation.NonNull

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

// vosk
import org.vosk.LibVosk

/** FlutterVoskWakewordPlugin */
class FlutterVoskWakewordPlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHandler {

    private lateinit var methodChannel : MethodChannel
    private lateinit var eventChannel: EventChannel
    private var eventSink: EventChannel.EventSink? = null
    private lateinit var context: android.content.Context
    private var eventReceiver: EventReceiver? = null

    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        context = flutterPluginBinding.applicationContext
        methodChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_vosk_wakeword/methods")
        methodChannel.setMethodCallHandler(this)

        eventChannel = EventChannel(flutterPluginBinding.binaryMessenger, "flutter_vosk_wakeword/events")
        eventChannel.setStreamHandler(this)
    }

    override fun onMethodCall(call: MethodCall, result: Result) {
        val callName = call.method
        if (callName == "initialize") {
//            val modelPath = call.argument<String>("modelPath")
            val wakeWords = call.argument<ArrayList<String>>("wakeWords")
            if (wakeWords == null) {
                result.error("INVALID_ARGUMENTS", "wakeWords is null", null)
                return
            }
            val intent = Intent(context, WakewordService::class.java).apply {
                action = WakewordService.ACTION_INITIALIZE
                putExtra(WakewordService.EXTRA_WAKE_WORDS, wakeWords)
            }
            context.startService(intent)
        } else if (callName == "start") {
            val intent = Intent(context, WakewordService::class.java).apply {
                action = WakewordService.ACTION_START
            }
            context.startService(intent)
            result.success(null)
        } else if (callName == "stop") {
            val intent = Intent(context, WakewordService::class.java).apply {
                action = WakewordService.ACTION_STOP
            }
            context.startService(intent)
            result.success(null)
        } else {
            result.notImplemented()
        }
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        methodChannel.setMethodCallHandler(null)
        eventChannel.setStreamHandler(null)
        context.stopService(Intent(context, WakewordService::class.java)) }


    // --- EventChannel.StreamHandler Callbacks ---
    override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
        eventSink = events
        // 注册广播接收器以从 Service 接收事件
         eventReceiver = EventReceiver { eventType, text ->
             // 将事件发送回 Dart
             eventSink?.success(mapOf("type" to eventType, "text" to text))
         }
         eventReceiver?.register(context)
    }

    override fun onCancel(arguments: Any?) {
        eventSink = null
        eventReceiver?.unregister(context)
        eventReceiver = null
    }

}
