package de.sebse.fuplanner2.ui.courses

import android.os.Bundle
import android.provider.Settings.Global.getString
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import com.google.android.material.composethemeadapter.MdcTheme
import de.sebse.fuplanner2.databinding.FragmentRefreshRecyclerBinding

class CoursesFragment : Fragment() {

    private lateinit var coursesViewModel: CoursesViewModel
    private lateinit var navController: NavController
    private lateinit var binding: FragmentRefreshRecyclerBinding


    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View {
        navController = findNavController()
        coursesViewModel = ViewModelProvider(this).get(CoursesViewModel::class.java)
        binding = FragmentRefreshRecyclerBinding.inflate(inflater, container, false)
        return ComposeView(requireContext()).apply {
            setContent {
                MdcTheme {
                    val courses = coursesViewModel.text.observeAsState(listOf())
                    LazyColumn {
                        items(courses.value) { course -> CourseItem(course) {
                            course.uid?.let {
                                navController.navigate(CoursesFragmentDirections.actionNavHomeToCourseDetails(it, course.title))
                            }
                        } }
                    }
                }
            }
        }
    }
}
