package com.orange.widget.ui

import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.LinearLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.orange.tool.RouterPath
import com.orange.widget.R
import com.orange.widget.app.App
import com.orange.widget.databinding.ActivityVideoBinding
import com.orange.widget.vm.VideoViewModel
import xyz.doikki.videocontroller.StandardVideoController
import xyz.doikki.videocontroller.component.*
import xyz.doikki.videoplayer.player.VideoView
import xyz.doikki.videoplayer.util.PlayerUtils

@Route(path = RouterPath.WIDGET_VIDEO)
class VideoActivity :IVideoActivity<VideoViewModel, ActivityVideoBinding>(){
    override lateinit var videoView: VideoView

    companion object{
        fun start(context: Context){
            context.startActivity(Intent(context,VideoActivity::class.java))
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_video

    override fun createLooker() {
    }

    override fun initDo(){
        videoView = iDataBinding.player
    }

    override fun initUi(){
        initPlayer()
    }

    override fun initData() {

    }

    override fun initEvent() {

    }

    private fun initPlayer(){
        iDataBinding.player.run {
//            setUrl("http://upload-minio.dev.jinr.com/longvideo/2021/0112/102032/index.m3u8")
//            val videoController :StandardVideoController = StandardVideoController(activity)
//            videoController.addDefaultControlComponent("hello",true)
//            setVideoController(videoController)
//            start()

            startFullScreen()
            setUrl("http://upload-minio.dev.jinr.com/longvideo/2021/0112/102032/index.m3u8")
            val videoController :StandardVideoController = StandardVideoController(activity)
            videoController.run {
                addControlComponent(CompleteView(activity))
                addControlComponent(ErrorView(activity))
                addControlComponent(PrepareView(activity))

                val titleView = TitleView(activity)
                titleView.findViewById<View>(App.getApp().resources.getIdentifier("back","id",App.getApp().packageName))
                    .setOnClickListener {
                        finish()
                    }
                titleView.setTitle("hello")
                addControlComponent(titleView)

                val vodControlView = VodControlView(activity)
                vodControlView.findViewById<View>(App.getApp().resources.getIdentifier("fullscreen","id",App.getApp().packageName))
                    .visibility = View.GONE
                val lp =
                    vodControlView.findViewById<View>(App.getApp().resources.getIdentifier("total_time","id",App.getApp().packageName))
                        .layoutParams as LinearLayout.LayoutParams
                lp.rightMargin = PlayerUtils.dp2px(activity, 16f)
                addControlComponent(vodControlView)

                addControlComponent(GestureView(activity))
            }
            setVideoController(videoController)
            setScreenScaleType(VideoView.SCREEN_SCALE_16_9)
            start()
        }
    }




}