package ch.dissem.yaep.android

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import ch.dissem.yaep.domain.generateGame
import ch.dissem.yaep.ui.common.App
import ch.dissem.yaep.ui.common.focus.FocusFollowingSelectionManager
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import yaep.commonui.generated.resources.action_restart
import yaep.commonui.generated.resources.app_name
import yaep.commonui.generated.resources.restart
import yaep.commonui.generated.resources.Res as CRes

class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            var game by remember { mutableStateOf(generateGame()) }
            var resetCluesBeacon by remember { mutableStateOf(Any()) }

            Scaffold(
                topBar = {
                    CenterAlignedTopAppBar(
                        title = {
                            Text(text = stringResource(CRes.string.app_name))
                        },
                        navigationIcon = { },
                        actions = {
                            IconButton(
                                onClick = {
                                    do {
                                        // continue
                                    } while (game.grid.undo())
                                    resetCluesBeacon = Any()
                                }
                            ) {
                                Icon(
                                    painter = painterResource(CRes.drawable.action_restart),
                                    contentDescription = stringResource(CRes.string.restart)
                                )
                            }
                        },
                    )
                }
            ) { insets ->
                App(
                    modifier = Modifier.padding(insets),
                    rootSelectionManager = FocusFollowingSelectionManager,
                    spacing = 4.dp,
                    game = game,
                    onNewGame = { game = generateGame() },
                    resetCluesBeacon = resetCluesBeacon
                )
            }
        }
    }
}

@Preview
@Composable
fun AppAndroidPreview() {
    var game by remember { mutableStateOf(generateGame()) }
    var resetCluesBeacon by remember { mutableStateOf(Any()) }

    App(
        game = game,
        spacing = 4.dp,
        rootSelectionManager = FocusFollowingSelectionManager,
        onNewGame = { game = generateGame() },
        resetCluesBeacon = resetCluesBeacon
    )
}
