package mg.dot.feufaro.ui

import SharedScreenModel
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Book
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.automirrored.filled.MenuBook
import androidx.compose.material.icons.filled.MusicNote
import androidx.compose.material3.Button
import androidx.compose.material3.DrawerState
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.Surface
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.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import mg.dot.feufaro.data.DrawerItem
import mg.dot.feufaro.getPlatform
import mg.dot.feufaro.solfa.Solfa
import mg.dot.feufaro.viewmodel.SolfaScreenModel

@Composable
fun SimpleDrawerContent(
    items: List<DrawerItem>,
    solfaScreenModel: SolfaScreenModel,
    sharedScreenModel: SharedScreenModel,
    activePath: String = Solfa.currentFile,
    drawerState: DrawerState,
    scope: CoroutineScope,
    onScannerButtonClick: () -> Unit,
    onSongSelected: (String) -> Unit,
) {
    var searchTxt by remember { mutableStateOf("") }

    val context = getPlatform()

val midi = "whawyd3.mid"
    ModalDrawerSheet(
        modifier = Modifier.width(300.dp)
    ) {
        Box (
            modifier = Modifier.fillMaxSize()
        )
        {
            val lazyListState = rememberLazyListState()
            ScrollableDrawerContent(
                lazyListState = lazyListState,
                modifier = Modifier.fillMaxSize()
            ) {
                LazyColumn(
                    state = lazyListState,
                ){
                    stickyHeader {
                        Surface(
                            color = MaterialTheme.colorScheme.surfaceContainerHigh,
                            modifier = Modifier.fillParentMaxWidth()
                        ) {
                            Text(
                                "Liste des solfa disponibles",
                                style = MaterialTheme.typography.titleLarge
                            )
                        }
                    }
                    itemsIndexed(items){ index, item ->
                        val isSelected = item.path == activePath
                        val title = item.title
                        var isFfpm = false
                        var isEws = false
                        var isFF = false

                        if (title.startsWith("ffpm")) {
                            isFfpm = true
                        } else if(title.startsWith("ews")) {
                            isEws = true
                        } else {
                            isFF = true
                        }
                        NavigationDrawerItem(
                            label = {
                                Text(item.title)
                            },
                                icon = {
                                    val isIcon = when {
                                        isFfpm -> Icons.AutoMirrored.Filled.MenuBook
                                        isEws -> Icons.Filled.MusicNote
                                        isFF -> Icons.Filled.Book
                                        else -> Icons.Filled.Menu
                                    }
                                    Icon(
                                        isIcon,
                                        contentDescription = "",
                                        tint = Color.Blue
                                    )
                                },
                            badge = {
                                Icon(
                                    Icons.Filled.Menu,
                                    contentDescription = ""
                                )
                            },
                            selected = if(isSelected){ true } else { false },
                            onClick = {
                                scope.launch {
                                    drawerState.close()
                                }
                                sharedScreenModel.reset()
                                solfaScreenModel.loadFromFile(item.path)
                                onSongSelected(midi)
                            }
                        )
                    }
                }
                Box(
                    modifier = Modifier
                                        .fillMaxWidth()
                                        .align(Alignment.BottomCenter)
                                        .padding(16.dp),
                    contentAlignment = Alignment.Center
                ) {
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Row (

                        ) {
                            Spacer(modifier = Modifier.width(10.dp))
                            Button(
                                onClick = {
                                    onScannerButtonClick()
                                }
                            ) {
                                Text("Scanner")
                            }
                            Spacer(modifier = Modifier.width(10.dp))
                            Button(
                                onClick = {
                                scope.launch {
                                    drawerState.close()
                                }
                                solfaScreenModel.loadCustomFile()
                                }
                            ) {
                                Text("Importer")
                            }
                        }
                    }
                }
            }
        }
    }
}