package com.sambath.kunkhmer.screen.news

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.tabs.TabLayout
import com.sambath.kunkhmer.R
import com.sambath.kunkhmer.adapter.HighlightNewsAdapter
import com.sambath.kunkhmer.adapter.TopNewsAdapter
import com.sambath.kunkhmer.adapter.TopNewsCardItem
import com.sambath.kunkhmer.adapter.TopNewsHeaderAdapter
import kotlinx.android.synthetic.main.fragment_news.view.recyclerViewNews
import kotlinx.android.synthetic.main.fragment_news.view.recyclerViewNewsHeader
import kotlinx.android.synthetic.main.fragment_news.view.selectedTabTextView
import kotlinx.android.synthetic.main.fragment_news.view.tabLayout

/**
 * A simple [Fragment] subclass.
 * Use the [NewsFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class NewsFragment : Fragment() {
    private var _root: View? = null
    private val binding get() = _root!!

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

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        _root = inflater.inflate(R.layout.fragment_news, container, false)

        setTab()

        return binding
    }

    private fun setTab() {
        val tabTitles = listOf("TOP NEWS", "POPULAR", "LAST", "HIGHLIGHT")

        // 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"
                    val selectedIndex = it.position
                    when (selectedIndex) {
                        0 -> {
                            //Top News
                            setNewsHeaderFeed()
                            setNewsFeed()
                        }

                        1 -> {
                            // Popular
                        }

                        2 -> {
                            // Last
                        }

                        3 -> {
                            // Highlight
                            removeNewsHeaderFeed()
                            setHighlight()
                        }
                        // Add more cases if needed
                    }
                }
            }

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

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

        setNewsHeaderFeed()
        setNewsFeed()
    }

    private fun setNewsHeaderFeed() {
        val recyclerView = binding.recyclerViewNewsHeader
        val cardList = createCardList() // Create your card data list here
        val cardAdapter = TopNewsHeaderAdapter(cardList)

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

    private fun removeNewsHeaderFeed() {
        val recyclerView = binding.recyclerViewNewsHeader
        recyclerView.adapter = null
    }


    private fun setNewsFeed() {
        val recyclerView = binding.recyclerViewNews
        val cardList = createCardList() // Create your card data list here
        val cardAdapter = TopNewsAdapter(cardList)

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

    private fun setHighlight() {
        val recyclerView = binding.recyclerViewNews
        val cardList = createCardList() // Create your card data list here
        val cardAdapter = HighlightNewsAdapter(cardList)

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

    private fun createCardList(): List<TopNewsCardItem> {
        // Create and return your list of CardItems
        return listOf(
            TopNewsCardItem(R.drawable.ic_bg_dashboard, "Date 1", "Title 1"),
            TopNewsCardItem(R.drawable.ic_bg_dashboard, "Date 2", "Title 2"),
            TopNewsCardItem(R.drawable.ic_bg_dashboard, "Date 3", "Title 3"),
            TopNewsCardItem(R.drawable.ic_bg_dashboard, "Date 4", "Title 4"),
            TopNewsCardItem(R.drawable.ic_bg_dashboard, "Date 5", "Title 5"),
            // Add more card items as needed
        )
    }

    interface TabSelectionListener {
        fun onTabSelected(tabIndex: Int)
    }

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