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

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
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.AccelViewMode
import llc.arma.ble.domain.usecase.AccelViewMode.*

val AccelViewMode.localized: String
    get() {
        return when(this){
            ACCELERATION -> "Ускорение"
            PEAK_ACCELERATION -> "Пиковое ускорение"
            RMS -> "Среднеквадратичное ускорение"
            VIBRATION -> "Вибрация"
            ANGLE -> "Угол"
        }
    }

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

    var value by remember(state.accelViewMode) {
        mutableStateOf(state.accelViewMode)
    }

    Column(
        modifier = Modifier
    ) {

        Text(
            modifier = Modifier.padding(horizontal = 12.dp),
            text = "Accel view mode",
            style = MaterialTheme.typography.titleLarge
        )

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

        values().forEach {

            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .fillMaxWidth()
                    .clip(RoundedCornerShape(8.dp))
                    .clickable { value = it }
                    .padding(4.dp)
            ) {

                RadioButton(
                    selected = it == value,
                    onClick = {
                        value = it

                    }
                )

                Text(text = it.localized)

            }

        }

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

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

                when(next){
                    AccelerometerContract.Event.Next.ACCEL -> {
                        onEvent(AccelerometerContract.Event.OnAccelViewModelChanged(value))
                        onEvent(AccelerometerContract.Event.OnAccelEdit)
                    }
                    AccelerometerContract.Event.Next.SPECTRE -> {
                        onEvent(AccelerometerContract.Event.OnAccelViewModelChanged(value))
                        onEvent(AccelerometerContract.Event.OnSpectreEdit)
                    }

                    AccelerometerContract.Event.Next.HISTORY -> {
                        onEvent(AccelerometerContract.Event.OnHistoryViewModeChanged(value))
                        onEvent(AccelerometerContract.Event.OnHistoryEdit)
                    }
                }

            }
        ) {

            Box(modifier = Modifier.fillMaxSize()) {

                Text(
                    modifier = Modifier.align(Alignment.Center),
                    color = MaterialTheme.colorScheme.background,
                    style = MaterialTheme.typography.labelLarge,
                    text = "Ок"
                )

            }

        }

    }

}