package com.example.libary

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
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.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.shape.AbsoluteRoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.libary.HeaderState.*
import com.example.libary.theme.Black1Color
import com.example.libary.theme.InputBGColor

enum class HeaderState {
    Big,
    Small
}

// Хэдер
// modifier - модификаторы
// state - состояние хзэдера
// title - загаловок
// onBack - вызываеться при нажатии на кнопку назад
// onRm - вызываеться при нажатии на кнопку удалить
@Composable
fun Header(
    modifier: Modifier = Modifier,
    state: HeaderState,
    title: String,
    onBack: () -> Unit = {},
    onRm: () -> Unit = {}
) {
    when(state) {
        Big -> {
            Column(
                modifier = modifier
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(end = 11.dp),
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    HeaderBt(
                        icon = R.drawable.back,
                        onClick = onBack
                    )

                    Image(
                        painter = painterResource(R.drawable.rm),
                        contentDescription = null,
                        modifier = Modifier.clickable {
                            onRm()
                        }
                    )
                }

                Spacer(Modifier.height(24.dp))

                Text(
                    text = title,
                    color = Black1Color,
                    fontWeight = FontWeight.W800,
                    fontSize = 24.sp
                )
            }
        }
        Small -> {
            Row(
                modifier = modifier
                    .fillMaxWidth()
                    .padding(start = 20.dp, end = 26.dp),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                HeaderBt(
                    icon = R.drawable.back,
                    onClick = onBack
                )

                Text(
                    text = title,
                    color = Black1Color,
                    fontWeight = FontWeight.W600,
                    fontSize = 20.sp
                )

                Image(
                    painter = painterResource(R.drawable.rm),
                    contentDescription = null,
                    modifier = Modifier.clickable {
                        onRm()
                    }
                )
            }
        }
    }
}

@Composable
internal fun HeaderBt(
    icon: Int,
    onClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier
            .size(32.dp)
            .background(InputBGColor, AbsoluteRoundedCornerShape(8.dp))
            .clip(AbsoluteRoundedCornerShape(8.dp)),
        contentAlignment = Alignment.Center
    ) {
        Image(
            painter = painterResource(icon),
            contentDescription = null,
            modifier = Modifier.padding(6.dp).clickable {
                onClick()
            }
        )
    }
}

@Preview
@Composable
private fun HeaderPr() {
    Column(
        modifier = Modifier.fillMaxSize().background(Color.White).padding(10.dp)
    ) {
        Header(
            state = HeaderState.Big,
            title = "Корзина"
        )
    }
}