package com.ez.ertc.call.ui

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ez.ertc.call.bean.RemoteUser
import com.ez.ertc.call.bean.UserStatusData
import com.ez.ertc.call.bean.VideoInfoData
import com.ez.ertc.call.ui.widget.EzmTextureView
import java.util.*

class GridVideoViewContainer : RecyclerView {
    private var mGridVideoViewContainerAdapter: GridVideoViewContainerAdapter? = null
    private var listener: RecyclerItemClickListener.OnItemClickListener? = null

    constructor(context: Context?) : super(context!!) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(
        context!!, attrs
    )

    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
        context!!, attrs, defStyle
    )

    fun setItemEventHandler(listener: RecyclerItemClickListener.OnItemClickListener?) {
//        addOnItemTouchListener(RecyclerItemClickListener(context, listener))
        this.listener = listener
    }

    fun initViewContainer(
        activity: Activity,
        localUserId: String,
        uids: HashMap<String, EzmTextureView?>,
        userStatusList: ArrayList<UserStatusData>
    ) {
        initAdapter(activity, localUserId, uids, userStatusList)
        this.adapter = mGridVideoViewContainerAdapter
//        val count = uids.size
//        val itemSpanCount = getNearestSqrt(count)
        val itemSpanCount = 3
        this.layoutManager = GridLayoutManager(activity.applicationContext, itemSpanCount, VERTICAL, false)
        mGridVideoViewContainerAdapter?.notifyDataSetChanged()
    }

    private fun initAdapter(
        activity: Activity,
        localUserId: String,
        uids: HashMap<String, EzmTextureView?>,
        userStatusList: ArrayList<UserStatusData>
    ){
        if (mGridVideoViewContainerAdapter == null) {
            mGridVideoViewContainerAdapter = GridVideoViewContainerAdapter(activity, localUserId, uids, userStatusList)
            mGridVideoViewContainerAdapter!!.setHasStableIds(true)
            mGridVideoViewContainerAdapter!!.setListener(object : GridVideoViewContainerAdapter.OnClickListener{
                override fun onVideoViewClick(v: View, position: Int) {
                    listener?.onItemClick(v, position)
                }
            })
        }
    }

    fun notifyDataSetChanged(userStatusList: ArrayList<UserStatusData>) {
        mGridVideoViewContainerAdapter?.notifyDataSetChanged(userStatusList)
    }

    private fun getNearestSqrt(n: Int): Int {
        return Math.sqrt(n.toDouble()).toInt()
    }

    fun notifyUiChanged(
        uids: HashMap<String, EzmTextureView?>,
        userStatusList: ArrayList<UserStatusData>
    ) {
        mGridVideoViewContainerAdapter?.notifyUiChanged(uids, userStatusList)
    }

    fun getItem(position: Int): UserStatusData {
        return mGridVideoViewContainerAdapter!!.getItem(position)
    }

}