package eu.darken.bluemusic.devices.ui.settings

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Start
import androidx.compose.material.icons.twotone.Approval
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import eu.darken.bluemusic.R
import eu.darken.bluemusic.common.compose.Preview2
import eu.darken.bluemusic.common.compose.PreviewWrapper
import eu.darken.bluemusic.common.compose.horizontalCutoutPadding
import eu.darken.bluemusic.common.compose.navigationBarBottomPadding
import eu.darken.bluemusic.common.error.ErrorEventHandler
import eu.darken.bluemusic.common.settings.SettingsDivider
import eu.darken.bluemusic.common.settings.SettingsSwitchItem
import androidx.lifecycle.compose.collectAsStateWithLifecycle

@Composable
fun DevicesSettingsScreenHost(vm: DevicesSettingsViewModel = hiltViewModel()) {
    ErrorEventHandler(vm)

    val state by vm.state.collectAsStateWithLifecycle()

    state?.let { state ->
        DevicesSettingsScreen(
            state = state,
            onNavigateUp = { vm.navUp() },
            onUpgrade = { vm.upgrade() },
            onToggleEnabled = { vm.onToggleEnabled(it) },
            onToggleRestoreOnBoot = { vm.onToggleRestoreOnBoot(it) },
        )
    }
}

@Composable
fun DevicesSettingsScreen(
    state: DevicesSettingsViewModel.State,
    onNavigateUp: () -> Unit,
    onUpgrade: () -> Unit,
    onToggleEnabled: (Boolean) -> Unit,
    onToggleRestoreOnBoot: (Boolean) -> Unit,
) {

    val snackbarHostState = remember { SnackbarHostState() }

    Scaffold(
        snackbarHost = {
            SnackbarHost(
                hostState = snackbarHostState,
                modifier = Modifier.navigationBarsPadding()
            )
        },
        topBar = {
            TopAppBar(
                title = { Text(stringResource(R.string.devices_settings_label)) },
                navigationIcon = {
                    IconButton(onClick = onNavigateUp) {
                        Icon(
                            imageVector = Icons.AutoMirrored.Filled.ArrowBack,
                            contentDescription =
                                stringResource(R.string.general_back_action)
                        )
                    }
                }
            )
        },
        contentWindowInsets = WindowInsets.statusBars
    ) { paddingValues ->
        val navBarPadding = navigationBarBottomPadding()
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .horizontalCutoutPadding(),
            contentPadding = PaddingValues(bottom = navBarPadding),
            verticalArrangement = Arrangement.Top
        ) {
            item {
                SettingsSwitchItem(
                    icon = Icons.TwoTone.Approval,
                    title = stringResource(R.string.label_app_enabled),
                    subtitle = stringResource(R.string.description_app_enabled),
                    checked = state.isEnabled,
                    onCheckedChange = { onToggleEnabled(it) }
                )
                SettingsDivider()
            }
            item {
                SettingsSwitchItem(
                    icon = Icons.Default.Start,
                    title = stringResource(R.string.label_boot_restore),
                    subtitle = stringResource(R.string.description_boot_restore),
                    checked = state.restoreOnBoot,
                    onCheckedChange = { onToggleRestoreOnBoot(it) }
                )
                SettingsDivider()
            }
        }
    }

}

@Preview2
@Composable
private fun DevicesSettingsScreenPreview() {
    PreviewWrapper {
        DevicesSettingsScreen(
            state = DevicesSettingsViewModel.State(
                isUpgraded = true,
                isEnabled = true,
                restoreOnBoot = false,
            ),
            onNavigateUp = {},
            onUpgrade = {},
            onToggleEnabled = {},
            onToggleRestoreOnBoot = {},
        )
    }
}