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

import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.Image
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.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
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.graphics.StrokeCap
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import llc.arma.ble.R
import llc.arma.ble.app.ui.screen.inspection.host.HostContract
import llc.arma.ble.app.ui.screen.inspection.thermometer.localizedName

@Composable
fun Write(
    state: HostContract.State.Display.WriteState,
    onEvent: (HostContract.Event) -> Unit
) {

    Column(
        modifier = Modifier.animateContentSize()
    ) {

        Text(
            modifier = Modifier.padding(horizontal = 12.dp),
            text = "Запись изменений",
            style = MaterialTheme.typography.titleLarge
        )

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

        when (state) {
            is HostContract.State.Display.WriteState.DisplayPreview -> {

                if(state.writeRequest.tx != null || state.writeRequest.interval != null || state.writeRequest.readInterval !== null) {

                    state.writeRequest.tx?.let {
                        Box(
                            modifier = Modifier.padding(
                                vertical = 0.dp,
                                horizontal = 8.dp
                            )
                        ) {

                            Row(
                                verticalAlignment = Alignment.CenterVertically,
                                modifier = Modifier
                                    .clip(RoundedCornerShape(16.dp))
                                    .padding(8.dp)
                            ) {

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

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

                                }

                            }

                        }
                    }

                    state.writeRequest.interval?.let {

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

                            Row(
                                verticalAlignment = Alignment.CenterVertically,
                                modifier = Modifier
                                    .clip(RoundedCornerShape(16.dp))
                                    .padding(8.dp)
                            ) {

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

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

                                    val hours = it / millisInHour
                                    val minutes = (it - (hours * millisInHour)) / millisInMinute
                                    val seconds = (it - (hours * millisInHour) - (minutes * millisInMinute)) / millisInSecond

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

                                }

                            }

                        }

                    }

                    state.writeRequest.readInterval?.let {

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

                            Row(
                                verticalAlignment = Alignment.CenterVertically,
                                modifier = Modifier
                                    .clip(RoundedCornerShape(16.dp))
                                    .padding(8.dp)
                            ) {

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

                                    Text(
                                        text = "Интервал чтения"
                                    )

                                    val hours = it / millisInHour
                                    val minutes = (it - (hours * millisInHour)) / millisInMinute
                                    val seconds = (it - (hours * millisInHour) - (minutes * millisInMinute)) / millisInSecond

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

                                }

                            }

                        }

                    }

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

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

                        Box(modifier = Modifier.fillMaxSize()) {

                            Text(
                                modifier = Modifier.align(Alignment.Center),
                                color = MaterialTheme.colorScheme.background,
                                style = MaterialTheme.typography.labelLarge,
                                text = "Записать"
                            )

                        }

                    }

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

                        Box(modifier = Modifier.fillMaxSize()) {

                            Text(
                                modifier = Modifier.align(Alignment.Center),
                                color = MaterialTheme.colorScheme.onSurfaceVariant,
                                style = MaterialTheme.typography.labelLarge,
                                text = "Отменить"
                            )

                        }

                    }

                } else {

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

                    Text(
                        text = "Нет изменений",
                        modifier = Modifier
                            .align(Alignment.CenterHorizontally)
                    )

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

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

                        Box(modifier = Modifier.fillMaxSize()) {

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

                        }

                    }

                }


            }
            is HostContract.State.Display.WriteState.Writing -> {

                Box {

                    Column() {

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

                        CircularProgressIndicator(
                            strokeCap = StrokeCap.Round,
                            modifier = Modifier
                                .align(Alignment.CenterHorizontally)
                        )

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

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

                            Box(modifier = Modifier.fillMaxSize()) {

                                Text(
                                    modifier = Modifier.align(Alignment.Center),
                                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                                    style = MaterialTheme.typography.labelLarge,
                                    text = "Отменить"
                                )

                            }

                        }

                    }

                }

            }
            HostContract.State.Display.WriteState.Success -> {

                Box {

                    Column {

                        Box(
                            modifier = Modifier
                                .padding(8.dp)
                                .fillMaxWidth()
                        ) {

                            Image(
                                modifier = Modifier
                                    .size(125.dp)
                                    .align(Alignment.Center),
                                painter = painterResource(R.drawable.ic_done),
                                contentDescription = null
                            )

                        }

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

                        Text(
                            modifier = Modifier.align(Alignment.CenterHorizontally),
                            text = "Успешно завершено"
                        )

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

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

                            Box(modifier = Modifier.fillMaxSize()) {

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

                            }

                        }

                    }

                }

            }
            HostContract.State.Display.WriteState.Failure -> {

                Box {

                    Column {

                        Box(
                            modifier = Modifier
                                .padding(8.dp)
                                .fillMaxWidth()
                        ) {

                            Image(
                                modifier = Modifier
                                    .size(125.dp)
                                    .align(Alignment.Center),
                                painter = painterResource(R.drawable.ic_error),
                                contentDescription = null
                            )

                        }

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

                        Text(
                            modifier = Modifier.align(Alignment.CenterHorizontally),
                            text = "Ошибка записи"
                        )

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

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

                            Box(modifier = Modifier.fillMaxSize()) {

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

                            }

                        }

                    }

                }

            }
        }

    }

}