package de.sebse.fuplanner2.ui.details_description

import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebSettings
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import de.sebse.fuplanner2.R
import de.sebse.fuplanner2.databinding.FragmentDescriptionBinding
import de.sebse.fuplanner2.ui.details.DetailsViewModel
import de.sebse.fuplanner2.ui.details.DetailsViewModelFactory


class DescriptionFragment : Fragment() {

    private var title: String = ""
    private val args: DescriptionFragmentArgs by navArgs()
    private val detailsViewModel: DetailsViewModel by viewModels { DetailsViewModelFactory(args.courseId) }
    private lateinit var navController: NavController
    private lateinit var binding: FragmentDescriptionBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_description, container, false).apply {
            val nightModeFlags = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
            if (nightModeFlags == Configuration.UI_MODE_NIGHT_YES && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                binding.description.settings.forceDark = WebSettings.FORCE_DARK_ON
            }
            detailsViewModel.course.observe(viewLifecycleOwner, Observer {
                binding.description.loadDataWithBaseURL("", it.description, "text/html", "UTF-8", "")
            })
            navController = findNavController()
        }
    }
}
