package com.adealink.weparty.profile.comp

import android.graphics.Outline
import android.view.View
import android.view.ViewOutlineProvider
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.viewmodel.viewModels
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.module.room.Room
import com.adealink.weparty.module.room.data.RoomInfo
import com.adealink.weparty.module.room.data.SpeakingRippleConfig
import com.adealink.weparty.profile.databinding.LayoutUserProfileHeaderBinding
import com.adealink.weparty.profile.databinding.LayoutUserProfileRoomOnlineBinding
import com.adealink.weparty.profile.viewmodel.ProfileRoomViewModel

class ProfileRoomStatusComp(
    lifecycleOwner: LifecycleOwner,
    private val userUid: String,
    private val headerBinding: LayoutUserProfileHeaderBinding,
    private val onlineViewContainer: View,
    private val binding: LayoutUserProfileRoomOnlineBinding,
) : BaseProfileViewComp(lifecycleOwner, userUid) {
    private val roomViewModel by viewModels<ProfileRoomViewModel>()

    private var roomInfo: RoomInfo? = null

    override fun initView() {
        initBlur()
        onlineViewContainer.gone()
        onlineViewContainer.onClick {
            goRoom()
        }
    }

    private fun initBlur() {
        headerBinding.vRoomOnlineBlurBar.setClipToOutline(true)
        headerBinding.vRoomOnlineBlurBar.outlineProvider = object : ViewOutlineProvider() {
            override fun getOutline(view: View?, outline: Outline?) {
                outline ?: return
                headerBinding.vRoomOnlineBlurBar.background?.getOutline(outline)
                outline.alpha = 1f
            }
        }
        val windowBackground = activity?.window?.decorView?.background
        headerBinding.vRoomOnlineBlurBar.setupWith(headerBinding.ivBgBlurTarget)
            .setFrameClearDrawable(windowBackground)
            .setBlurRadius(8f)
    }

    override fun observeViewModel() {
        roomViewModel.userRoomInfoLD.observe(viewLifecycleOwner) {
            if (it.first == userUid) {
                val roomInfo = it.second
                updateRoomInfo(roomInfo)
            } else {
                updateRoomInfo(null)
            }
        }
    }

    private fun updateRoomInfo(roomInfo: RoomInfo?) {
        this.roomInfo = roomInfo
        if (roomInfo == null || roomInfo.roomId.isNullOrEmpty()) {
            binding.vRipple.onStop()
            onlineViewContainer.gone()
            return
        }
        onlineViewContainer.show()
        binding.vRipple.onStart(lifecycleOwner)
        binding.ivAvatar.setImageUrl(roomInfo.roomCover)
    }

    override fun loadData() {
        super.loadData()
        roomViewModel.getUserRoomStatus(userUid)
    }

    private fun goRoom() {
        val act = (activity as? BaseActivity) ?: return
        val roomInfo = roomInfo ?: return
        Router.build(act, Room.Room.PATH)
            .putExtra(Room.Room.EXTRA_ENTER_ROOM_ID, roomInfo.roomId)
            .start()
    }

}