package com.sambath.kunkhmer.screen.fighter

import android.content.Intent
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 com.sambath.kunkhmer.app.App
import com.sambath.kunkhmer.app.MainActivity
import com.sambath.kunkhmer.remote.MatchesData
import com.sambath.kunkhmer.screen.login.LoginViewModel
import com.sambath.kunkhmer.screen.login.LoginViewState
import kotlinx.android.synthetic.main.activity_login.tv_error
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
import java.util.Observer

class FighterFragment : Fragment() {
    private var _root: View? = null
    private val binding get() = _root!!

    // Inside your Fragment:
    var tabSelectionListener: TabSelectionListener? = null
    private lateinit var fighterViewModel: FighterViewModel

    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)

        fighterViewModel = FighterViewModel(App.injectApiService(), App.injectPrefHelper())
        fighterViewModel.getMatches()
        fighterViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
            render(it)
        })

        return binding
    }

    private fun render(state: FighterViewState) {
        if (state.isProgress) {
        }
        if (state.isLoginSuccess) {
            if(state.matchesData != null) {
                setTab(state.matchesData)
            }
        }
        if (state.error != null) {
        }
    }

    private fun setTab(data: MatchesData) {
        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(data)
                }
            }

            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(data: MatchesData) {
        val recyclerView = binding.recyclerViewFighter
        //val cardList = createCardList() // Create your card data list here
        val cardAdapter = FighterAdapter(data.obj, App.injectApiService())

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

    interface TabSelectionListener {
        fun onTabSelected(tabIndex: Int)
    }

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