package com.adealink.weparty.profile

import android.os.Bundle
import androidx.activity.viewModels
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.BindExtra
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.recycleview.adapter.BaseActivityTabFragmentStateAdapter
import com.adealink.weparty.commonui.widget.EmptyFragment
import com.adealink.weparty.module.profile.Profile
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.TAB_INDEX_PERSONAL
import com.adealink.weparty.profile.comp.ProfileBottomComp
import com.adealink.weparty.profile.comp.ProfileHeaderComp
import com.adealink.weparty.profile.comp.ProfileToolBarViewComp
import com.adealink.weparty.profile.databinding.ActivityUserProfileBinding
import com.adealink.weparty.profile.relation.viewmodel.FollowViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory
import kotlin.getValue

@RouterUri(
    path = [Profile.UserProfile.PATH],
    desc = "用户个人页"
)
class UserProfileActivity : BaseActivity() {

    @BindExtra(name = Profile.Common.EXTRA_UID)
    var userUid: String = ""

    val isSelf by fastLazy { userUid == ProfileModule.getMyUid() }

    private val binding by viewBinding(ActivityUserProfileBinding::inflate)

    private val profileViewModel by viewModels<ProfileViewModel> { ProfileViewModelFactory() }
    private val followViewModel by viewModels<FollowViewModel> { ProfileViewModelFactory() }
    private lateinit var profilePageAdapter: ProfilePageAdapter

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        binding.profileHeaderLayout.ivBg.updateLayoutParams<ConstraintLayout.LayoutParams> {
            height =
                getCompatDimension(R.dimen.profile_header_height).toInt() + this@UserProfileActivity.statusBarHeight()
        }

        profilePageAdapter = ProfilePageAdapter()
        binding.profileVp.adapter = profilePageAdapter
        binding.tabLayout.createMediatorAndAttach(
            binding.profileVp,
            profilePageAdapter,
            TAB_INDEX_PERSONAL
        )
    }

    override fun initComponents() {
        super.initComponents()
        ProfileToolBarViewComp(this, userUid, binding).attach()
        ProfileHeaderComp(this, userUid, binding.profileHeaderLayout).attach()
        ProfileBottomComp(this, userUid, binding.vBottom).attach()
    }

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

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

    internal inner class ProfilePageAdapter : BaseActivityTabFragmentStateAdapter(this) {

        override fun getTabName(pos: Int): String {
            return PROFILE_TABS.getOrNull(pos)?.name?.invoke() ?: ""
        }

        override fun getItemCount(): Int {
            return PROFILE_TABS.size
        }

        override fun createFragment(position: Int): Fragment {
            val profileTab = PROFILE_TABS.getOrNull(position) ?: return EmptyFragment()
            return when (profileTab.type) {
                ProfileTab.PROFILE -> {
                    profileTab.fragmentBuilder().apply {
                        arguments = Bundle().apply {
                            putString(Profile.Common.EXTRA_UID, userUid)
                        }
                    }
                }

                ProfileTab.PHOTO_WALL -> {
                    profileTab.fragmentBuilder().apply {
                        arguments = Bundle().apply {
                            putString(Profile.Common.EXTRA_UID, userUid)
                        }
                    }
                }
            }
        }
    }
}