package com.adealink.weparty.profile.me

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.activityViewModels
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.module.profile.Profile
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.FragmentMeBinding
import com.adealink.weparty.profile.me.comp.MeFunctionCenterComp
import com.adealink.weparty.profile.me.comp.MeHeaderComp
import com.adealink.weparty.profile.me.comp.MeWalletComp
import com.adealink.weparty.profile.me.comp.OrderCenterComp
import com.adealink.weparty.profile.me.comp.PlaymateShareComp
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory
import com.adealink.weparty.util.applyImmersive
import com.adealink.weparty.R as APP_R

@RouterUri(path = [Profile.Me.PATH], desc = "我的页面")
class MeFragment : BaseFragment(R.layout.fragment_me) {

    private val binding by viewBinding(FragmentMeBinding::bind)
    private val profileViewModel by activityViewModels<ProfileViewModel> { ProfileViewModelFactory() }

    override fun initViews() {
        super.initViews()
        activity?.applyImmersive { top ->
            binding.svContent.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = top
            }
        }
        binding.clContent.setPadding(
            16.dp(),
            0.dp(),
            16.dp(),
            getCompatDimension(APP_R.dimen.main_content_padding_bottom).toInt()
        )
    }

    override fun onResume() {
        super.onResume()
        profileViewModel.pullUserInfoBy(ProfileModule.getMyUid(), false)
    }

    override fun loadData() {
        super.loadData()
        profileViewModel.pullUserInfoBy(ProfileModule.getMyUid(), true)
    }

    override fun initComponents() {
        super.initComponents()
        MeHeaderComp(this, binding.vHeader).attach()
        PlaymateShareComp(this, binding.vPlaymateShare).attach()
        MeWalletComp(this, binding.vWallet).attach()
        OrderCenterComp(this, binding.vOrderCenter).attach()
        MeFunctionCenterComp(this, binding.vFunctionCenter).attach()
    }

}