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

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.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.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
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.graphics.StrokeCap
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import llc.arma.ble.R

@Composable
fun Write(
    state: BleTableEditContract.State.Display.WriteState,
    onEvent: (BleTableEditContract.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 BleTableEditContract.State.Display.WriteState.DisplayPreview -> {

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

                    LazyColumn(
                        modifier = Modifier
                            .padding(horizontal = 12.dp)
                    ) {

                        item {
                            Text(
                                style = MaterialTheme.typography.titleLarge,
                                textAlign = TextAlign.Center,
                                text = "Новая таблица BLE",
                            )
                        }

                        items(items = state.writeRequest) {
                            SelectBleItem(it)
                        }

                        if(state.writeRequest.isEmpty()){
                            item {
                                Text(
                                    textAlign = TextAlign.Center,
                                    text = "Пусто",
                                    modifier = Modifier.padding(48.dp).fillMaxWidth()
                                )
                            }
                        }

                    }
                }

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

                Surface(
                    shape = CircleShape,
                    color = MaterialTheme.colorScheme.primaryContainer,
                    onClick = {
                        onEvent(BleTableEditContract.Event.OnWrite)
                    },
                    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(BleTableEditContract.Event.OnHideWritePreview)
                    },
                    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 = "Отменить"
                        )

                    }

                }

            }
            is BleTableEditContract.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(BleTableEditContract.Event.OnHideWritePreview)
                            }
                        ) {

                            Box(modifier = Modifier.fillMaxSize()) {

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

                            }

                        }

                    }

                }

            }
            BleTableEditContract.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(BleTableEditContract.Event.OnHideWritePreview)
                            }
                        ) {

                            Box(modifier = Modifier.fillMaxSize()) {

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

                            }

                        }

                    }

                }

            }
            BleTableEditContract.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(BleTableEditContract.Event.OnHideWritePreview)
                            }
                        ) {

                            Box(modifier = Modifier.fillMaxSize()) {

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

                            }

                        }

                    }

                }

            }
        }

    }

}