package com.ydl.confide.intro

import android.app.Activity
import android.content.Context
import android.net.Uri
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import androidx.recyclerview.widget.RecyclerView
import com.dou361.ijkplayer.widget.IjkVideoView
import com.ydl.confide.R
import com.ydl.confide.databinding.ItemExpertIntroBinding
import com.ydl.confide.home.util.ConfideNetworkUtil
import com.ydl.ydlcommon.view.dialog.CommonDialog

internal class IntroAdapter(
    private val context: Context,
    private val data: List<VideoViewModel>,
    private val lifecycleOwner: LifecycleOwner
) : RecyclerView.Adapter<ItemIntroHolder>(), LifecycleObserver {

    private val videoViews = hashMapOf<Int, IjkVideoView>()

    private var hasAgreePlayWithoutWiFi = false

    private var curPos = 0

    init {
        lifecycleOwner.lifecycle.addObserver(this)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemIntroHolder {
        val binding = DataBindingUtil.inflate<ItemExpertIntroBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_expert_intro,
            parent,
            false
        )
        val holder = ItemIntroHolder(binding)
        lifecycleOwner.lifecycle.addObserver(holder)
        return holder
    }

    override fun onBindViewHolder(holder: ItemIntroHolder, position: Int) {
        holder.onBind()
        val item = data[position]
        holder.binding.item = item
    }


    override fun onViewAttachedToWindow(holder: ItemIntroHolder) {
        val adapterPosition = holder.adapterPosition
        val videoView = IjkVideoView(context)
        if (hasAgreePlayWithoutWiFi || ConfideNetworkUtil.isWifi(context)) {
            videoView.setVideoURI(Uri.parse("https://video.ydlcdn.com/2020/04/01/ac2e4bb4e3ac8e2f0eca41e2d49c8484.mp4"))
        } else {
            data[adapterPosition].playUrl?.let { videoView.tag = it }
        }
        videoViews.put(adapterPosition, videoView)
        holder.onAttach(videoView)
    }

    internal fun checkNetwork() {
        if (!ConfideNetworkUtil.isWifi(context)) {
            val dialog = CommonDialog.create(context)
                .setTitle(context.getString(R.string.confide_tip))
                .setMessage(context.getString(R.string.confide_video_wifi_tip))
                .setLeftOnclick("继续播放") {
                    hasAgreePlayWithoutWiFi = true
                    for (entry in videoViews.entries) {
                        val value = entry.value
                        (value.tag as? String)?.let {
                            value.setVideoURI(Uri.parse(it))
                            if (curPos == entry.key) {
                                value.start()
                            }
                        }
                    }
                }
                .setLeftButton_color(R.color.platform_but_text_color_selected)
                .setRightButton_color(R.color.platform_text_bright_color)
                .setRightClick("取消") {
                    if (context is Activity) {
                        context.onBackPressed()
                    }
                }
                .setCancelAble(true)
            dialog.setOnCancelListener {
                if (context is Activity) {
                    context.onBackPressed()
                }
            }
            dialog.show()
        }
    }

    override fun onViewDetachedFromWindow(holder: ItemIntroHolder) {
        super.onViewDetachedFromWindow(holder)
        val adapterPosition = holder.adapterPosition
        val video = videoViews.get(adapterPosition)
        video?.release(true)
        videoViews.remove(adapterPosition)
        lifecycleOwner.lifecycle.removeObserver(holder)
        holder.onDetach()
    }

    override fun getItemCount(): Int {
        return data.size
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDestroy() {
        for (entry in videoViews.entries) {
            entry.value.release(true)
        }
    }

    fun onSelect(position: Int) {
        curPos = position
        if (!ConfideNetworkUtil.isWifi(context) && !hasAgreePlayWithoutWiFi) {
            return
        }
        for (entry in videoViews.entries) {
            if (entry.key == position) {
                entry.value.seekTo(0)
                entry.value.start()
            } else {
                entry.value.pause()
            }
        }
    }
}