package com.adealink.weparty.profile.me.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.frame.router.Router
import com.adealink.frame.util.copyToClipBoard
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.toast.util.showToast
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.profile.databinding.LayoutMeHeaderBinding
import com.adealink.weparty.profile.relation.viewmodel.RelationShipTab
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory
import com.adealink.weparty.R as APP_R

class MeHeaderComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutMeHeaderBinding
) : ViewComponent(lifecycleOwner) {

    private val profileViewModel by activityViewModels<ProfileViewModel> { ProfileViewModelFactory() }

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

    private fun initView() {
        binding.ivAvatar.onClick {
            goProfile()
        }
        binding.tvUserName.onClick {
            goProfile()
        }
        binding.tvHomePageLabel.onClick {
            goProfile()
        }
        binding.tvUserId.onClick {
            copyUserId()
        }
        binding.btnCopyId.onClick {
            copyUserId()
        }

        binding.clFollow.onClick {
            goFollowList()
        }
        binding.clFans.onClick {
            goFansList()
        }
        binding.clVisitor.onClick {
            goVisitorList()
        }
    }

    override fun onResume() {
        super.onResume()
        updateHeader(ProfileModule.getMyUserInfo())
        profileViewModel.pullMyUserInfo()
    }

    private fun observeViewModel() {
        profileViewModel.userInfoLD.observe(viewLifecycleOwner) { user ->
            updateHeader(user)
        }
        profileViewModel.myUserInfoLD.observe(viewLifecycleOwner) { myUserInfo ->
            binding.tvFollowCount.text = (myUserInfo?.userFollowStat?.followCount ?: 0).toString()
            binding.tvFansCount.text = (myUserInfo?.userFollowStat?.fansCount ?: 0).toString()
            binding.tvVisitorCount.text = (myUserInfo?.userFollowStat?.visitCount ?: 0).toString()
        }
    }

    private fun loadData() {
        updateHeader(ProfileModule.getMyUserInfo())
    }

    private fun updateHeader(userInfo: UserInfo?) {
        userInfo ?: return
        binding.ivAvatar.setImageUrl(userInfo.avatar)
        binding.tvUserName.text = userInfo.nickName
        binding.tvUserId.text = userInfo.uid
    }

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

    private fun copyUserId() {
        val myUserInfo = ProfileModule.getMyUserInfo() ?: return
        val userIdStr = myUserInfo.uid
        if (userIdStr.isEmpty()) {
            return
        }
        copyToClipBoard(userIdStr, userIdStr)
        showToast(getCompatString(APP_R.string.common_copy_success))
    }

    private fun goFollowList() {
        activity?.let { act ->
            Router.build(act, Profile.RelationShip.PATH)
                .putExtra(Profile.RelationShip.EXTRA_TAB, RelationShipTab.FOLLOW.index)
                .start()
        }
    }

    private fun goFansList() {
        activity?.let { act ->
            Router.build(act, Profile.RelationShip.PATH)
                .putExtra(Profile.RelationShip.EXTRA_TAB, RelationShipTab.FANS.index)
                .start()
        }
    }

    private fun goVisitorList() {
        activity?.let { act ->
            Router.build(act, Profile.Visitor.PATH).start()
        }
    }
}