package com.swago.room.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.swago.baseswago.R
import com.swago.baseswago.baseroom.IRoomActiveListener
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.RoleType
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.model.live.RoomUserModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.room.adapter.RoomUserAdapter
import com.swago.room.databinding.LayoutUserHeaderViewBinding
import com.swago.room.inter.IHeader

/**
 *@date 2022/3/4 14:56
 *description:
 */
class ComHeaderView  : ConstraintLayout, IRoomActiveListener, IHeader {


    lateinit var binding: LayoutUserHeaderViewBinding

    var followFun:((id:String)->Unit)? = null
    var showUserInfo:((id:String)->Unit)? = null

    private val adapter by lazy {
        RoomUserAdapter()
    }

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        initView(context)
    }

    private fun initView(context: Context) {
        SwagoRoomManager.addListener(this)
        binding = LayoutUserHeaderViewBinding.inflate(LayoutInflater.from(context), this, true)
        binding.rvUser.layoutManager = LinearLayoutManager(context,RecyclerView.HORIZONTAL,false)
        binding.rvUser.adapter = adapter

        binding.tvFollow.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                SwagoRoomManager.iRoomInfo?.let {
                    followFun?.invoke(it.getAnchorId())
                }
            }
        })

        binding.tvIncome.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance()
                    .build(ARouteConstant.Room.rankList)
                    .withString("roomId",SwagoRoomManager.iRoomInfo?.getRoomId()?:"")
                    .navigation()
            }
        })

        binding.cl.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                SwagoRoomManager.iRoomInfo?.let {
                    showUserInfo?.invoke(it.getAnchorId())
                }
            }
        })

        adapter.setOnItemClickListener { _, _, position ->
            if (adapter.data.size>position){
                showUserInfo?.invoke(adapter.data[position].user_id)
            }
        }
    }


    fun updateIncome(income:String){
        binding.tvIncome.text = income
    }

    fun updateHotValue(hotValue:String){
        binding.tvHot.text = hotValue
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {
        binding.clRoot.visibility = View.INVISIBLE
    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
        adapter.data.clear()
        adapter.notifyDataSetChanged()
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
        binding.clRoot.visibility = View.VISIBLE
        Glide.with(context).load(iRoomInfo.getRoomCover())
            .placeholder(R.mipmap.default_avatar)
            .error(R.mipmap.default_avatar).into(binding.ivAvatar)
        binding.tvName.text =iRoomInfo.getAnchorName()
        binding.tvId.text = AppContext.getContext().resources.getString(R.string.id_x).format(iRoomInfo.getAnchorCode())
        binding.tvHot.text = iRoomInfo.getRoomHot()
        binding.tvIncome.text = iRoomInfo.getRoomIncome()

        if (SwagoRoomManager.roleType == RoleType.anchor){
            binding.tvFollow.visibility = View.GONE
        }else{
            if (iRoomInfo.getIsFollow()==1){
                binding.tvFollow.visibility = View.GONE
            }else{
                binding.tvFollow.visibility = View.VISIBLE
            }
        }
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
    }

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

    override fun getHeaderView(): View {
        return this
    }

    fun updateRoomUser(data: List<RoomUserModel.ListBean>) {
        adapter.setNewData(data)
        if (data.isNotEmpty()){
            binding.rvUser.smoothScrollToPosition(0)
        }
    }

    fun setFollow(){
        binding.tvFollow.visibility = View.GONE
    }


}