package llc.arma.ble.app.ui.screen.inspection.accelerometer.view

import androidx.compose.foundation.clickable
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.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.KeyboardArrowDown
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import llc.arma.ble.app.ui.screen.inspection.accelerometer.AccelerometerContract
import llc.arma.ble.domain.usecase.AccelScale
import llc.arma.ble.domain.usecase.FftAxis
import llc.arma.ble.domain.usecase.FftFrequency
import llc.arma.ble.domain.usecase.FftViewMode

val FftFrequency.localized: String
    get() {
        return when(this){
            FftFrequency.OFF -> "откл"
            FftFrequency.F_1 -> "1 Гц"
            FftFrequency.F_10 -> "10 Гц"
            FftFrequency.F_25 -> "25 Гц"
            FftFrequency.F_50 -> "50 Гц"
            FftFrequency.F_100 -> "100 Гц"
            FftFrequency.F_200 -> "200 Гц"
            FftFrequency.F_400 -> "400 Гц"
            FftFrequency.F_1620 -> "1620 Гц"
            FftFrequency.F_1344 -> "1344 Гц"
        }
    }

val FftAxis.localized: String
    get() {
        return when(this){
            FftAxis.AUTO -> "Авто"
            FftAxis.X -> "Ось X"
            FftAxis.Y -> "Ось Y"
            FftAxis.Z -> "Ось Z"
        }
    }

val FftViewMode.localized: String
    get() {
        return when(this){
            FftViewMode.SPECTRE -> "Спектр"
            FftViewMode.X -> "Ось X"
            FftViewMode.Y -> "Ось Y"
            FftViewMode.Z -> "Ось Z"
        }
    }

val AccelScale.localized: String
    get() {
        return when(this){
            AccelScale.S_2 -> "2g"
            AccelScale.S_4 -> "4g"
            AccelScale.S_8 -> "8g"
            AccelScale.S_16 -> "16g"
        }
    }

@Composable
fun AccelEdit(
    state: AccelerometerContract.State.Display,
    onEvent: (AccelerometerContract.Event) -> Unit,
){

    val accelMode = state.accelRealtimeViewMode
    val fftMode = state.fftViewMode
    val fftAxis = state.fftAxis
    val fftFrequency = state.fftFrequency
    val accelScale = state.accelScale

    Column(
        modifier = Modifier
    ) {

        Text(
            modifier = Modifier.padding(horizontal = 12.dp),
            text = "Параметры",
            style = MaterialTheme.typography.titleLarge
        )

        Spacer(modifier = Modifier.height(16.dp))

        Column(
            modifier = Modifier
        ) {

            Box(
                modifier = Modifier.padding(
                    vertical = 8.dp,
                    horizontal = 8.dp
                )
            ) {

                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier
                        .clip(RoundedCornerShape(16.dp))
                        .clickable {
                            onEvent(AccelerometerContract.Event.OnRealtimeViewModeEdit)
                        }
                        .padding(8.dp)
                ) {

                    Column(
                        modifier = Modifier.weight(1f)
                    ) {

                        Text(
                            text = "Измеряемый параметр"
                        )
                        Text(
                            color = MaterialTheme.colorScheme.secondary,
                            style = MaterialTheme.typography.bodyMedium,
                            text = accelMode.localized
                        )

                    }

                    Icon(
                        imageVector = Icons.Rounded.KeyboardArrowDown,
                        contentDescription = null
                    )

                }

            }

            if(accelMode is RealtimeViewMode.Spectre){

                Box(
                    modifier = Modifier.padding(
                        vertical = 8.dp,
                        horizontal = 8.dp
                    )
                ) {

                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier
                            .clip(RoundedCornerShape(16.dp))
                            .clickable {
                                onEvent(AccelerometerContract.Event.OnFftModeEdit)
                            }
                            .padding(8.dp)
                    ) {

                        Column(
                            modifier = Modifier.weight(1f)
                        ) {

                            Text(
                                text = "Режим просмотра"
                            )
                            Text(
                                color = MaterialTheme.colorScheme.secondary,
                                style = MaterialTheme.typography.bodyMedium,
                                text = fftMode.localized
                            )

                        }

                        Icon(
                            imageVector = Icons.Rounded.KeyboardArrowDown,
                            contentDescription = null
                        )

                    }

                }

                Box(
                    modifier = Modifier.padding(
                        vertical = 8.dp,
                        horizontal = 8.dp
                    )
                ) {

                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier
                            .clip(RoundedCornerShape(16.dp))
                            .clickable { onEvent(AccelerometerContract.Event.OnFftAxisEdit) }
                            .padding(8.dp)
                    ) {

                        Column(
                            modifier = Modifier.weight(1f)
                        ) {

                            Text(
                                text = "Ось"
                            )
                            Text(
                                color = MaterialTheme.colorScheme.secondary,
                                style = MaterialTheme.typography.bodyMedium,
                                text = fftAxis.localized
                            )

                        }

                        Icon(
                            imageVector = Icons.Rounded.KeyboardArrowDown,
                            contentDescription = null
                        )

                    }

                }

                Box(
                    modifier = Modifier.padding(
                        vertical = 8.dp,
                        horizontal = 8.dp
                    )
                ) {

                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier
                            .clip(RoundedCornerShape(16.dp))
                            .clickable {
                                onEvent(AccelerometerContract.Event.OnFftFrequencyEdit)
                            }
                            .padding(8.dp)
                    ) {

                        Column(
                            modifier = Modifier.weight(1f)
                        ) {

                            Text(
                                text = "Частота дистретизации"
                            )
                            Text(
                                color = MaterialTheme.colorScheme.secondary,
                                style = MaterialTheme.typography.bodyMedium,
                                text = fftFrequency.localized
                            )

                        }

                        Icon(
                            imageVector = Icons.Rounded.KeyboardArrowDown,
                            contentDescription = null
                        )

                    }

                }

            }

            Box(
                modifier = Modifier.padding(
                    vertical = 8.dp,
                    horizontal = 8.dp
                )
            ) {

                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier
                        .clip(RoundedCornerShape(16.dp))
                        .clickable {
                            onEvent(AccelerometerContract.Event.OnAccelScaleEdit(next = AccelerometerContract.Event.Next.ACCEL))
                        }
                        .padding(8.dp)
                ) {

                    Column(
                        modifier = Modifier.weight(1f)
                    ) {

                        Text(
                            text = "Масштаб"
                        )
                        Text(
                            color = MaterialTheme.colorScheme.secondary,
                            style = MaterialTheme.typography.bodyMedium,
                            text = accelScale.localized
                        )

                    }

                    Icon(
                        imageVector = Icons.Rounded.KeyboardArrowDown,
                        contentDescription = null
                    )

                }

            }

        }

        Spacer(modifier = Modifier.height(16.dp))

        Surface(
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp)
                .height(50.dp),
            shape = CircleShape,
            color = MaterialTheme.colorScheme.primaryContainer,
            onClick = {
                onEvent(AccelerometerContract.Event.OnShowAccelerometerAccel)
            }
        ) {

            Box(modifier = Modifier.fillMaxSize()) {

                Text(
                    modifier = Modifier.align(Alignment.Center),
                    color = MaterialTheme.colorScheme.background,
                    style = MaterialTheme.typography.labelLarge,
                    text = "Продолжить"
                )

            }

        }

    }

}