package com.swago.room.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.lifecycle.findViewTreeLifecycleOwner
import com.swago.baseswago.baseroom.IRoomActiveListener
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.model.live.RoomConfig
import com.swago.room.adapter.BannerViewAdapter
import com.swago.room.databinding.ViewBannerBinding
import com.youth.banner.indicator.CircleIndicator

class BannerView : FrameLayout,IRoomActiveListener {

    private val mData by lazy {
        ArrayList<RoomConfig.BannersBean>()
    }

    var jumpToWebFun:((RoomConfig.BannersBean)->Unit)? = null

    private val bannerViewAdapter  = BannerViewAdapter(mData)

    private var binding:ViewBannerBinding? = null

    constructor(context: Context) : super(context)
    constructor(context: Context, mAttributeSet: AttributeSet?) : super(context, mAttributeSet){
        initView(context)
    }

    private fun initView(context: Context) {
        SwagoRoomManager.addListener(this)
        binding = ViewBannerBinding.inflate(LayoutInflater.from(context),this,true)
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {
        visibility = View.GONE
    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
        visibility = View.VISIBLE
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
    }

    override fun closeRoomed() {
        SwagoRoomManager.removeListener(this)
    }

    fun setData(data:ArrayList<RoomConfig.BannersBean>){
        binding?.let {
            mData.addAll(data)
            it.banner.setAdapter(bannerViewAdapter)
                .setIndicator(CircleIndicator(context))
                .addBannerLifecycleObserver(findViewTreeLifecycleOwner())
                .setOnBannerListener { data, _ ->
                    val bannerData = data as RoomConfig.BannersBean
                    jumpToWebFun?.invoke(bannerData)
                }
        }
    }

}