package com.tutpro.baresip.plus

import android.content.Context
import android.util.AttributeSet
import android.view.Surface
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.widget.ImageView

class VideoView(val context: Context) {

    external fun set_surface(surface: Surface?)
    external fun set_surfaceSelf(surface: Surface?)
    external fun on_start()
    external fun on_resume()
    external fun on_pause()
    external fun on_stop()
    external fun get_frameFormat(): Int      // added by ritoseo

    private val surfaceHolderCallback: SurfaceHolderCallback?
    private val surfaceHolderCallbackSelf: SurfaceHolderCallbackSelf?
    val surfaceView: View
    val surfaceSelfView: View
    val standbyView: ImageView
    var afterCreate = false

    init {
        surfaceHolderCallback = SurfaceHolderCallback()
        surfaceHolderCallbackSelf = SurfaceHolderCallbackSelf()

        surfaceSelfView = View(surfaceHolderCallbackSelf, context)
        surfaceView = View(surfaceHolderCallback, context)
        standbyView = ImageView(context)
        standbyView.setImageResource(R.drawable.osvc_splash)
    }

    inner class View: SurfaceView {
        constructor(callback: SurfaceHolder.Callback, context: Context) : super(context) {
            holder.addCallback(callback)
        }

        constructor(callback: SurfaceHolder.Callback, context: Context, attrs: AttributeSet) :
                super(context, attrs) {
            holder.addCallback(callback)
        }

        constructor(callback: SurfaceHolder.Callback, context: Context, attrs: AttributeSet, defStyle: Int) :
                super(context, attrs, defStyle) {
            holder.addCallback(callback)
        }

        constructor(callback: SurfaceHolder.Callback, context: Context, attrs: AttributeSet, defStyle: Int, defStyleRes: Int) :
                super(context, attrs, defStyle, defStyleRes) {
            holder.addCallback(callback)
        }
    }

    inner class SurfaceHolderCallback: SurfaceHolder.Callback {

        override fun surfaceCreated(holder: SurfaceHolder) {
            Log.d(TAG, "Surface created")
            afterCreate = true
        }

        override fun surfaceChanged(holder: SurfaceHolder, format: Int, w: Int, h: Int) {
            Log.d(TAG, "Surface changed")
            set_surface(holder.surface)
            if (afterCreate) {
                for (call in Call.calls())
                    if (call.hasVideo()) {
                        if (call.startVideoDisplay() != 0)
                            Log.e(TAG, "Failed to start video display")
                        break
                    }

                /* Added by ritoseo */
//                surfaceView.setScaleX(1920f / 1280f)
//                surfaceView.setScaleY(1080f / 720f)
                /********************/
                afterCreate = false
            }
        }

        override fun surfaceDestroyed(holder: SurfaceHolder) {
            Log.d(TAG, "Surface destroyed")
            for (call in Call.calls())
                if (call.hasVideo())
                    call.stopVideoDisplay()
            set_surface(null)
        }

    }


    inner class SurfaceHolderCallbackSelf: SurfaceHolder.Callback {

        override fun surfaceCreated(holder: SurfaceHolder) {
            Log.w(TAG, "SurfaceSelf created")
        }

        override fun surfaceChanged(holder: SurfaceHolder, format: Int, w: Int, h: Int) {
            Log.w(TAG, "SurfaceSelf changed")
            set_surfaceSelf(holder.surface)
        }

        override fun surfaceDestroyed(holder: SurfaceHolder) {
            Log.w(TAG, "SurfaceSelf destroyed")
            set_surfaceSelf(null)
        }

    }
}
