package com.adealink.weparty.profile.relation

import android.os.Bundle
import androidx.activity.viewModels
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.aab.util.getCompatString
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.onClick
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
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.profile.R
import com.adealink.weparty.profile.databinding.ActivityProfileRelationshipBinding
import com.adealink.weparty.profile.databinding.LayoutRelationshipTabBinding
import com.adealink.weparty.profile.relation.viewmodel.FollowViewModel
import com.adealink.weparty.profile.relation.viewmodel.RELATIONSHIP_TABS
import com.adealink.weparty.profile.relation.viewmodel.RelationShipTab
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory
import com.adealink.weparty.util.applyImmersive

@RouterUri(
    path = [Profile.RelationShip.PATH],
    desc = "关系"
)
class RelationShipActivity : BaseActivity() {

    @BindExtra(name = Profile.RelationShip.EXTRA_TAB)
    var tabIndex: Int = RelationShipTab.FOLLOW.index

    private val binding by viewBinding(ActivityProfileRelationshipBinding::inflate)
    private val viewModel by viewModels<FollowViewModel> { ProfileViewModelFactory() }
    private lateinit var pageAdapter: PageAdapter

    private var followCount: Int? = null
    private var fansCount: Int? = null

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

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        applyImmersive { top ->
            binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = top
            }
        }
        binding.ivBack.onClick {
            finish()
        }

        pageAdapter = PageAdapter()
        binding.vpContent.offscreenPageLimit = 1
        binding.vpContent.adapter = pageAdapter
        binding.vpContent.isSaveEnabled = false
        binding.vpContent.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                tabIndex = position
                updateTabView(
                    binding.vFollowTab,
                    RelationShipTab.FOLLOW.index == position
                )
                updateTabView(
                    binding.vFansTab,
                    RelationShipTab.FANS.index == position
                )
            }
        })
        onConfigureTab(binding.vFollowTab, 0)
        updateTabView(
            binding.vFollowTab,
            RelationShipTab.FOLLOW.index == tabIndex
        )
        binding.vFollowTab.root.onClick {
            binding.vpContent.currentItem = 0
        }
        onConfigureTab(binding.vFansTab, 1)
        updateTabView(
            binding.vFansTab,
            RelationShipTab.FANS.index == tabIndex
        )
        binding.vFansTab.root.onClick {
            binding.vpContent.currentItem = 1
        }
        binding.vpContent.currentItem = tabIndex
    }

    private fun onConfigureTab(binding: LayoutRelationshipTabBinding, position: Int) {
        binding.tvTab.text = pageAdapter.getTabName(position)
    }

    private fun updateTabView(
        binding: LayoutRelationshipTabBinding,
        isSelected: Boolean
    ) {
        if (isSelected) {
            binding.ivTabBg.show()
            binding.tvCount.setTextColor(getCompatColor(com.adealink.weparty.R.color.color_FF1D2129))
            binding.tvTab.setTextColor(getCompatColor(com.adealink.weparty.R.color.color_FF1D2129))
        } else {
            binding.ivTabBg.gone()
            binding.tvCount.setTextColor(getCompatColor(com.adealink.weparty.R.color.color_FFC9CDD4))
            binding.tvTab.setTextColor(getCompatColor(com.adealink.weparty.R.color.color_FFC9CDD4))
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.followCountLD.observe(this) {
            updateCount(binding.vFollowTab, it)
        }
        viewModel.fansCountLD.observe(this) {
            updateCount(binding.vFansTab, it)
        }
    }

    private fun updateCount(
        binding: LayoutRelationshipTabBinding,
        count: Long,
    ) {
        binding.tvCount.text = count.toString()
    }

    internal inner class PageAdapter : BaseActivityTabFragmentStateAdapter(this) {

        override fun getTabName(pos: Int): String {
            val tab = RELATIONSHIP_TABS.getOrNull(pos) ?: return ""
            return when (tab.type) {
                RelationShipTab.FANS -> {
                    getCompatString(
                        R.string.profile_fans_list_title,
                        fansCount?.toString() ?: ""
                    )
                }

                RelationShipTab.FOLLOW -> {
                    getCompatString(
                        R.string.profile_follow_list_title,
                        followCount?.toString() ?: ""
                    )
                }
            }
        }

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

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

                RelationShipTab.FANS -> {
                    profileTab.fragmentBuilder().apply {
                        arguments = Bundle().apply {
//                            putString(Profile.Common.EXTRA_UID, userUid)
                        }
                    }
                }
            }
        }
    }
}