package com.adealink.weparty.im.session.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
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.im.databinding.LayoutSessionTopBarBinding
import com.adealink.weparty.im.session.dialog.SessionSettingDialog
import com.adealink.weparty.module.profile.Profile
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.module.profile.data.isFollowed
import com.tencent.qcloud.tuikit.tuichat.bean.ChatInfo

class SessionTopComp(
    lifecycleOwner: LifecycleOwner,
    val topBar: LayoutSessionTopBarBinding,
    val chatInfo: ChatInfo?
) : ViewComponent(lifecycleOwner) {

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

    override fun onCreate() {
        super.onCreate()
        initView()
        observeViewModel()
        loadData()
    }

    private fun initView() {
        topBar.ivBack.onClick {
            activity?.finish()
        }
        topBar.ivAvatar.onClick {
            goProfile()
        }
        topBar.tvUserName.onClick {
            goProfile()
        }
        topBar.tvUserOnline.onClick {
            goProfile()
        }
        topBar.btnFollow.onClick {
            clickFollow()
        }
        topBar.btnSetting.onClick {
            clickSetting()
        }
    }

    private fun observeViewModel() {
        profileViewModel?.userInfoLD?.observe(viewLifecycleOwner) {
            updateUserInfo(it)
        }

        followViewModel?.isFollowLD?.observe(viewLifecycleOwner) {
            val uid = chatInfo?.id ?: return@observe
            updateFollow(it[uid]?.isFollowed() ?: false)
        }
    }

    private fun loadData() {
        val chatInfo = chatInfo ?: return
        updateUserInfo(profileViewModel?.getUserInfoBy(chatInfo.id))
    }

    override fun onResume() {
        super.onResume()
        chatInfo?.id?.let { uid ->
            profileViewModel?.pullUserInfoBy(uid, false)
            followViewModel?.isFollow(uid)
        }
    }

    private fun updateUserInfo(userInfo: UserInfo?) {
        topBar.ivAvatar.setImageUrl(userInfo?.avatar)
        topBar.tvUserName.text = userInfo?.nickName
    }

    private fun updateFollow(isFollowed: Boolean) {
        if (isFollowed) {
            topBar.btnFollow.gone()
        } else {
            topBar.btnFollow.show()
        }
    }

    private fun goProfile() {
        val uid = chatInfo?.id ?: return
        val act = activity ?: return
        Router.build(act, Profile.UserProfile.PATH)
            .putExtra(Profile.Common.EXTRA_UID, uid)
            .start()
    }

    private fun clickFollow() {
        val uid = chatInfo?.id ?: return
        followViewModel?.follow(uid)?.observe(viewLifecycleOwner) {
            showFailedToast(it)
        }
    }

    private fun clickSetting() {
        SessionSettingDialog().show(fragmentManager, "SessionSettingDialog")
    }


}