package com.example.labwork.pages

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.expandIn
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkOut
import androidx.compose.foundation.Image
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.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.labwork.models.ItemProduct
import com.example.labwork.ui.theme.LightBluePolitech


@Composable
fun ListProduct(item: ItemProduct) {
    var isFullAbout by remember { mutableStateOf(false) }
    val scale by animateFloatAsState(if (isFullAbout) 1f else 0f)
    val textSize by animateDpAsState(if (isFullAbout) 18.dp else 24.dp)

    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp),
        shape = RoundedCornerShape(15.dp),
        elevation = 5.dp
    ) {
        Box {
            Row(verticalAlignment = Alignment.CenterVertically) {
                Column {
                    Image(
                        painter = painterResource(id = item.imageId),
                        contentDescription = "book",
                        contentScale = ContentScale.Crop,
                        modifier = Modifier
                            .padding(10.dp)
                            .size(128.dp)
                            .shadow(2.dp)
                    )
                    Button(
                        colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech),
                        modifier = Modifier
                            .padding(start = 9.dp, bottom = 9.dp)
                            .size(height = 32.dp, width = 128.dp),
                        onClick = {
                            isFullAbout = !isFullAbout
                        },
                        shape = RoundedCornerShape(15.dp)
                    ) {
                        Text(
                            text = if(isFullAbout) "Убрать" else "Подробнее",
                            color = Color.White,
                            fontSize = 10.sp,
                            textAlign = TextAlign.Center,
                        )
                    }
                    Row {
                        Button(
                            colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech),
                            modifier = Modifier
                                .padding(start = 9.dp, bottom = 9.dp)
                                .size(height = 32.dp, width = 40.dp),
                            onClick = {
                                /*TODO*/
                            },
                            shape = RoundedCornerShape(15.dp)
                        ) {
                            Text(
                                text = "+",
                                color = Color.White,
                                fontSize = 10.sp,
                                textAlign = TextAlign.Center,
                            )
                        }
                        Text(
                            text = "1000",
                            color = Color.Black,
                            fontSize = 14.sp,
                            textAlign = TextAlign.Center,
                            modifier = Modifier.padding(start = 8.dp, top = 4.dp)
                        )
                        Button(
                            colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech),
                            modifier = Modifier
                                .padding(start = 9.dp, bottom = 9.dp)
                                .size(height = 32.dp, width = 40.dp),
                            onClick = {
                                /*TODO*/
                            },
                            shape = RoundedCornerShape(15.dp)
                        ) {
                            Text(
                                text = "-",
                                color = Color.White,
                                fontSize = 10.sp,
                                textAlign = TextAlign.Center,
                            )
                        }
                    }
                }
                Column {
                    Text(
                        text = item.name,
                        color = Color.Black,
                        fontSize = textSize.value.sp,
                        fontWeight = FontWeight.Bold,
                        textAlign = TextAlign.Center,
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 8.dp)
                    )
                    AnimatedVisibility(
                        visible = isFullAbout,
                        enter = fadeIn() + expandIn(),
                        exit = fadeOut() + shrinkOut(),
                        modifier = Modifier.scale(scale)
                    ) {
                        Text(
                            color = Color.Black,
                            text = item.about,
                            maxLines = 10,
                            fontSize = 14.sp,
                            textAlign = TextAlign.Center,
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 8.dp)
                        )
                    }
                }
            }
        }
    }
}
