package com.adealink.weparty.profile.relation

import android.os.Bundle
import android.util.TypedValue
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.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.statusBarHeight
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.databinding.LayoutHomeTabBinding
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.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.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

@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)
        binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = this@RelationShipActivity.statusBarHeight()
        }

        pageAdapter = PageAdapter()
        binding.vpContent.offscreenPageLimit = 1
        binding.vpContent.adapter = pageAdapter
        binding.vpContent.isSaveEnabled = false
        TabLayoutMediator(
            binding.tlTab,
            binding.vpContent,
            true,
            false
        ) { tabLayout, position ->
            tabLayout.setCustomView(com.adealink.weparty.R.layout.layout_home_tab)
            tabLayout.customView?.let { tabView ->
                val tabViewBinding = LayoutHomeTabBinding.bind(tabView)
                onConfigureTab(tabViewBinding, position)
                updateTabView(
                    tabLayout,
                    0 == position
                )
            }
        }.attach()
        binding.tlTab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {

            override fun onTabSelected(tab: TabLayout.Tab?) {
                updateTabView(tab, true)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                updateTabView(tab, false)
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
        binding.vpContent.currentItem = tabIndex
    }

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

    private fun updateTabView(
        tabView: TabLayout.Tab?,
        isSelected: Boolean
    ) {
        tabView?.customView?.let { tabView ->
            val tabViewBinding = LayoutHomeTabBinding.bind(tabView)
            if (isSelected) {
                tabViewBinding.ivTabBg.show()
                tabViewBinding.tvTab.setTextColor(getCompatColor(com.adealink.weparty.R.color.color_FF1D2129))
                tabViewBinding.tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22f)
            } else {
                tabViewBinding.ivTabBg.gone()
                tabViewBinding.tvTab.setTextColor(getCompatColor(com.adealink.weparty.R.color.color_FF4E5969))
                tabViewBinding.tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
            }
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()

    }

    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_follow_list_title,
                        followCount?.toString() ?: ""
                    )
                }

                RelationShipTab.FOLLOW -> {
                    getCompatString(R.string.profile_fans_list_title, fansCount?.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)
                        }
                    }
                }
            }
        }
    }
}