package com.example.myapplication.database.entities.composeui

import android.content.res.Configuration
import android.graphics.BitmapFactory
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
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.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.myapplication.ui.theme.PmudemoTheme
import org.threeten.bp.format.DateTimeFormatter

@Composable
fun RentView(
    id: Int,
    viewModel: RentViewModel = viewModel(factory = AppViewModelProvider.Factory)
) {
    val coroutineScope = rememberCoroutineScope()
    val rentUiState by viewModel.rentUiState.collectAsState()
    LazyColumn(
        modifier = Modifier
            .padding(10.dp)
    ) {
        items(rentUiState.itemList) { item ->
            val count = remember { mutableStateOf(item.count) }
            val dateFormatter = DateTimeFormatter.ofPattern("yyyy")
            val formattedDate = dateFormatter.format(item.dateTime)

/*            Text(
                text = formattedDate,
                color = MaterialTheme.colorScheme.onBackground,
            )*/
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
                    .clip(RoundedCornerShape(16.dp))
                    .background(MaterialTheme.colorScheme.primary)
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    if (item.bike.image != null)
                        Image(
                            bitmap = BitmapFactory.decodeByteArray(
                                item.bike.image,
                                0,
                                item.bike.image.size
                            ).asImageBitmap(),
                            contentDescription = null,
                            modifier = Modifier
                                .size(90.dp)
                                .padding(4.dp)
                        )

                    Column(
                        modifier = Modifier
                            .weight(1f)
                            .padding(start = 8.dp),
                        verticalArrangement = Arrangement.spacedBy(4.dp)
                    ) {
                        Text(
                            text = "${item.bike.name}\n" +
                                    "Вес: ${item.frozenWeight}\n" +
                                    "Количество: ${count.value}\n" +
                                    "Дата: ${formattedDate}",
                            color = MaterialTheme.colorScheme.onPrimary
                        )
/*                        Row(verticalAlignment = Alignment.CenterVertically) {
                            // Кнопка "Удалить"
                            Button(
                                onClick = {
                                    coroutineScope.launch {
                                        //viewModel.removeItemFromRent(item)
                                    }
                                },
                                modifier = Modifier.padding(end = 8.dp)
                            ) {
                                Icon(
                                    imageVector = Icons.Default.Delete,
                                    contentDescription = "Удалить"
                                )
                                Text(text = "Удалить")
                            }
                        }*/
                    }
                }
            }
        }
    }
}


@Preview(name = "Light Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun RentViewPreview() {
    PmudemoTheme {
        Surface(
            color = MaterialTheme.colorScheme.background
        ) {
            RentView(id = 1)
        }
    }
}