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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.KeyboardArrowDown
import androidx.compose.material.icons.rounded.KeyboardArrowRight
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
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.model.BleView
import llc.arma.ble.app.ui.screen.BleInfoView
import llc.arma.ble.app.ui.screen.inspection.accelerometer.AccelerometerContract
import llc.arma.ble.domain.model.Ble

@Composable
fun DisplayState(
    onEvent: (AccelerometerContract.Event) -> Unit,
    origin: Ble.Accelerometer,
    ble: BleView.Accelerometer
) {

    Column() {

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

            Box(
                modifier = Modifier.padding(
                    vertical = 8.dp,
                    horizontal = 8.dp
                )
            ) {
                BleInfoView(bleInfo = origin.info)
            }

            Column(
                modifier = Modifier,
                content = {

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

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

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

                                Text(
                                    text = "Мощность"
                                )
                                Text(
                                    color = MaterialTheme.colorScheme.secondary,
                                    style = MaterialTheme.typography.bodyMedium,
                                    text = "${ble.state.tx.value} db"
                                )

                            }

                            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 { }
                                .padding(8.dp)
                        ) {

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

                                Text(
                                    text = "Сохранять историю измерений"
                                )

                                val history = ble.accelerometerState.saveHistory

                                if(history is Ble.Accelerometer.HistorySettings.Enabled){
                                    Text(
                                        color = MaterialTheme.colorScheme.secondary,
                                        style = MaterialTheme.typography.bodyMedium,
                                        text = "View mode ${history.mode.localized}"
                                    )

                                    Text(
                                        color = MaterialTheme.colorScheme.secondary,
                                        style = MaterialTheme.typography.bodyMedium,
                                        text = "Scale: ${history.scale.localized}"
                                    )

                                }

                            }

                            Switch(
                                checked = ble.accelerometerState.saveHistory is Ble.Accelerometer.HistorySettings.Enabled,
                                onCheckedChange = {
                                    onEvent(AccelerometerContract.Event.OnSaveHistoryChanged(it))
                                }
                            )

                        }

                    }

                    if(ble.accelerometerState.saveHistory is Ble.Accelerometer.HistorySettings.Enabled) {

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

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

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

                                    Text(
                                        text = "Интервал измерений"
                                    )

                                    val hours =
                                        ble.accelerometerState.historyInterval / millisInHour
                                    val minutes =
                                        (ble.accelerometerState.historyInterval - (hours * millisInHour)) / millisInMinute
                                    val seconds =
                                        (ble.accelerometerState.historyInterval - (hours * millisInHour) - (minutes * millisInMinute)) / millisInSecond

                                    Text(
                                        color = MaterialTheme.colorScheme.secondary,
                                        style = MaterialTheme.typography.bodyMedium,
                                        text = "$hours ч. $minutes мин. $seconds сек."
                                    )

                                }

                                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 {

                                    when(origin.accelerometerState.saveHistorySettings){
                                        is Ble.Accelerometer.HistorySettings.Disabled ->
                                            onEvent(AccelerometerContract.Event.OnAccelEdit)
                                        is Ble.Accelerometer.HistorySettings.Enabled ->
                                            onEvent(AccelerometerContract.Event.OnShowAccelerometerHistory)
                                    }
                                }
                                .padding(8.dp)
                        ) {

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

                                Text(
                                    text = "График измерений"
                                )

                                /*Text(
                                    color = MaterialTheme.colorScheme.secondary,
                                    style = MaterialTheme.typography.bodyMedium,
                                    text = when(origin.accelerometerState.saveHistorySettings){
                                        Ble.Accelerometer.HistorySettings.Disabled -> "Текущие измерения"
                                        is Ble.Accelerometer.HistorySettings.Enabled -> ""
                                    }
                                )*/

                            }

                            Icon(
                                imageVector = Icons.Rounded.KeyboardArrowRight,
                                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.OnChangePassword)
                                }
                                .padding(8.dp)
                        ) {

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

                                Text(
                                    text = "Изменить пароль"
                                )

                            }

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

                        }

                    }

                }
            )

        }

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

            Box(modifier = Modifier.fillMaxSize()) {

                Text(
                    modifier = Modifier.align(Alignment.Center),
                    color = MaterialTheme.colorScheme.background,
                    style = MaterialTheme.typography.labelLarge,
                    text = "Сохранить"
                )

            }

        }

    }

}