package com.ydl.confide.intro

import android.util.Log
import android.view.View
import android.widget.SeekBar
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.recyclerview.widget.RecyclerView
import com.dou361.ijkplayer.widget.IjkVideoView
import com.ydl.confide.databinding.ItemExpertIntroBinding
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import tv.danmaku.ijk.media.player.IjkMediaPlayer
import java.util.concurrent.TimeUnit

internal class ItemIntroHolder(binding: ItemExpertIntroBinding) :
    BindingViewHolder<ItemExpertIntroBinding>(binding), LifecycleObserver {
    companion object {
        private const val TAG = "ItemIntroHolder"
    }

    private var disposable: Disposable? = null
    private var video: IjkVideoView? = null

    fun onDetach() {
        stopTiming()
        video?.release(true)
    }

    fun onBind() {
        binding.btnCall.setOnClickListener { }
        binding.btnChat.setOnClickListener { }
        binding.btnEvaluate.setOnClickListener { }
        binding.btnShare.setOnClickListener { }
        binding.videoView.setOnClickListener {
            video?.pause()
            binding.ivPlay.visibility = View.VISIBLE
        }
        binding.ivPlay.setOnClickListener {
            video?.start()
            binding.ivPlay.visibility = View.GONE
        }
        binding.seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {

            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                if (seekBar != null) {
                    video?.seekTo(seekBar.progress)
                    video?.start()
                    binding.ivPlay.visibility = View.GONE
                }
            }
        })
    }

    fun onAttach(videoView: IjkVideoView) {
        this.video = videoView
        binding.videoView.addView(videoView)
        video?.setOnPreparedListener { player ->
            Log.d(TAG, "${player.videoHeight},${player.videoWidth},${player.duration}")
            val duration = player.duration.toInt()
            if (duration > 0) {
                binding.seekbar.max = duration
            } else {
                binding.seekbar.visibility = View.GONE
            }
        }
        video?.setOnInfoListener { mp, what, extra ->
            Log.d(TAG, "${what},${extra}")
            return@setOnInfoListener false
        }
        video?.setOnCompletionListener { stopTiming() }
        startTiming()
    }

    private fun startTiming() {
        disposable = Observable.interval(300, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.computation())
            .subscribe {
                val pos = (video?.mMediaPlayer as IjkMediaPlayer?)?.currentPosition ?: 0
                if (pos > 0) {
                    binding.seekbar.progress = pos.toInt()// video.bufferPercentage
                }
            }
    }

    private fun stopTiming() {
        disposable?.dispose()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    private fun onResume() {
        video?.onResume()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    private fun onPause() {
        video?.onPause()
    }

}

class VideoViewModel {
    val name = ObservableField<String>("")
    val avatar = ObservableField<String>("")
    val count = ObservableInt()
    val intro = ObservableField<String>("")
    val tag = ObservableField<String>("")
    var playUrl: String? = "https://video.ydlcdn.com/2020/04/01/ac2e4bb4e3ac8e2f0eca41e2d49c8484.mp4"
    var coverUrl: String? = null
    var isVideo = true
}


open class BindingViewHolder<T : ViewDataBinding>(val binding: T) :
    RecyclerView.ViewHolder(binding.root)