package com.adealink.weparty.wallet.detail

import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.onClick
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.recycleview.adapter.BaseActivityTabFragmentStateAdapter
import com.adealink.weparty.commonui.widget.EmptyFragment
import com.adealink.weparty.module.wallet.Wallet
import com.adealink.weparty.wallet.R
import com.adealink.weparty.wallet.databinding.ActivityWalletDetailBinding
import com.adealink.weparty.wallet.databinding.LayoutWalletDetailTabItemBinding
import com.adealink.weparty.wallet.detail.filter.DetailDateFilterDialog
import com.adealink.weparty.wallet.detail.tab.DETAIL_TABS
import com.adealink.weparty.wallet.viewmodel.WalletDetailViewModel
import com.adealink.weparty.wallet.viewmodel.WalletViewModelFactory
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [Wallet.Detail.PATH],
    desc = "钱包首页"
)
class WalletDetailActivity : BaseActivity() {

    private val binding by viewBinding(ActivityWalletDetailBinding::inflate)
    private val viewModel by viewModels<WalletDetailViewModel> { WalletViewModelFactory() }
    private lateinit var pagerAdapter: PageAdapter

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

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        val statusBarHeight = this@WalletDetailActivity.statusBarHeight()
        binding.clTop.setPadding(0, statusBarHeight, 0, 10.dp())

        binding.btnBack.onClick {
            finish()
        }
        binding.btnFilter.onClick {
            clickFilter()
        }

        pagerAdapter = PageAdapter()
        binding.vpContent.offscreenPageLimit = 1
        binding.vpContent.adapter = pagerAdapter
        TabLayoutMediator(
            binding.tlTab,
            binding.vpContent,
            true,
            false
        ) { tabLayout, position ->
            tabLayout.setCustomView(R.layout.layout_wallet_detail_tab_item)
            tabLayout.customView?.let { customView ->
                val tabViewBinding = LayoutWalletDetailTabItemBinding.bind(customView)
                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?) {
                }

            }
        )
    }

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

    private fun updateTabView(
        tabView: TabLayout.Tab?,
        isSelected: Boolean
    ) {
        tabView?.customView?.let { tabView ->
            val tabViewBinding = LayoutWalletDetailTabItemBinding.bind(tabView)
            if (isSelected) {
                tabViewBinding.root.setBackgroundResource(APP_R.drawable.common_tab_item_bg)
                tabViewBinding.tvTab.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
            } else {
                tabViewBinding.root.setBackgroundResource(0)
                tabViewBinding.tvTab.setTextColor(getCompatColor(APP_R.color.color_FF4E5969))
            }
        }
    }

    private fun clickFilter() {
        DetailDateFilterDialog().apply {
            //setStartEnd()
            setListener(object : DetailDateFilterDialog.OnFilterListener {
                override fun onDateSelected(fromTs: Long, toTs: Long) {
                    viewModel.changedDate(fromTs, toTs)
                }
            })
        }.show(supportFragmentManager, "DetailDateFilterDialog")
    }


    internal inner class PageAdapter : BaseActivityTabFragmentStateAdapter(this) {
        override fun getTabName(pos: Int): String {
            return DETAIL_TABS.getOrNull(pos)?.name?.invoke() ?: ""
        }

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

        override fun createFragment(position: Int): Fragment {
            val homeTab = DETAIL_TABS.getOrNull(position) ?: return EmptyFragment()
            return homeTab.fragmentBuilder.invoke()
        }

    }

}