package com.adealink.weparty.room.member.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.fastLazy
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseViewComponent
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.module.im.IM
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.FollowStatus
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.room.databinding.LayoutRoomMemberBottomBinding
import com.adealink.weparty.room.gift.RoomGiftPanelDialog
import com.adealink.weparty.R as APP_R

class MemberBottomComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutRoomMemberBottomBinding,
    val opUid: String,
    val isSelf: Boolean,
) : BaseViewComponent(lifecycleOwner) {

    private val profileViewModel by fastLazy {
        ProfileModule.getProfileViewModel(viewModelStoreOwner)
    }

    private val followViewModel by fastLazy { ProfileModule.getFollowViewModel(viewModelStoreOwner) }
    private var isFollow = false

    override fun initViews() {
        super.initViews()
        if (isSelf) {
            binding.root.gone()
            return
        }
        binding.root.show()
        binding.btnFollow.onClick {
            if (isFollow) {
                unFollow()
            } else {
                follow()
            }
        }
        binding.btnChat.onClick {
            clickChat()
        }
        binding.btnSendGift.onClick {
            clickGift()
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        if (!isSelf) {
            followViewModel?.isFollowLD?.observe(viewLifecycleOwner) {
                updateFollow(it[opUid] ?: FollowStatus.NONE)
            }
        }
        profileViewModel?.userInfoLD?.observe(viewLifecycleOwner) {
            updateBottom(it)
        }
    }

    override fun loadData() {
        super.loadData()
        if (!isSelf) {
            followViewModel?.isFollow(opUid)
        }
    }

    private fun updateBottom(userInfo: UserInfo?) {

    }

    private fun follow() {
        followViewModel?.follow(opUid)?.observe(viewLifecycleOwner) {
            showFailedToast(it)
        }
    }

    private fun unFollow() {
        followViewModel?.unFollow(opUid)?.observe(viewLifecycleOwner) {
            showFailedToast(it)
        }
    }

    private fun updateFollow(followStatus: FollowStatus) {
        when (followStatus) {
            FollowStatus.NONE,
            FollowStatus.FANS -> {
                //未关注,对方关注了你
                isFollow = false
                binding.btnFollow.setTextColor(getCompatColor(APP_R.color.white))
                binding.btnFollow.text = getCompatString(APP_R.string.common_follow)
            }

            FollowStatus.EACH_FOLLOW,
            FollowStatus.FOLLOW -> {
                //互关,已经关注对方
                isFollow = true
                binding.btnFollow.setTextColor(getCompatColor(APP_R.color.color_33FFFFFF))
                binding.btnFollow.text = getCompatString(APP_R.string.common_unfollow)
            }
        }
    }

    private fun clickChat() {
        val act = activity ?: return
        Router.build(act, IM.Session.PATH)
            .putExtra(IM.Session.EXTRA_CHAT_ID, opUid)
            .start()
    }

    private fun clickGift() {
        RoomGiftPanelDialog().apply {
            setTargetUid(opUid)
        }.show(fragmentManager)
    }

}