package xyz.r0r5chach.dermy_app.map

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.commit
import androidx.fragment.app.replace
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration
import com.badlogic.gdx.backends.android.AndroidFragmentApplication
import xyz.r0r5chach.dermy_app.PredictionMode
import xyz.r0r5chach.dermy_app.R
import xyz.r0r5chach.dermy_app.db.DermyDatabase
import xyz.r0r5chach.dermy_app.fragments.CameraFragment
import xyz.r0r5chach.dermy_app.fragments.LocationFragment

class MapFragment(private val mapMode: Mode, val db: DermyDatabase) : AndroidFragmentApplication() {
    private var map: BodyMap? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreate(savedInstanceState)

        val backgroundColor = requireContext().getColor(R.color.cardview_dark_background)

        map = BodyMap ({ newValue ->
            if (newValue != null) {
                parentFragmentManager.commit {
                    setReorderingAllowed(true)
                    when (mapMode) {
                        Mode.ADD -> replace(R.id.fragment_container, CameraFragment(newValue, db, PredictionMode.NEW, null), null)
                        Mode.VIEW -> replace(R.id.fragment_container, LocationFragment(newValue, db), null)
                    }
                    addToBackStack(null)
                }
            }
        }, backgroundColor)
        return initializeForView(map, initConfig())
    }

    private fun initConfig(): AndroidApplicationConfiguration {
        val config = AndroidApplicationConfiguration()
        config.useAccelerometer = false
        config.useCompass = false
        config.useGyroscope = false
        config.useWakelock = false
        config.useImmersiveMode = false
        config.useRotationVectorSensor = false

        return config
    }
}
