package llc.arma.ble.app.ui.screen.inspection.host.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.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.painter.Painter
import androidx.compose.ui.graphics.vector.rememberVectorPainter
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.host.HostContract
import llc.arma.ble.domain.model.Ble

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

    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 = {

                    BleMenuItem(
                        title = "Мощность",
                        subtitle = "${ble.state.tx.value} db",
                        icon = rememberVectorPainter(Icons.Rounded.KeyboardArrowDown)
                    ) {
                        onEvent(HostContract.Event.OnPowerEdit)
                    }

                    var hours =
                       ble.hostState.historyInterval / millisInHour
                    var minutes =
                       (ble.hostState.historyInterval - (hours * millisInHour)) / millisInMinute
                    var seconds =
                        (ble.hostState.historyInterval - (hours * millisInHour) - (minutes * millisInMinute)) / millisInSecond

                    BleMenuItem(
                        title = "Интервал измерений",
                        subtitle = "$hours ч. $minutes мин. $seconds сек.",
                        icon = rememberVectorPainter(Icons.Rounded.KeyboardArrowDown)
                    ) {
                        onEvent(HostContract.Event.OnShowIntervalEdit)
                    }

                    hours = ble.hostState.readInterval / millisInHour
                    minutes =
                        (ble.hostState.readInterval - (hours * millisInHour)) / millisInMinute
                    seconds =
                        (ble.hostState.readInterval - (hours * millisInHour) - (minutes * millisInMinute)) / millisInSecond

                    BleMenuItem(
                        title = "Интервал чтения",
                        subtitle = "$hours ч. $minutes мин. $seconds сек.",
                        icon = rememberVectorPainter(Icons.Rounded.KeyboardArrowDown)
                    ) {
                        onEvent(HostContract.Event.OnShowReadIntervalEdit)
                    }

                    BleMenuItem(
                        title = "График измерений",
                        icon = rememberVectorPainter(Icons.Rounded.KeyboardArrowRight)
                    ) {
                        onEvent(HostContract.Event.OnShowHostHistory)
                    }

                    BleMenuItem(
                        title = "Таблица BLE ID",
                        icon = rememberVectorPainter(Icons.Rounded.KeyboardArrowRight)
                    ) {
                        onEvent(HostContract.Event.OnShowHostBleTable)
                    }

                    BleMenuItem(
                        title = "Изменить пароль",
                        icon = rememberVectorPainter(Icons.Rounded.KeyboardArrowRight)
                    ) {
                        onEvent(HostContract.Event.OnChangePassword)
                    }

                }
            )

        }

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

            Box(modifier = Modifier.fillMaxSize()) {

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

            }

        }

    }

}

@Composable
fun BleMenuItem(
    title: String,
    subtitle: String? = null,
    icon: Painter,
    onClick: () -> Unit
){

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

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

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

                Text(
                    text = title
                )

                subtitle?.let {
                    Text(
                        color = MaterialTheme.colorScheme.secondary,
                        style = MaterialTheme.typography.bodyMedium,
                        text = it
                    )
                }

            }

            Icon(
                painter = icon,
                contentDescription = null
            )

        }

    }

}