package com.humand.demo.modules.CallService

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewManagerDelegate
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.viewmanagers.CallVideoViewManagerDelegate
import com.facebook.react.viewmanagers.CallVideoViewManagerInterface

/**
 * React Native view manager for CallVideoView.
 */
@ReactModule(name = CallVideoViewManager.REACT_CLASS)
class CallVideoViewManager(
    private val reactContext: ReactApplicationContext
) : SimpleViewManager<CallVideoView>(), CallVideoViewManagerInterface<CallVideoView> {

    companion object {
        const val REACT_CLASS = "CallVideoView"
    }

    private val delegate: ViewManagerDelegate<CallVideoView> =
        CallVideoViewManagerDelegate(this)

    override fun getDelegate(): ViewManagerDelegate<CallVideoView> = delegate

    override fun getName(): String = REACT_CLASS

    override fun createViewInstance(reactContext: ThemedReactContext): CallVideoView {
        return CallVideoView(reactContext)
    }

    /**
     * Set the session ID of the participant to render.
     */
    @ReactProp(name = "sessionId")
    override fun setSessionId(view: CallVideoView, value: String?) {
        view.sessionId = value ?: ""
    }

    /**
     * Set the track type to render (videoTrack or screenShareTrack).
     */
    @ReactProp(name = "trackType")
    override fun setTrackType(view: CallVideoView, value: String?) {
        view.trackType = value ?: CallConstants.TrackType.VIDEO
    }

    /**
     * Set whether the video should be mirrored.
     */
    @ReactProp(name = "isMirrored")
    override fun setIsMirrored(view: CallVideoView, value: Boolean) {
        view.isMirrored = value
    }

    /**
     * Set the scaling mode (fit or fill).
     */
    @ReactProp(name = "scalingMode")
    override fun setScalingMode(view: CallVideoView, value: String?) {
        view.scalingMode = value ?: CallConstants.ScalingMode.FILL
    }
}
