package com.sambath.kunkhmer.screen.lives

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.sambath.kunkhmer.R
import com.sambath.kunkhmer.adapter.LivesAdapter
import com.sambath.kunkhmer.adapter.LivesCardItem
import com.sambath.kunkhmer.adapter.ProductAdapter
import com.sambath.kunkhmer.app.App
import com.sambath.kunkhmer.remote.LivesInfo
import com.sambath.kunkhmer.screen.shop.ShopViewModel
import kotlinx.android.synthetic.main.fragment_event.view.recyclerViewEvent
import kotlinx.android.synthetic.main.fragment_lives.view.recyclerViewLives

class LivesFragment : Fragment(), LivesAdapter.OnItemClickListener  {
    private var _root: View? = null
    private val binding get() = _root!!
    private var cardList: List<LivesInfo>? = null
    private lateinit var livesAdapter: LivesAdapter
    private lateinit var livesViewModel: LivesViewModel

    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_lives, container, false)

        livesAdapter = LivesAdapter(mutableListOf(), this)
        livesViewModel = LivesViewModel(App.injectApiService(), App.injectPrefHelper())
        livesViewModel.getLives()
        livesViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
            render(it)
        })

        val recyclerView = binding.recyclerViewLives
        recyclerView.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        recyclerView.adapter = livesAdapter

        return binding
    }

    private fun render(state: LivesViewModel.LivesViewState) {
        if (state.isLoginSuccess) {
            if(state.livesData != null) {
                livesAdapter.cardList = state.livesData
                cardList = livesAdapter.cardList
                livesAdapter.notifyDataSetChanged()
            }
        }
        // Other state handling...
    }

//    private fun setEvent() {
//        val recyclerView = binding.recyclerViewLives
//        cardList = createCardList() // Create your card data list here
//        val cardAdapter = LivesAdapter(cardList!!, this)
//
//        recyclerView.layoutManager =
//            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
//        recyclerView.adapter = cardAdapter
//    }

//    private fun createCardList(): List<LivesCardItem> {
//        // Create and return your list of CardItems
//        return listOf(
//            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
//                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
//            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
//                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
//            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
//                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
//            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
//                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
//            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
//                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
//            // Add more card items as needed
//        )
//    }

    override fun onItemClick(position: Int) {
        val clickedItem = cardList!![position]
        val liveDetailFragment = LiveDetailFragment()

        // You can pass data to the LiveDetailFragment using arguments if needed
        val bundle = Bundle()
        bundle.putString("image_url", clickedItem.filename)
        bundle.putString("title", clickedItem.name)
        bundle.putString("description", clickedItem.desc)
        bundle.putString("url", clickedItem.url)
        liveDetailFragment.arguments = bundle

        // Replace the current fragment with the LiveDetailFragment
        parentFragmentManager.beginTransaction()
            .replace(R.id.nav_host_fragment_activity_main, liveDetailFragment)
            .addToBackStack(null) // Add to back stack to handle the back navigation
            .commit()
    }
}