// Autogenerated from Pigeon (v11.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package org.moxxy.moxxyv2.quirks

import android.util.Log
import io.flutter.plugin.common.BasicMessageChannel
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MessageCodec
import io.flutter.plugin.common.StandardMessageCodec
import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer

private fun wrapResult(result: Any?): List<Any?> {
  return listOf(result)
}

private fun wrapError(exception: Throwable): List<Any?> {
  if (exception is FlutterError) {
    return listOf(
      exception.code,
      exception.message,
      exception.details
    )
  } else {
    return listOf(
      exception.javaClass.simpleName,
      exception.toString(),
      "Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception)
    )
  }
}

/**
 * Error class for passing custom error details to Flutter via a thrown PlatformException.
 * @property code The error code.
 * @property message The error message.
 * @property details The error details. Must be a datatype supported by the api codec.
 */
class FlutterError (
  val code: String,
  override val message: String? = null,
  val details: Any? = null
) : Throwable()

enum class QuirkNotificationEventType(val raw: Int) {
  MARKASREAD(0),
  REPLY(1),
  OPEN(2);

  companion object {
    fun ofRaw(raw: Int): QuirkNotificationEventType? {
      return values().firstOrNull { it.raw == raw }
    }
  }
}

/** Generated class from Pigeon that represents data sent in messages. */
data class QuirkNotificationEvent (
  /** The notification id. */
  val id: Long,
  /** The JID the notification was for. */
  val jid: String,
  /** The type of event. */
  val type: QuirkNotificationEventType,
  /**
   * An optional payload.
   * - type == NotificationType.reply: The reply message text.
   * Otherwise: undefined.
   */
  val payload: String? = null,
  /** Extra data. Only set when type == NotificationType.reply. */
  val extra: Map<String?, String?>? = null

) {
  companion object {
    @Suppress("UNCHECKED_CAST")
    fun fromList(list: List<Any?>): QuirkNotificationEvent {
      val id = list[0].let { if (it is Int) it.toLong() else it as Long }
      val jid = list[1] as String
      val type = QuirkNotificationEventType.ofRaw(list[2] as Int)!!
      val payload = list[3] as String?
      val extra = list[4] as Map<String?, String?>?
      return QuirkNotificationEvent(id, jid, type, payload, extra)
    }
  }
  fun toList(): List<Any?> {
    return listOf<Any?>(
      id,
      jid,
      type.raw,
      payload,
      extra,
    )
  }
}
@Suppress("UNCHECKED_CAST")
private object MoxxyQuirkApiCodec : StandardMessageCodec() {
  override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
    return when (type) {
      128.toByte() -> {
        return (readValue(buffer) as? List<Any?>)?.let {
          QuirkNotificationEvent.fromList(it)
        }
      }
      else -> super.readValueOfType(type, buffer)
    }
  }
  override fun writeValue(stream: ByteArrayOutputStream, value: Any?)   {
    when (value) {
      is QuirkNotificationEvent -> {
        stream.write(128)
        writeValue(stream, value.toList())
      }
      else -> super.writeValue(stream, value)
    }
  }
}

/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
interface MoxxyQuirkApi {
  fun earlyNotificationEventQuirk(): QuirkNotificationEvent?

  companion object {
    /** The codec used by MoxxyQuirkApi. */
    val codec: MessageCodec<Any?> by lazy {
      MoxxyQuirkApiCodec
    }
    /** Sets up an instance of `MoxxyQuirkApi` to handle messages through the `binaryMessenger`. */
    @Suppress("UNCHECKED_CAST")
    fun setUp(binaryMessenger: BinaryMessenger, api: MoxxyQuirkApi?) {
      run {
        val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.moxxyv2.MoxxyQuirkApi.earlyNotificationEventQuirk", codec)
        if (api != null) {
          channel.setMessageHandler { _, reply ->
            var wrapped: List<Any?>
            try {
              wrapped = listOf<Any?>(api.earlyNotificationEventQuirk())
            } catch (exception: Throwable) {
              wrapped = wrapError(exception)
            }
            reply.reply(wrapped)
          }
        } else {
          channel.setMessageHandler(null)
        }
      }
    }
  }
}
