package com.ejada.hmg.opentok

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.ejada.hmg.R
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory

class LocalVideoFactory : PlatformViewFactory(StandardMessageCodec.INSTANCE) {

    companion object {
        private lateinit var view: LocalVideoPlatformView

        fun getViewInstance(context: Context): LocalVideoPlatformView {
            if(!this::view.isInitialized) {
                view = LocalVideoPlatformView(context)
            }

            return view
        }
    }

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

class LocalVideoPlatformView(context: Context) : PlatformView {
    private val videoContainer: LocalVideoContainer = LocalVideoContainer(context)

    val container get() = videoContainer.publisherContainer

    override fun getView(): View {
        return videoContainer
    }

    override fun dispose() {}
}

class LocalVideoContainer @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyle, defStyleRes) {

    var publisherContainer: FrameLayout private set

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.local_video, this, true)
        publisherContainer = view.findViewById(R.id.publisher_container)
    }
}