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 RemoteVideoFactory : PlatformViewFactory(StandardMessageCodec.INSTANCE) {

    companion object {
        private lateinit var view: RemoteVideoPlatformView

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

            return view
        }
    }

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

class RemoteVideoPlatformView(context: Context) : PlatformView {
    private val videoContainer: RemoteVideoContainer = RemoteVideoContainer(context)

    val container get() = videoContainer.subscriberContainer

    override fun getView(): View {
        return videoContainer
    }

    override fun dispose() {}
}

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

    var subscriberContainer: FrameLayout private set

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.remote_video, this, true)
        subscriberContainer = view.findViewById(R.id.subscriber_container)
    }
}