package com.ez.ertc.meeting.widget

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.ez.basertc.RTCConstant
import com.ez.ertc.meeting.R
import kotlinx.android.synthetic.main.ys_mt_dialog_video_param_select.*

import kotlin.collections.ArrayList

class VideoParamDialog(val mContext: Context) : Dialog(mContext) {

    private val mData: ArrayList<RTCConstant.ERTCVideoResolution> = ArrayList()
    private var adapter: ParamRecyclerViewAdapter? = null


    var itemClickListener: ParamRecyclerViewAdapter.OnItemClickListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.ys_mt_dialog_video_param_select)

        mData.addAll(RTCConstant.ERTCVideoResolution.values())

        recyclerView.adapter = ParamRecyclerViewAdapter(mData).also {
            adapter = it
            adapter?.itemClickListener = itemClickListener
        }
    }


    class ParamRecyclerViewAdapter(val mItemList: List<RTCConstant.ERTCVideoResolution>) : RecyclerView.Adapter<ParamRecyclerViewAdapter.ViewHolder>() {

        interface OnItemClickListener {
            fun selectParam(param: RTCConstant.ERTCVideoResolution)
        }

        var itemClickListener: OnItemClickListener ? = null

        class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var tvResolution: TextView = itemView.findViewById(R.id.tvResolution) as TextView
            var tvBitrate: TextView = itemView.findViewById(R.id.tvBitrate) as TextView
            var itemLaout: LinearLayout = itemView.findViewById(R.id.itemLaout) as LinearLayout
        }

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

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val item: RTCConstant.ERTCVideoResolution = mItemList[position]
            holder.tvResolution.text =  holder.itemView.context.getString(R.string.ys_mt_room_dialog_monitor_resolution)+ ":" + item.width + "x" + item.height
            holder.tvBitrate.text = holder.itemView.context.getString(R.string.ys_mt_room_dialog_monitor_bitrate) + ":" + item.recommendBitrate/1024
            holder.itemView.setOnClickListener {
                itemClickListener?.selectParam(item)
            }
        }

        override fun getItemCount(): Int = mItemList.size
    }
}