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

import androidx.compose.animation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.ArrowBack
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import llc.arma.ble.app.ui.model.BleView
import llc.arma.ble.app.ui.screen.BleInfoView
import llc.arma.ble.app.ui.screen.inspection.accelerometer.AccelerometerContract
import llc.arma.ble.app.ui.screen.inspection.accelerometer.AccelerometerScreen
import llc.arma.ble.app.ui.screen.inspection.beacon.BeaconScreen
import llc.arma.ble.app.ui.screen.password.ChangePasswordContract
import llc.arma.ble.app.ui.screen.inspection.thermometer.ThermometerContract
import llc.arma.ble.app.ui.screen.inspection.thermometer.ThermometerScreen
import llc.arma.ble.domain.model.Ble
import llc.arma.ble.domain.usecase.GetBleBySerial

@OptIn(ExperimentalMaterial3Api::class, ExperimentalAnimationApi::class)
@Composable
fun ConnectionScreen(
    onNavigationEvent: (ConnectionContract.Effect.Navigation) -> Unit
) {

    val viewModel = hiltViewModel<ConnectionViewModel>()
    val state = viewModel.viewState.value

    LaunchedEffect("effect"){
        viewModel.effect.onEach {
            when(it){
                is ConnectionContract.Effect.Navigation -> onNavigationEvent(it)
            }
        }.launchIn(this)
    }

    Column {

        CenterAlignedTopAppBar(
            navigationIcon = {
                IconButton(
                    onClick = {
                        viewModel.setEvent(ConnectionContract.Event.OnNavigateUp)
                    },
                    content = {
                        Icon(
                            imageVector = Icons.Rounded.ArrowBack,
                            contentDescription = null
                        )
                    }
                )
            },
            title = {

                AnimatedContent(
                    targetState = when(state){
                        is ConnectionContract.State.Display -> state.ble.info.name
                        is ConnectionContract.State.DisplayException -> "Исключение"
                        is ConnectionContract.State.Loading -> "Соединение.."
                    },
                    transitionSpec = {
                        (slideInVertically { height -> height } + fadeIn() with
                                slideOutVertically { height -> -height } + fadeOut()).using(
                            SizeTransform(clip = false)
                        )
                    }
                ) { targetText ->
                    Text(
                        text = targetText
                    )
                }

            }
        )

        when (state) {
            is ConnectionContract.State.DisplayException -> DisplayException(
                onEvent = {
                    viewModel.setEvent(it)
                }
            )
            is ConnectionContract.State.Loading -> LoadingState()
            is ConnectionContract.State.Display -> {
                when(state.ble){
                    is Ble.Beacon -> BeaconScreen(
                        ble = state.ble,
                        onNavigationEvent = {
                            viewModel.setEvent(ConnectionContract.Event.OnBeaconNavigationEvent(it))
                        }
                    )
                    is Ble.Thermometer -> {

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

                            ThermometerScreen(
                                ble = state.ble,
                                onNavigationEvent = {
                                    viewModel.setEvent(
                                        ConnectionContract.Event.OnThermometerNavigationEvent(it)
                                    )
                                }
                            )

                        }

                    }

                    is Ble.Accelerometer -> {
                        AccelerometerScreen(ble = state.ble) {
                            viewModel.setEvent(
                                ConnectionContract.Event.OnAccelNavigationEvent(it)
                            )
                        }
                    }
                }

            }
        }

    }

}


@Composable
private fun LoadingState(){
    Column {
        Box(modifier = Modifier.fillMaxSize()) {
            CircularProgressIndicator(
                strokeCap = StrokeCap.Round,
                modifier = Modifier.align(Alignment.Center
                )
            )
        }
    }
}

@Composable
private fun DisplayException(
    onEvent: (ConnectionContract.Event) -> Unit
){

    Box(
        modifier = Modifier.fillMaxSize()
    ) {

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

            Text(
                textAlign = TextAlign.Center,
                style = MaterialTheme.typography.titleMedium,
                text = "Неудалось соединится с устройством"
            )

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

            Surface(
                modifier = Modifier
                    .height(42.dp),
                shape = CircleShape,
                color = MaterialTheme.colorScheme.primaryContainer,
                onClick = {
                    onEvent(ConnectionContract.Event.RefreshBle)
                }
            ) {

                Box(modifier = Modifier.padding(horizontal = 16.dp)) {

                    Text(
                        modifier = Modifier.align(Alignment.Center),
                        color = MaterialTheme.colorScheme.onPrimaryContainer,
                        style = MaterialTheme.typography.labelLarge,
                        text = "Повторить"
                    )

                }

            }

        }

    }

}