package com.adealink.weparty.room.applymic

import androidx.fragment.app.Fragment
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.weparty.commonui.recycleview.adapter.BaseTabFragmentStateAdapter
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.commonui.widget.EmptyFragment
import com.adealink.weparty.room.R
import com.adealink.weparty.room.databinding.DialogRoomApplyMicHostListBinding
import com.adealink.weparty.room.databinding.LayoutHostApplyMicTabItemBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.adealink.weparty.R as APP_R

class HostApplyMicListDialog : BottomDialogFragment(R.layout.dialog_room_apply_mic_host_list) {

    private val binding by viewBinding(DialogRoomApplyMicHostListBinding::bind)

    private lateinit var pagerAdapter: PageAdapter

    override fun initViews() {
        super.initViews()

        pagerAdapter = PageAdapter()
        binding.vpContent.offscreenPageLimit = 1
        binding.vpContent.adapter = pagerAdapter
        binding.vpContent.isSaveEnabled = false
        TabLayoutMediator(
            binding.tlTab,
            binding.vpContent,
            true,
            true
        ) { tabLayout, position ->
            tabLayout.setCustomView(R.layout.layout_host_apply_mic_tab_item)
            tabLayout.customView?.let { customView ->
                updateTabView(
                    tabLayout,
                    0 == position,
                    position
                )
            }
        }.attach()
        binding.tlTab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {

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

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

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

        })
        binding.vpContent.currentItem = 0
    }

    fun updateTabView(
        tabView: TabLayout.Tab?,
        isSelected: Boolean,
        position: Int
    ) {
        tabView?.customView?.let {
            val customViewBinding = LayoutHostApplyMicTabItemBinding.bind(it)
            customViewBinding.tvTab.text = HOST_APPLY_MIC_TABS.getOrNull(position)?.name?.invoke() ?: ""
            if (isSelected) {
                customViewBinding.root.setBackgroundResource(APP_R.drawable.common_tab_item_bg)
                customViewBinding.tvTab.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
            } else {
                customViewBinding.root.setBackgroundResource(0)
                customViewBinding.tvTab.setTextColor(getCompatColor(APP_R.color.color_FFC9CDD4))
            }
        }
    }


    internal inner class PageAdapter : BaseTabFragmentStateAdapter(this) {
        override fun getTabName(pos: Int): String {
            return ""
        }

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

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

    }
}