package ru.myitschool.work.ui.screen.room

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import ru.myitschool.work.core.TestIds
import ru.myitschool.work.ui.screen.auth.AuthViewModel

@Composable
fun RoomScreen(
    roomId: Int,
    viewModel: AuthViewModel = viewModel(),
    roomId: Int,
    navController: NavController
) {
    val state by viewModel.uiState.collectAsState()


    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.SpaceBetween,
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.surfaceContainerLow)
    ) {
        when(val currentState = state) {
            is RoomState.Data -> {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center,
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(20.dp)
                ) {
                    Text(
                        text = "TEST_LOCATOIN",
                        style = MaterialTheme.typography.displayMedium
                    )
                    Spacer(modifier = Modifier.size(12.dp))
                    LazyColumn {
                        itemsIndexed(currentState.data.data.entries.toList()) { index, item ->
                            DayCard(item.key, item.value.toString(), index)
                        }
                    }
                }
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                ) {
                    Button(
                        onClick = { viewModel.onIntent(RoomIntent.Booking(1488, "1488")) }
                    ) {
                        Text("Book")
                    }
                    Button(
                        onClick = { viewModel.onIntent(RoomIntent.UnBook(1488, "1488")) }
                    ) {
                        Text("Cancel")
                    }
                }


            }
            is RoomState.Error -> {
                Card(
                    shape = RoundedCornerShape(20.dp),
                    colors = CardDefaults.cardColors(
                        containerColor = MaterialTheme.colorScheme.errorContainer,
                    ),
                ) {
                    Text(
                        modifier = Modifier.testTag(TestIds.Auth.ERROR).padding(16.dp),
                        text = currentState.error,
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onErrorContainer,
                    )
                }
                Button(
                    onClick = { viewModel.onIntent(RoomIntent.Refresh) }
                ) {
                    Text("Refresh")
                }
            }
            is RoomState.Loading -> {
                CircularProgressIndicator()
            }
        }
    }
}

@Composable
fun DayCard(name: String?, day: String, index: Int) {
    Card(
        shape = RoundedCornerShape(20.dp),
        colors = CardDefaults.cardColors(
            containerColor = if (name == null) {
                MaterialTheme.colorScheme.surfaceContainerLowest
            } else if (name == "hitler") {
                MaterialTheme.colorScheme.secondaryContainer
            } else {
                MaterialTheme.colorScheme.tertiaryContainer
            },   //REMAKE TO STATE
        ),
        modifier = Modifier.fillMaxWidth()
    ) {
        Row(
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.padding(24.dp)
        ) {
            Column(
                verticalArrangement = Arrangement.Center,
            ) {
                Text(
                    text = "${name}",
                    style = MaterialTheme.typography.labelLarge,
                    color = MaterialTheme.colorScheme.onSurface
                )
                Text(
                    text = "${day}",
                    style = MaterialTheme.typography.headlineMedium,
                    color = MaterialTheme.colorScheme.onSurface
                )
            }
            Text("${index}")
        }
    }
}