package xyz.r0r5chach.dermy_app.fragments

import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.fragment.app.replace
import kotlinx.coroutines.DelicateCoroutinesApi
import xyz.r0r5chach.dermy_app.R
import xyz.r0r5chach.dermy_app.db.DermyDatabase
import xyz.r0r5chach.dermy_app.map.MapFragment
import xyz.r0r5chach.dermy_app.map.Mode

@DelicateCoroutinesApi
class HomeFragment(val db: DermyDatabase) : Fragment(R.layout.fragment_home) {
    private var buttons: IntArray = intArrayOf(R.id.add_mole_button, R.id.map_button, R.id.links_button, R.id.settings_button)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        for (button in buttons) {
            view.findViewById<ImageButton>(button).setOnClickListener {
                initButton(button)
            }
        }
    }

    private fun initButton(button: Int) {
        requireActivity().findViewById<ImageButton>(R.id.home_button).visibility = View.VISIBLE

        when (button) {
            R.id.add_mole_button -> {
                parentFragmentManager.commit {
                    setReorderingAllowed(true)
                    replace(R.id.fragment_container, MapFragment(Mode.ADD, db), null)
                    addToBackStack(null)
                }
            }
            R.id.map_button -> {
                parentFragmentManager.commit {
                    setReorderingAllowed(true)
                    replace(R.id.fragment_container, MapFragment(Mode.VIEW, db), null)
                    addToBackStack(null)
                }
            }
            R.id.links_button -> {
                parentFragmentManager.commit {
                    setReorderingAllowed(true)
                    replace<LinksFragment>(R.id.fragment_container, null)
                    addToBackStack(null)
                }
            }
            R.id.settings_button -> {
                parentFragmentManager.commit {
                    setReorderingAllowed(true)
                    replace<SettingsFragment>(R.id.fragment_container, null)
                    addToBackStack(null)
                }
            }

        }
    }
}
