package xyz.r0r5chach.dermy_app

import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.add
import androidx.fragment.app.commit
import androidx.fragment.app.replace
import androidx.room.Room
import com.badlogic.gdx.backends.android.AndroidFragmentApplication
import kotlinx.coroutines.DelicateCoroutinesApi
import xyz.r0r5chach.dermy_app.db.DermyDatabase
import xyz.r0r5chach.dermy_app.fragments.HomeFragment

class MainActivity: AppCompatActivity(R.layout.activity_main), AndroidFragmentApplication.Callbacks {
    val db = DermyDatabase(this)

    @OptIn(DelicateCoroutinesApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val homeButton = findViewById<ImageButton>(R.id.home_button)

        homeButton.visibility = View.GONE

        homeButton.setOnClickListener {
            goHome()
            homeButton.visibility = View.GONE
        }

        if (savedInstanceState == null) {
            supportFragmentManager.commit {
                setReorderingAllowed(true)
                add(R.id.fragment_container, HomeFragment(db!!), null)
            }
        }

    }

    override fun exit() {}

    @OptIn(DelicateCoroutinesApi::class)
    fun goHome() {
        supportFragmentManager.commit {
            setReorderingAllowed(true)
            replace(R.id.fragment_container, HomeFragment(db!!), null)
            addToBackStack(null)
        }
    }
}
