package llc.arma.ble.app.ui.screen.inspection.thermometer.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.thermometer.ThermometerContract
import llc.arma.ble.domain.model.Ble

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

    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(ThermometerContract.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 = "Температура"
                                )

                                Text(
                                    color = MaterialTheme.colorScheme.secondary,
                                    style = MaterialTheme.typography.bodyMedium,
                                    text = "${ble.thermometerState.temperature.value} °C"
                                )

                            }

                        }

                    }

                    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 = "Сохранять историю измерений"
                                )

                            }

                            Switch(
                                checked = ble.thermometerState.saveHistory,
                                onCheckedChange = {
                                    onEvent(ThermometerContract.Event.OnSaveHistoryChanged(it))
                                }
                            )

                        }

                    }

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

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

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

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

                                val hours = ble.thermometerState.historyInterval / 1000 / 60 / 60
                                val minutes = (ble.thermometerState.historyInterval - ( hours * 1000 * 60 * 60 )) / 1000 / 60

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

                            }

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

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

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

                            }

                            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(ThermometerContract.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(ThermometerContract.Event.OnShowWriteBlePreview)
            }
        ) {

            Box(modifier = Modifier.fillMaxSize()) {

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

            }

        }

    }

}