package com.eventtan.app.penguin_flutter_sample.penguin

import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import com.eventtan.test.penguin_flutter_sample.MainActivity
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory

// PenguinViewFactory.kt

/**
 * A factory class for creating instances of [PenguinView].
 *
 * @property messenger The [BinaryMessenger] used for communication.
 * @property mainActivity The [MainActivity] instance associated with this view.
 */
class PenguinViewFactory(
    private val messenger: BinaryMessenger,
    private val mainActivity: MainActivity
) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {

    /**
     * Creates a new [PenguinView] instance.
     *
     * @param context The [Context] in which the view is running.
     * @param viewId The unique ID of the view to be created.
     * @param args Optional arguments passed for creating the view, expected to be a [Map].
     * @return A new instance of [PenguinView].
     */
    @RequiresApi(Build.VERSION_CODES.O)
    override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
        // Cast the arguments to a Map if they are provided
        val creationParams = args as Map<String, String>

        // Instantiate and return the PenguinView with the provided parameters
        return PenguinView(context, viewId, creationParams, messenger, mainActivity)
    }
}