package llc.arma.ble.app.ui.screen

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.BatteryFull
import androidx.compose.material.icons.rounded.Key
import androidx.compose.material.icons.rounded.NetworkCell
import androidx.compose.material.icons.rounded.ShortText
import androidx.compose.material3.Divider
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.unit.dp
import llc.arma.ble.app.ui.screen.ble.icon
import llc.arma.ble.app.ui.screen.ble.localized
import llc.arma.ble.domain.model.BleInfo

@Composable
fun BleInfoView(
    bleInfo: BleInfo
) {

    Surface(
        modifier = Modifier.padding(bottom = 16.dp),
        shape = RoundedCornerShape(24.dp),
        color = MaterialTheme.colorScheme.surfaceVariant
    ) {

        Column(
            modifier = Modifier.padding(8.dp)
        ) {

            Column() {

                BleInfoItem(
                    icon = {
                        Icon(
                            imageVector = bleInfo.type.icon,
                            contentDescription = null
                        )
                    },
                    title = "Тип метки",
                    subtitle = bleInfo.type.localized
                )

                SpecDivider()

                BleInfoItem(
                    icon = {
                        Icon(
                            imageVector = Icons.Rounded.ShortText,
                            contentDescription = null
                        )
                    },
                    title = "Наименование",
                    subtitle = bleInfo.name
                )

                SpecDivider()

                BleInfoItem(
                    icon = {
                        Icon(
                            imageVector = Icons.Rounded.Key,
                            contentDescription = null
                        )
                    },
                    title = "Адрес",
                    subtitle = bleInfo.serial
                )

                SpecDivider()

                BleInfoItem(
                    icon = {
                        Icon(
                            imageVector = Icons.Rounded.BatteryFull,
                            contentDescription = null
                        )
                    },
                    title = "Заряд батареи",
                    subtitle = "${bleInfo.batteryLevel} %"
                )

                SpecDivider()

                BleInfoItem(
                    icon = {
                        Icon(
                            imageVector = Icons.Rounded.NetworkCell,
                            contentDescription = null
                        )
                    },
                    title = "Мощность сигнала",
                    subtitle = if(bleInfo.rssi != null) "${bleInfo.rssi } dBm" else "Нет сигнала"
                )

            }

        }

    }
    
}

@Composable
private fun ColumnScope.SpecDivider(){

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

    Divider()

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

}

@Composable
private fun BleInfoItem(
    icon: @Composable () -> Unit,
    title: String,
    subtitle: String
){

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

        Surface(
            modifier = Modifier.size(40.dp),
            shape = CircleShape
        ) {

            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ){

                icon()

            }

        }

        Spacer(modifier = Modifier.width(12.dp))

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

            Text(
                text = title
            )
            Text(
                color = MaterialTheme.colorScheme.secondary,
                style = MaterialTheme.typography.bodyMedium,
                text = subtitle
            )

        }

    }

}