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

import androidx.compose.animation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.KeyboardArrowDown
import androidx.compose.material.icons.rounded.KeyboardArrowUp
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import llc.arma.ble.app.ui.model.BleView
import llc.arma.ble.app.ui.screen.inspection.accelerometer.AccelerometerContract
import llc.arma.ble.app.ui.screen.inspection.thermometer.ThermometerContract

@Composable
fun IntervalEdit(
    state: BleView.Accelerometer,
    onEvent: (AccelerometerContract.Event) -> Unit,
){

    var value by remember(state.accelerometerState.historyInterval) {
        mutableIntStateOf((state.accelerometerState.historyInterval).toInt())
    }

    val maxInterval = 10 * 24 * 60 * 60 * 1000

    if(value > maxInterval){
        value = maxInterval
    }

    if(value < 1){
        value = 1 * 60 * 1000
    }

    val maxMinutes = maxInterval / millisInMinute
    val maxHours = maxInterval / millisInHour
    val maxDays = maxInterval / millisInDay

    val dayValue = value / millisInDay
    val hourValue = (value - (dayValue * millisInDay)) / millisInHour
    val minutesValue = (value - (dayValue * millisInDay) - (hourValue * millisInHour)) / millisInMinute

    Column(
        modifier = Modifier
    ) {

        Text(
            modifier = Modifier.padding(horizontal = 12.dp),
            text = "Интервал измерений",
            style = MaterialTheme.typography.titleLarge
        )

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


        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.align(Alignment.CenterHorizontally)
        ) {

            NumberPicker(
                range = -1..maxDays,
                value = dayValue,
                onValueChanged = {
                    value = (it * millisInDay) + (hourValue * millisInHour) + (minutesValue * millisInMinute)
                }
            )

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

            Text(text = "Дни")

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

            NumberPicker(
                range = -1..maxHours,
                value = hourValue,
                onValueChanged = {
                    value = (it * millisInHour) + (dayValue * millisInDay) + (minutesValue * millisInMinute)
                }
            )

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

            Text(text = "Часы")

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

            NumberPicker(
                range = -1..maxMinutes,
                value = minutesValue,
                onValueChanged = {
                    value = (it * millisInMinute) + (dayValue * millisInDay) + (hourValue * millisInHour)
                }
            )

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

            Text(text = "Минуты")

        }

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

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

            Box(modifier = Modifier.fillMaxSize()) {

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

            }

        }

    }

}

const val millisInMinute = 1000 * 60
const val millisInHour = millisInMinute * 60
const val millisInDay = millisInHour * 24


@Composable
fun NumberPicker(
    modifier: Modifier = Modifier,
    range: IntRange,
    value: Int,
    onValueChanged: (Int) -> Unit
) {

    LaunchedEffect(range){

        if(value > range.last){

            onValueChanged(range.last)

        }

        if(value < range.first){

            onValueChanged(range.first)

        }

    }

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier
    ){

        FilledIconButton(
            onClick = {
                if(value < range.last) onValueChanged(value + 1)
            }
        ) {
            Icon(
                imageVector = Icons.Rounded.KeyboardArrowUp,
                contentDescription = null
            )
        }

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

        AnimatedContent(
            targetState = value,
            transitionSpec = {
                if (targetState > initialState) {
                    (slideInVertically { height -> height } + fadeIn()).togetherWith(
                        slideOutVertically { height -> -height } + fadeOut())
                } else {
                    (slideInVertically { height -> -height } + fadeIn()).togetherWith(
                        slideOutVertically { height -> height } + fadeOut())
                }.using(
                    SizeTransform(clip = false)
                )
            }
        ) { targetCount ->
            Text(
                style = MaterialTheme.typography.displaySmall,
                text = "$targetCount"
            )
        }

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

        FilledIconButton(
            onClick = {
                if(value > range.first) onValueChanged(value - 1)

            }
        ) {
            Icon(
                imageVector = Icons.Rounded.KeyboardArrowDown,
                contentDescription = null
            )
        }

    }


}