package com.pixelized.headache

import android.Manifest.permission.READ_CALENDAR
import android.Manifest.permission.WRITE_CALENDAR
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.material3.Surface
import androidx.core.content.ContextCompat
import com.pixelized.headache.ui.navigation.home.HomeNavigator
import com.pixelized.headache.ui.navigation.main.MainNavigator
import com.pixelized.headache.ui.page.MainPage
import com.pixelized.headache.ui.page.calendar.CalendarChooserPage
import com.pixelized.headache.ui.theme.HeadacheTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    @Inject
    lateinit var mainNavigator: MainNavigator

    @Inject
    lateinit var homeNavigator: HomeNavigator

    private val requestPermissionLauncher = registerForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // Force the 3-button navigation bar to be transparent
            // See: https://developer.android.com/develop/ui/views/layout/edge-to-edge#create-transparent
            window.isNavigationBarContrastEnforced = false
        }

        if (ContextCompat.checkSelfPermission(this, READ_CALENDAR) != PERMISSION_GRANTED) {
            requestPermissionLauncher.launch(READ_CALENDAR)
        }
        if (ContextCompat.checkSelfPermission(this, WRITE_CALENDAR) != PERMISSION_GRANTED) {
            requestPermissionLauncher.launch(WRITE_CALENDAR)
        }

        setContent {
            HeadacheTheme {
                Surface {
//                    CalendarChooserPage()
                    MainPage(
                        mainNavigator = mainNavigator,
                        homeNavigator = homeNavigator,
                    )
                }
            }
        }
    }
}