package com.dronex.rec.recording

import android.content.Context
import android.view.View
import androidx.camera.view.PreviewView
import com.dronex.rec.MainActivity
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory

class RecordingPreviewFactory(
        private val activity: MainActivity,
) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
    override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
        return RecordingPreviewPlatformView(activity)
    }
}

class RecordingPreviewPlatformView(
        private val activity: MainActivity,
) : PlatformView {
    val previewView: PreviewView =
            PreviewView(activity).apply {
                implementationMode = PreviewView.ImplementationMode.COMPATIBLE
                scaleType = PreviewView.ScaleType.FILL_CENTER
            }

    init {
        activity.attachRecordingPreview(previewView)
    }

    override fun getView(): View = previewView

    override fun dispose() {
        activity.detachRecordingPreview(previewView)
    }
}
