package mg.dot.feufaro

import SharedScreenModel
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.screen.Screen
import java.io.ObjectStreamException
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.material3.Text
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import cafe.adriel.voyager.koin.koinScreenModel
import mg.dot.feufaro.solfa.Transpose
import androidx.compose.ui.Alignment
import mg.dot.feufaro.solfa.TimeUnitObject


object ScreenTranspose : Screen {
    @Composable
    override fun Content() {
        FlowRow(
            modifier = Modifier.fillMaxWidth()
                .windowInsetsPadding(WindowInsets.safeDrawing)
                .padding(start = 8.dp, end = 8.dp, top = 8.dp),
            horizontalArrangement = Arrangement.spacedBy(16.dp),
            verticalArrangement = Arrangement.spacedBy(4.dp)
        ) {
            val sharedScreenModel = koinScreenModel<SharedScreenModel>()
            val originalKey by sharedScreenModel.songKey.collectAsState()
            val transposeTo by sharedScreenModel.transposeTo.collectAsState()
            val transposeAsIf by sharedScreenModel.transposeAsIf.collectAsState()
            Box(modifier = Modifier.fillMaxHeight().align(Alignment.CenterVertically)) {
                Text("Original Key: $originalKey")
            }
            Box(modifier = Modifier.fillMaxHeight().align(Alignment.CenterVertically)) {
                Text("Transpose to key: $transposeTo")
            }
            Transpose.chooseKey { it ->
                sharedScreenModel.setTransposeTo(it)
                TimeUnitObject.changeTransposeTo(it)
            }
            Box(modifier = Modifier.fillMaxHeight().align(Alignment.CenterVertically)) {
                Text("Transpose as if key was : $transposeAsIf")
            }
            Transpose.chooseKey { it ->
                sharedScreenModel.setTransposeAsIf(it)
                TimeUnitObject.changeTransposeAsIf(it)
            }
        }
    }
    @Throws(ObjectStreamException::class) // C'est une méthode de sérialisation Java, donc l'exception est nécessaire
    private fun readResolve(): Any {
        return this // Toujours retourner l'instance unique de ce singleton
    }
}