package com.jld.vod.view

import android.media.MediaPlayer
import android.os.CountDownTimer
import android.util.Log
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.widget.RelativeLayout
import cn.jzvd.Jzvd
import com.bumptech.glide.Glide
import com.jld.vod.R
import com.jld.vod.base.BaseActivity
import com.jld.vod.config.Config
import com.jld.vod.utils.PopupUtils
import com.jld.vod.view.widget.MyJzvdStd
import kotlinx.android.synthetic.main.activity_play_video.*


/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2020/7/13 16:44
 */
class VideoPlayActivity : BaseActivity() {
    private var jzvdStd:MyJzvdStd ?= null
    private var flagAdsComplete:Boolean = false

    override fun getLayoutId(): Int {
        return R.layout.activity_play_video
    }

    override fun initView() {
        super.initView()
        flagAdsComplete  = false//广告未播放完成
//        val intent= intent
//        val movieFileUrl = intent.getStringExtra("movieFileUrl")
//        val movieName = intent.getStringExtra("movieName")
//        val movieImgUrl = intent.getStringExtra("movieImgUrl")
//
//        val jzvdStd = findViewById(R.id.jz_video) as MyJzvdStd
//        jzvdStd.setUp(Config.ApiBaseUrl+"/"+movieFileUrl, movieName)
//
//        jzvdStd.setScreenFullscreen()//全屏播放
//        jzvdStd.startButton.performClick()//自动播放
//
//        Glide.with(this).load(movieImgUrl).into(jzvdStd.posterImageView)
        jzvdStd = findViewById(R.id.jz_video) as MyJzvdStd
        playVideo()
        //返回按钮
        jzvdStd!!.backButton.setOnClickListener {
            finish()
        }
        //返回
        jzvdStd!!.iv_back_page!!.setOnClickListener {
            finish()
        }

        //回到主界面
        jzvdStd!!.iv_home!!.setOnClickListener {
            go<HomeActivity>()
            finish()
        }

        //音量调节
        jzvdStd!!.iv_volume!!.setOnClickListener {
            PopupUtils.popupUtilsVolume(this,jzvdStd!!.iv_volume!!)
        }
        //背光设置
        jzvdStd!!.iv_brightness!!.setOnClickListener {
            PopupUtils.popupAppUtilsbrightness(this,jzvdStd!!.iv_brightness!!)
        }
        //待机
        jzvdStd!!.iv_power!!.setOnClickListener {
            jzvdStd!!.fl_bg!!.visibility = View.VISIBLE
            this.onPause()
        }
        jzvdStd!!.iv_fl_power!!.setOnClickListener {
            jzvdStd!!.fl_bg!!.visibility = View.INVISIBLE
            playVideo()
        }

        //解决穿透问题
        jzvdStd!!.fl_bg!!.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                return true
            }
        })
        vd_ad.setOnTouchListener(object :View.OnTouchListener{
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                return true
            }
        })

        //videoview全屏拉伸
        val layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT)
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP)
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
        vd_ad.setLayoutParams(layoutParams)

        val videoPath = "http://vfx.mtime.cn/Video/2019/07/12/mp4/190712140656051701.mp4";
        //设置播放地址，网络视频同样使用此方法，将网址链接放入即可
        vd_ad.setVideoPath(videoPath);
        vd_ad.start()
        //循环播放
        vd_ad.setOnPreparedListener(MediaPlayer.OnPreparedListener { mp ->
            mp.start()
            mp.isLooping = true
        })


        /** 倒计时100秒，一次1秒 */
        val timer = object : CountDownTimer(100 * 1000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                val s = millisUntilFinished / 1000
                auto_ms_text.setText(s.toString() + "")
            }

            override fun onFinish() {
                vd_ad.stopPlayback()
                auto_ms_text.setText("0")
                fl_bg_ad.visibility = View.GONE
                jzvdStd!!.startButton.performClick()//自动播放电影
                flagAdsComplete  = true//广告播放完成
            }
        }.start()

//        val layoutParams = jzvdStd.backButton.getLayoutParams()
//        layoutParams.width = 72
//        layoutParams.height = 48
//        jzvdStd.backButton.setLayoutParams(layoutParams)
//
//        val layoutParamss = jzvdStd.titleTextView.getLayoutParams()
//        layoutParamss.height = 22
//        jzvdStd.titleTextView.setLayoutParams(layoutParamss)

    }
    private fun playVideo() {
        val movieFileUrl = intent.getStringExtra("movieFileUrl")
        val movieName = intent.getStringExtra("movieName")
        val movieImgUrl = intent.getStringExtra("movieImgUrl")
        jzvdStd!!.setUp(Config.ApiBaseUrl+"/"+movieFileUrl, movieName)

        jzvdStd!!.setScreenFullscreen()//全屏播放

       // jzvdStd!!.startButton.performClick()//自动播放

        Glide.with(this).load(movieImgUrl).into(jzvdStd!!.posterImageView)
    }
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        if (flagAdsComplete)
        {
           if (hasFocus)
           {
                jzvdStd!!.startButton.performClick()//自动播放
           }else{
                jzvdStd!!.startButton.performClick()//自动播放
           }
        }
        Log.e("zhaofuxin", "onWindowFocusChanged: activity焦点变化=="+hasFocus );
        super.onWindowFocusChanged(hasFocus )
    }
    override fun onBackPressed() {
        if(Jzvd.backPress()) { return }
        super.onBackPressed()
    }

    override fun onPause() {
        super.onPause()

        Jzvd.releaseAllVideos()
    }

    override fun onDestroy() {
        super.onDestroy()
        if(Jzvd.backPress()) { return }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK && event.repeatCount == 0) {
            true
        } else super.onKeyDown(keyCode, event)
    }

}