package com.adealink.weparty.profile.comp

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.aab.util.getCompatDimensionPixelSize
import com.adealink.frame.util.onClick
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.commonui.util.DragDirection
import com.adealink.weparty.commonui.util.DragViewUtil
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.ActivityUserProfileBinding
import com.adealink.weparty.profile.dialog.CharmStarDialog
import com.adealink.weparty.util.applyImmersive

class ProfilePlaymateComp(
    lifecycleOwner: LifecycleOwner,
    private val userUid: String,
    private val binding: ActivityUserProfileBinding,
) : BaseProfileViewComp(lifecycleOwner, userUid) {

    override fun initView() {
        activity?.applyImmersive { top ->
            binding.vPlaymateCharm.root.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin =
                    top + //顶部状态栏
                            getCompatDimensionPixelSize(R.dimen.profile_header_height) + //顶部内容高度
                            50.dp() + //Tab高度
                            16.dp()
            }
        }
        binding.vPlaymateCharm.root.onClick {
            showCharmStar()
        }
        DragViewUtil.registerDragAction(binding.vPlaymateCharm.root, DragDirection.VERTICAL)
    }

    override fun observeViewModel() {
        profileViewModel.userInfoLD.observe(viewLifecycleOwner) {
            updateUI(it)
        }
    }

    override fun updateUI(userInfo: UserInfo?) {
        if (userInfo == null || userUid == ProfileModule.getMyUid()) {
            binding.vPlaymateCharm.root.gone()
            return
        }
        if (!userInfo.isPlaymate() || userInfo.rated == true) {
            //仅陪玩显示
            binding.vPlaymateCharm.root.gone()
            return
        }
        binding.vPlaymateCharm.root.show()
    }

    private fun showCharmStar() {
        CharmStarDialog().show(fragmentManager, "CharmStarDialog")
    }

}