package com.tencent.liteav.demo.player.ui

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.android.cast.dlna.dmc.OnDeviceRegistryListener
import com.tencent.liteav.demo.player.R
import com.tencent.liteav.demo.superplayer.util.CastorUtil
import org.fourthline.cling.model.meta.Device

class PlayerCastAdapter(
    val listener: OnItemSelectedListener? = null,
) : RecyclerView.Adapter<PlayerCastAdapter.ViewHolder>(), OnDeviceRegistryListener {
    interface OnItemSelectedListener {
        fun onItemSelected(castDevice: Device<*, *, *>?, selected: Boolean)
    }

    private val handler = Handler(Looper.getMainLooper())
    private val deviceList: MutableList<Device<*, *, *>> = ArrayList()

    val castDevice: Device<*, *, *>? get() = CastorUtil.castDevice

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_cast, parent, false)
//        val view = View.inflate(parent.context, R.layout.item_cast, null)
        return ViewHolder(view, listener)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.setDevice(getItem(position), isSelected(position))
    }

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

    private fun getItem(position: Int): Device<*, *, *>? {
        return deviceList[position]
    }

    private fun isSelected(position: Int): Boolean {
        val device = getItem(position)
        return if (device != null && castDevice != null) {
            device.identity.udn.identifierString == castDevice!!.identity.udn.identifierString
        } else false
    }

    override fun onDeviceAdded(device: Device<*, *, *>) {
        if (!deviceList.contains(device)) {
            deviceList.add(device)
            if (Thread.currentThread() !== Looper.getMainLooper().thread) {
                handler.post { notifyDataSetChanged() }
            } else {
                notifyDataSetChanged()
            }
        }
    }

    override fun onDeviceUpdated(device: Device<*, *, *>?) {}

    override fun onDeviceRemoved(device: Device<*, *, *>) {
        if (deviceList.contains(device)) {
            deviceList.remove(device)
            if (Thread.currentThread() !== Looper.getMainLooper().thread) {
                handler.post { notifyDataSetChanged() }
            } else {
                notifyDataSetChanged()
            }
        }
    }

    inner class ViewHolder(
        itemView: View,
        private val itemSelectedListener: OnItemSelectedListener?,
    ) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
        private val textDevice: TextView
        private val isSelect: ImageView

        private var device: Device<*, *, *>? = null

        fun setDevice(device: Device<*, *, *>?, isSelected: Boolean) {
            this.device = device
            textDevice.text = device?.details?.friendlyName
            isSelect.setImageResource(
                if (isSelected) R.mipmap.icon_check else R.mipmap.icon_uncheck
            )
        }

        init {
            textDevice = itemView.findViewById(R.id.device_name)
            isSelect = itemView.findViewById(R.id.device_select)
            itemView.setOnClickListener(this)
        }

        override fun onClick(v: View?) {
            device?.let {
                itemSelectedListener?.onItemSelected(it, true)
            }
        }
    }
}