package com.matchlivetv.match_live_tv

import android.content.Context
import android.view.View
import android.widget.FrameLayout
import com.pedro.library.view.OpenGlView
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory

class StreamingPreviewPlatformView(
    context: Context,
    private val onPreviewReady: (OpenGlView) -> Unit,
) : PlatformView {

    private val openGlView = OpenGlView(context).apply {
        layoutParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT,
        )
    }

    init {
        StreamPreviewHolder.openGlView = openGlView
        onPreviewReady(openGlView)
    }

    override fun getView(): View = openGlView

    override fun dispose() {
        // Non azzerare il holder: la rotazione ricrea la PlatformView e il plugin riaggancia.
    }
}

class StreamingPreviewFactory(
    private val onPreviewReady: (OpenGlView) -> Unit,
) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {

    override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
        return StreamingPreviewPlatformView(context, onPreviewReady)
    }
}
