package app.padelclub

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.safeContentPadding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.ui.tooling.preview.Preview

import padelclub.composeapp.generated.resources.Res
import padelclub.composeapp.generated.resources.compose_multiplatform
import app.padelclub.lestorage.LeStorage
import app.padelclub.lestorage.StoreCenter
import app.padelclub.lestorage.Store
import app.padelclub.lestorage.StoredCollection
import app.padelclub.lestorage.AndroidFileWriter
import app.padelclub.data.Session

@Composable
@Preview
fun App() {
    MaterialTheme {
        val context = LocalContext.current
        val storeCenter = remember { StoreCenter(context.filesDir.absolutePath + "/storage") }
        val sessionCollection = remember { storeCenter.mainStore.collection<Session>("sessions.json") }

        var sessionCount by remember { mutableStateOf(sessionCollection.items.size) }

        var showContent by remember { mutableStateOf(false) }
        Column(
            modifier = Modifier
                .background(MaterialTheme.colorScheme.primaryContainer)
                .safeContentPadding()
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Button(onClick = {
                val randomName = "Session-${(1000..9999).random()}"
                val newSession = Session(name = randomName)
                sessionCollection.addOrUpdate(newSession)
                sessionCount = sessionCollection.items.size
            }) {
                Text("Add New Session")
            }

            Text("Total Sessions: $sessionCount")

            Button(onClick = { showContent = !showContent }) {
                Text("Click me!")
            }
            AnimatedVisibility(showContent) {
                val greeting = remember { LeStorage.GREETING }
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalAlignment = Alignment.CenterHorizontally,
                ) {
                    Image(painterResource(Res.drawable.compose_multiplatform), null)
                    Text("Compose: $greeting")
                }
            }
        }
    }
}