package com.sambath.kunkhmer.screen.fighter

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.tabs.TabLayout
import com.sambath.kunkhmer.R
import com.sambath.kunkhmer.adapter.FighterAdapter
import com.sambath.kunkhmer.adapter.FighterCardItem
import kotlinx.android.synthetic.main.fragment_fighter.view.recyclerViewFighter
import kotlinx.android.synthetic.main.fragment_fighter.view.selectedTabTextView
import kotlinx.android.synthetic.main.fragment_fighter.view.tabLayout

class FighterFragment : Fragment() {
    private var _root: View? = null

    private val binding get() = _root!!

    // Inside your Fragment:
    var tabSelectionListener: TabSelectionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _root = inflater.inflate(R.layout.fragment_fighter, container, false)

        setTab()
        return binding
    }

    private fun setTab() {
        val tabTitles = listOf("Fri 18 Aug", "Yesterday", "Today", "Tomorrow", "Tue 22 Aug")

        // Populate TabLayout with dynamic tabs
        for (title in tabTitles) {
            val tab = binding.tabLayout.newTab().setText(title)
            binding.tabLayout.addTab(tab)
        }

        // Set tab click listener
        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.let {
                    val selectedTitle = it.text.toString()
                    binding.selectedTabTextView.text = "Selected Tab: $selectedTitle"
                    setFighters()
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                // No action needed
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
                // No action needed
            }
        })

        binding.tabLayout.getTabAt(2)?.select()
    }

    private fun setFighters() {
        val recyclerView = binding.recyclerViewFighter
        val cardList = createCardList() // Create your card data list here
        val cardAdapter = FighterAdapter(cardList)

        recyclerView.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        recyclerView.adapter = cardAdapter
    }

    private fun createCardList(): List<FighterCardItem> {
        // Create and return your list of CardItems
        return listOf(
            FighterCardItem(R.drawable.ic_user_white, "Fighter A", R.drawable.ic_user_white, "Fighter B", "Title 1", "Khun Khmer Champion", true),
            FighterCardItem(R.drawable.ic_user_white, "Fighter A", R.drawable.ic_user_white, "Fighter B", "Title 1", "Khun Khmer Champion", true),
            FighterCardItem(R.drawable.ic_user_white, "Fighter A", R.drawable.ic_user_white, "Fighter B", "Title 1", "Khun Khmer Champion", true),
            FighterCardItem(R.drawable.ic_user_white, "Fighter A", R.drawable.ic_user_white, "Fighter B", "Title 1", "Khun Khmer Champion", true),
            FighterCardItem(R.drawable.ic_user_white, "Fighter A", R.drawable.ic_user_white, "Fighter B", "Title 1", "Khun Khmer Champion", true),
            // Add more card items as needed
        )
    }

    interface TabSelectionListener {
        fun onTabSelected(tabIndex: Int)
    }

    fun selectTab(tabIndex: Int) {
        binding.tabLayout.getTabAt(tabIndex)?.select()
    }
}