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.R
import com.adealink.weparty.profile.databinding.LayoutMeHeaderBinding
import com.adealink.weparty.profile.relation.viewmodel.FollowViewModel
import com.adealink.weparty.profile.relation.viewmodel.RelationShipTab
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory

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

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

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

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

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

    override fun onResume() {
        super.onResume()
        updateHeader(ProfileModule.getMyUserInfo())

    }

    private fun observeViewModel() {
        profileViewModel.userInfoLD.observe(viewLifecycleOwner) { user ->
            updateHeader(user)
        }
    }

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

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

    private fun goProfile() {
        val myUseInfo = profileViewModel.userInfoLD.value ?: return
        if (myUseInfo.uid.isEmpty()) {
            return
        }
        activity?.let { act ->
            Router.build(act, Profile.UserProfile.PATH)
                .putExtra(Profile.Common.EXTRA_UID, myUseInfo.uid)
                .start()
        }
    }

    private fun copyUserId() {
        val userIdStr = binding.tvUserId.text.toString()
        copyToClipBoard(userIdStr, userIdStr)
        showToast(getCompatString(R.string.profile_toast_copy_id_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()
        }
    }

}