package com.laseroptek.raman.ui.screens.home.preset


import android.content.res.Configuration
import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
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.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.VerticalDivider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import com.laseroptek.raman.R
import com.laseroptek.raman.const.PulseDurations
import com.laseroptek.raman.const.handPieceTypes
import com.laseroptek.raman.data.datasource.db.RamanDatabase
import com.laseroptek.raman.data.model.Preset
import com.laseroptek.raman.data.source.db.RamanDatabaseService
import com.laseroptek.raman.repository.DatabaseRepository
import com.laseroptek.raman.repository.PreferenceRepository
import com.laseroptek.raman.repository.SerialPortRepository
import com.laseroptek.raman.ui.common.fullscreen.FullScreen
import com.laseroptek.raman.ui.screens.main.MainViewModel
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.ui.theme.robotoTextStyle
import com.laseroptek.raman.utils.DefaultDispatcherProvider
import com.laseroptek.raman.utils.ext.degreeToStep
import com.laseroptek.raman.utils.ext.dropShadow
import com.laseroptek.raman.utils.ext.innerShadow
import com.laseroptek.raman.utils.ext.px
import kotlinx.coroutines.launch
import timber.log.Timber

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PresetSavePopup(
    mainViewModel: MainViewModel = hiltViewModel<MainViewModel>(),
    onClick: (Boolean) -> Unit = {},
    title: String = stringResource(R.string.save_title),
) {
    val scope = rememberCoroutineScope()
    val context = LocalContext.current  // context For open system setting

    val presetList by mainViewModel.presetList.collectAsState()

    // handPiece
    val handPiece by mainViewModel.handPiece.collectAsState()
    val handPieceTitle = handPieceTypes.get(handPiece.type)

    // pulseWidth
    val pulseAngle by mainViewModel.pulseAngle.collectAsState()
    val pulseStep = pulseAngle.degreeToStep(totalSteps = PulseDurations.size)
    val pulseWidth = PulseDurations[pulseStep]

    // fluence
    val fluenceAngle by mainViewModel.fluenceAngle.collectAsState()
    val fluenceList by mainViewModel.fluenceList.collectAsState()
    val fluenceStep = fluenceAngle.degreeToStep(totalSteps = fluenceList.size)
    val fluence = fluenceList[fluenceStep]

    // repetition
    val repetitionAngle by mainViewModel.repetitionAngle.collectAsState()
    val repetitionList by mainViewModel.repetitionList.collectAsState()
    val repetitionStep = repetitionAngle.degreeToStep(totalSteps = repetitionList.size)
    val repetition = repetitionList[repetitionStep]

    // preset index (priority)
    var selectedPriority by remember { mutableStateOf(0) }

    // preset name
    var presetName by remember { mutableStateOf("") }

    /*
    var toastMessage by remember { mutableStateOf("") }
    if (toastMessage.isNotEmpty()) {
        LaunchedEffect(toastMessage) {
            delay(2500) // Show for 2.5 seconds
            toastMessage = "" // Clear the message to hide it
        }
    }
    */

    // keyboard
    val focusRequester = remember { FocusRequester() }
    val focusManager = LocalFocusManager.current
    val performOkClick = {
        when {
            // Condition 0: Check if the preset priority is selected.
            /*
            selectedPriority == 0 -> {
                Timber.d("isEmpty - $selectedPriority")
                Toast.makeText(
                    context,
                    "Preset priority is not selected. Please select it first.",
                    Toast.LENGTH_LONG
                ).show()
            }
            */

            // Condition 1: Check if the preset name is empty.
            presetName.trim().isEmpty() -> {
                Timber.d("isEmpty - $presetName")
                Toast.makeText(
                    context,
                    "Preset name is empty. Please input it first.",
                    Toast.LENGTH_LONG
                ).show()
                //toastMessage = "Preset name is empty. Please input name first."
            }

            // Condition 2: Check if the name already exists in the preset list.
            // This removes the need for the 'existingPresetNames' variable.
            presetList.any { it.name.trim() == presetName.trim() } -> {
                Timber.d("hasName - $presetName")
                Toast.makeText(
                    context,
                    "Selected name already exists. Please change name.",
                    Toast.LENGTH_LONG
                ).show()
                //toastMessage = "Selected name already exists. Please change name."
            }

            // Condition 3 (Success): If all checks pass, proceed with saving.
            else -> {
                // Hide the keyboard first for a clean UI transition
                focusManager.clearFocus()

                // 1. Get the current list of presets.
                var listToModify = presetList

                // 2. Find and remove the old preset that occupies the same priority and handpiece slot.
                val existingPresetToReplace = listToModify.find { preset ->
                    preset.priority == selectedPriority && preset.handPieceType == handPiece.type
                }

                // 3. If a preset to replace was found, update it's priority to 0 from our temporary list.
                if (existingPresetToReplace != null) {
                    Timber.d("Found existing preset to replace: $existingPresetToReplace. Removing it.")
                    //listToModify = listToModify.minus(existingPresetToReplace)
                    listToModify = listToModify.map { preset ->
                        if (preset.id == existingPresetToReplace.id) {
                            preset.copy(priority = 0)
                        } else {
                            preset
                        }
                    }
                }

                // 4. Create the new preset object to be added.
                val newPreset = Preset(
                    handPieceType = handPiece.type,
                    pulseWidth = pulseWidth,
                    fluence = fluence,
                    repetition = repetition,
                    name = presetName,
                    priority = selectedPriority,
                    id = (presetList.maxOfOrNull { it.id } ?: -1) + 1 // Ensure a unique ID
                )
                Timber.d("New preset to be added: $newPreset")

                // 5. Add the new preset to the modified list.
                val finalList = listToModify.plus(newPreset)

                // 6. Update the ViewModel with the final list.
                mainViewModel.setPresetList(finalList)

                // 7. Save the updated list to preferences.
                scope.launch {
                    mainViewModel.savePresetListToPreference()
                }

                Timber.d("Final presetList: ${mainViewModel.presetList.value}")

                onClick(true)
            }
        }
    }

    DisposableEffect(Unit) {
        onDispose {
            Timber.d("MyComposable is being disposed!")
            focusManager.clearFocus(force = true) // Added force = true
        }
    }

    FullScreen(contentAlignment= Alignment.TopCenter) {
        Box {
            Column(
                modifier = Modifier
                    .size(633.px.dp, 370.px.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Top
            ) {
                //Spacer(modifier = Modifier.weight(1f))
                Spacer(modifier = Modifier.height(50.px.dp))

                Column(
                    modifier = Modifier
                        .size(633.px.dp, 270.px.dp)
                        .clip(RoundedCornerShape(16.px.dp))
                        .background(Color.White)
                        .dropShadow(
                            shape = RoundedCornerShape(16.px.dp),
                            color = Color.Black.copy(alpha = 0.2f),
                            blur = 48.px.dp,
                            offsetX = 0.px.dp,
                            offsetY = 24.px.dp,
                            spread = 0.px.dp
                        )
                        .dropShadow(
                            shape = RoundedCornerShape(16.px.dp),
                            color = Color.Black.copy(alpha = 0.1f),
                            blur = 6.px.dp,
                            offsetX = 0.px.dp,
                            offsetY = 3.px.dp,
                            spread = 0.px.dp
                        )
                        .padding(start = 10.px.dp, end = 10.px.dp, top = 20.px.dp),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Top
                ) {
                    // Table
                    Row(
                        modifier = Modifier
                            .size(585.px.dp, 78.px.dp)
                            .clip(RoundedCornerShape(4.px.dp))
                            .background(
                                brush = Brush.linearGradient(
                                    colors = listOf(
                                        Color(255, 255, 255),
                                        Color(248, 245, 238),
                                    )
                                )
                            )
                            .border(
                                width = 1.4.px.dp,
                                //color = Color.White,
                                brush = Brush.linearGradient(
                                    colors = listOf(
                                        Color(183, 142, 9),
                                        Color(75, 63, 23)
                                    ),
                                    //start = Offset(0f, 0f), // Optional: Start at the top-left
                                    //end = Offset(200f, 200f) // Optional: End at the bottom-right
                                ),
                                shape = RoundedCornerShape(4.px.dp),
                            )
                            .dropShadow(
                                shape = RoundedCornerShape(4.px.dp),
                                color = Color.Black.copy(alpha = 0.12f),
                                blur = 10.px.dp,
                                offsetX = 0.px.dp,
                                offsetY = 2.px.dp,
                                spread = 0.px.dp
                            ),
                        horizontalArrangement = Arrangement.Center,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        // Spotsize (HandPiece)
                        Column(
                            modifier = Modifier
                                .fillMaxSize()
                                .weight(1f)
                                .clip(RoundedCornerShape(topStart = 4.px.dp, bottomStart = 4.px.dp)),
                            horizontalAlignment = Alignment.CenterHorizontally,
                            verticalArrangement = Arrangement.Center
                        ) {
                            // Spotsize (HandPiece) - Title
                            Row(
                                modifier = Modifier
                                    .fillMaxSize()
                                    .weight(1f)
                                    .clip(RoundedCornerShape(topStart = 4.px.dp))
                                    .background(
                                        brush = Brush.linearGradient(
                                            colors = listOf(
                                                Color(47, 39, 14),
                                                Color(34, 33, 31),
                                            )
                                        )
                                    )
                                    .innerShadow(
                                        shape = RoundedCornerShape(topStart = 4.px.dp),
                                        color = Color(255, 255, 255).copy(alpha = 0.15f),
                                        blur = 4.px.dp,
                                        offsetY = 4.px.dp,
                                        offsetX = 0.px.dp,
                                        spread = 0.px.dp
                                    ),
                                horizontalArrangement = Arrangement.Center,
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                Text(
                                    text = "Spotsize(mm)",
                                    style = RobotoTypography.bodyMedium,
                                    fontSize = 14.px.sp,
                                    lineHeight = 20.px.sp,
                                    letterSpacing = 0.25.px.sp,
                                    fontWeight = FontWeight.Normal,
                                    color = Color.White,
                                    textAlign = TextAlign.Center
                                )
                            }

                            // // Spotsize (HandPiece) - Value
                            Row(
                                modifier = Modifier
                                    .fillMaxSize()
                                    .weight(1f)
                                    .clip(RoundedCornerShape(bottomStart = 4.px.dp))
                                    .background(
                                        brush = Brush.linearGradient(
                                            colors = listOf(
                                                Color(255, 255, 255),
                                                Color(248, 245, 238),
                                            )
                                        )
                                    ),
                                horizontalArrangement = Arrangement.Center,
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                Text(
                                    text = handPieceTitle,
                                    style = RobotoTypography.bodyMedium,
                                    fontSize = 14.px.sp,
                                    lineHeight = 20.px.sp,
                                    letterSpacing = 0.25.px.sp,
                                    fontWeight = FontWeight.Normal,
                                    color = Color.Black,
                                    textAlign = TextAlign.Center
                                )
                            }
                        }

                        VerticalDivider(
                            color = Color(228,228,231),
                            thickness = 1.px.dp,
                            modifier = Modifier
                                .fillMaxHeight()
                                .width(1.px.dp)
                        )

                        // Pulse Duration (ms)
                        Column(
                            modifier = Modifier
                                .fillMaxSize()
                                .weight(1f),
                            horizontalAlignment = Alignment.CenterHorizontally,
                            verticalArrangement = Arrangement.Center
                        ) {
                            // Pulse Duration - Title
                            Row(
                                modifier = Modifier
                                    .fillMaxSize()
                                    .weight(1f)
                                    .background(
                                        brush = Brush.linearGradient(
                                            colors = listOf(
                                                Color(47, 39, 14),
                                                Color(34, 33, 31),
                                            )
                                        )
                                    )
                                    .innerShadow(
                                        shape = RectangleShape,
                                        color = Color(255, 255, 255).copy(alpha = 0.15f),
                                        blur = 4.px.dp,
                                        offsetY = 4.px.dp,
                                        offsetX = 0.px.dp,
                                        spread = 0.px.dp
                                    ),
                                horizontalArrangement = Arrangement.Center,
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                Text(
                                    text = "Fluence(J/cm²)",
                                    style = RobotoTypography.bodyMedium,
                                    fontSize = 14.px.sp,
                                    lineHeight = 20.px.sp,
                                    letterSpacing = 0.25.px.sp,
                                    fontWeight = FontWeight.Normal,
                                    color = Color.White,
                                    textAlign = TextAlign.Center
                                )
                            }

                            // Pulse Duration - Value
                            Row(
                                modifier = Modifier
                                    .fillMaxSize()
                                    .weight(1f)
                                    .background(
                                        brush = Brush.linearGradient(
                                            colors = listOf(
                                                Color(255, 255, 255),
                                                Color(248, 245, 238),
                                            )
                                        )
                                    ),
                                horizontalArrangement = Arrangement.Center,
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                Text(
                                    text = pulseWidth.toString(),
                                    style = RobotoTypography.bodyMedium,
                                    fontSize = 14.px.sp,
                                    lineHeight = 20.px.sp,
                                    letterSpacing = 0.25.px.sp,
                                    fontWeight = FontWeight.Normal,
                                    color = Color.Black,
                                    textAlign = TextAlign.Center
                                )
                            }
                        }

                        VerticalDivider(
                            color = Color(228,228,231),
                            thickness = 1.px.dp,
                            modifier = Modifier
                                .fillMaxHeight()
                                .width(1.px.dp)
                        )

                        // Fluence
                        Column(
                            modifier = Modifier
                                .fillMaxSize()
                                .weight(1f),
                            horizontalAlignment = Alignment.CenterHorizontally,
                            verticalArrangement = Arrangement.Center
                        ) {
                            // Fluence - Title
                            Row(
                                modifier = Modifier
                                    .fillMaxSize()
                                    .weight(1f)
                                    .background(
                                        brush = Brush.linearGradient(
                                            colors = listOf(
                                                Color(47, 39, 14),
                                                Color(34, 33, 31),
                                            )
                                        )
                                    )
                                    .innerShadow(
                                        shape = RoundedCornerShape(topEnd = 4.px.dp),
                                        color = Color(255, 255, 255).copy(alpha = 0.15f),
                                        blur = 4.px.dp,
                                        offsetY = 4.px.dp,
                                        offsetX = 0.px.dp,
                                        spread = 0.px.dp
                                    ),
                                horizontalArrangement = Arrangement.Center,
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                Text(
                                    text = "Fluence(J/cm²)",
                                    style = RobotoTypography.bodyMedium,
                                    fontSize = 14.px.sp,
                                    lineHeight = 20.px.sp,
                                    letterSpacing = 0.25.px.sp,
                                    fontWeight = FontWeight.Normal,
                                    color = Color.White,
                                    textAlign = TextAlign.Center
                                )
                            }

                            // Fluence - Value
                            Row(
                                modifier = Modifier
                                    .fillMaxSize()
                                    .weight(1f)
                                    .clip(RoundedCornerShape(bottomStart = 4.px.dp))
                                    .background(
                                        brush = Brush.linearGradient(
                                            colors = listOf(
                                                Color(255, 255, 255),
                                                Color(248, 245, 238),
                                            )
                                        )
                                    ),
                                horizontalArrangement = Arrangement.Center,
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                Text(
                                    text = fluence.toString(),
                                    style = RobotoTypography.bodyMedium,
                                    fontSize = 14.px.sp,
                                    lineHeight = 20.px.sp,
                                    letterSpacing = 0.25.px.sp,
                                    fontWeight = FontWeight.Normal,
                                    color = Color.Black,
                                    textAlign = TextAlign.Center
                                )
                            }
                        }

                        VerticalDivider(
                            color = Color(228,228,231),
                            thickness = 1.px.dp,
                            modifier = Modifier
                                .fillMaxHeight()
                                .width(1.px.dp)
                        )

                        // Repetition (Hz)
                        Column(
                            modifier = Modifier
                                .fillMaxSize()
                                .weight(1f)
                                .clip(RoundedCornerShape(topEnd = 4.px.dp, bottomEnd = 4.px.dp)),
                            horizontalAlignment = Alignment.CenterHorizontally,
                            verticalArrangement = Arrangement.Center
                        ) {
                            // Repetition - Title
                            Row(
                                modifier = Modifier
                                    .fillMaxSize()
                                    .weight(1f)
                                    .clip(RoundedCornerShape(bottomStart = 4.px.dp))
                                    .background(
                                        brush = Brush.linearGradient(
                                            colors = listOf(
                                                Color(47, 39, 14),
                                                Color(34, 33, 31),
                                            )
                                        )
                                    )
                                    .innerShadow(
                                        shape = RectangleShape,
                                        color = Color(255, 255, 255).copy(alpha = 0.15f),
                                        blur = 4.px.dp,
                                        offsetY = 4.px.dp,
                                        offsetX = 0.px.dp,
                                        spread = 0.px.dp
                                    ),
                                horizontalArrangement = Arrangement.Center,
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                Text(
                                    text = "Repetition(Hz)",
                                    style = RobotoTypography.bodyMedium,
                                    fontSize = 14.px.sp,
                                    lineHeight = 20.px.sp,
                                    letterSpacing = 0.25.px.sp,
                                    fontWeight = FontWeight.Normal,
                                    color = Color.White,
                                    textAlign = TextAlign.Center
                                )
                            }

                            // Repetition - Value
                            Row(
                                modifier = Modifier
                                    .fillMaxSize()
                                    .weight(1f)
                                    .clip(RoundedCornerShape(bottomEnd = 4.px.dp))
                                    .background(
                                        brush = Brush.linearGradient(
                                            colors = listOf(
                                                Color(255, 255, 255),
                                                Color(248, 245, 238),
                                            )
                                        )
                                    ),
                                horizontalArrangement = Arrangement.Center,
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                Text(
                                    text = repetition.toString(),
                                    style = RobotoTypography.bodyMedium,
                                    fontSize = 14.px.sp,
                                    lineHeight = 20.px.sp,
                                    letterSpacing = 0.25.px.sp,
                                    fontWeight = FontWeight.Normal,
                                    color = Color.Black,
                                    textAlign = TextAlign.Center
                                )
                            }
                        }
                    }

                    Spacer(modifier = Modifier.height(20.px.dp))

                    // EditBox
                    Row(
                        modifier = Modifier
                            .size(585.px.dp, 56.px.dp)
                            .clip(RoundedCornerShape(4.px.dp))
                            .background(Color(245, 245, 245))
                    ) {
                        OutlinedTextField(
                            value = presetName,
                            onValueChange = { newText ->
                                presetName = newText
                            },
                            keyboardOptions = KeyboardOptions(
                                keyboardType = KeyboardType.Password, //KeyboardType.Text,
                                imeAction = ImeAction.Done,
                                //autoCorrect = false
                            ),
                            placeholder = { Text("Name") },
                            singleLine = true,
                            keyboardActions = KeyboardActions(onDone = {
                                Timber.d("onDone - Save")
                                performOkClick()
                            }),
                            textStyle = robotoTextStyle.copy(
                                fontSize = 18.px.sp,
                                color = Color.Black
                            ),
                            modifier = Modifier
                                .fillMaxSize()
                                .focusRequester(focusRequester),
                        )

                        LaunchedEffect(Unit) {
                            Timber.d("focusRequester.requestFocus()")
                            focusRequester.requestFocus()
                        }

                        DisposableEffect(Unit) {
                            onDispose {
                                Timber.d("MyComposable is being disposed!")
                                focusManager.clearFocus(force = true) // Hide the keyboard
                            }
                        }
                    }

                    // Preset Buttons, Cancel & OK
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(88.px.dp)
                            .padding(start = 10.px.dp, end = 10.px.dp),
                        horizontalArrangement = Arrangement.Center,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        // Preset Buttons
                        for (i in 1..5) {
                            PresetButton(
                                modifier = Modifier.size(33.px.dp),
                                fontSize = 18,
                                no = i,
                                selected = selectedPriority,
                                //enabled = true,
                                onClick = {
                                    Timber.d("onClick PresetButton - $i")
                                    // Toggle selection if select again
                                    if (i == selectedPriority) {
                                        selectedPriority = 0
                                    } else {
                                        selectedPriority = i
                                    }
                                }
                            )
                            Spacer(Modifier.width(10.px.dp))
                        }

                        // Spacer
                        Spacer(modifier = Modifier.weight(1f))

                        // Cancel
                        TextButton(
                            modifier = Modifier
                                .size(68.px.dp, 40.px.dp),
                            onClick = {
                                Timber.d("onClick - Cancel")
                                onClick(false)
                            }
                        ) {
                            Text(
                                text = stringResource(R.string.cancel),
                                style = RobotoTypography.labelLarge,
                                fontWeight = FontWeight.Normal,
                                fontSize = 14.px.sp,
                                color = Color.Black
                            )
                        }

                        // Ok
                        TextButton(
                            modifier = Modifier
                                .size(68.px.dp, 40.px.dp),
                            onClick = {
                                Timber.d("onClick - Save")
                                performOkClick()
                            }
                        ) {
                            Text(
                                text = stringResource(R.string.save),
                                style = RobotoTypography.labelLarge,
                                fontWeight = FontWeight.Normal,
                                fontSize = 14.px.sp,
                                color = Color.Black
                            )
                        }
                    }

                }
            }

            /*
            Spacer(modifier = Modifier.height(10.px.dp))

            if (toastMessage.isNotEmpty()) {
                Text(
                    text = toastMessage,
                    style = RobotoTypography.labelLarge,
                    fontWeight = FontWeight.Normal,
                    fontSize = 14.px.sp,
                    color = Color.White
                )
            }
            */
        }
    }
}


@Preview(
    apiLevel = 34,
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false,
    device = "spec:width=1920dp,height=1080dp,dpi=150,isRound=false,chinSize=0dp,orientation=landscape"
)
@Composable
fun PreviewPresetSavePopup(
    mainViewModel:MainViewModel = MainViewModel(
        preferenceRepository = PreferenceRepository(LocalContext.current),
        serialPortRepository = SerialPortRepository.createWithFakeRepository(),
        databaseRepository = DatabaseRepository(RamanDatabaseService(RamanDatabase.getInstance(LocalContext.current))),
        dispatcherProvider = DefaultDispatcherProvider(),
        applicationContext = LocalContext.current
    )
) {
    PresetSavePopup(
        mainViewModel = mainViewModel
    )
}