package com.example.libary

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.layout.width
import androidx.compose.foundation.shape.AbsoluteRoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
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.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.libary.theme.AccentColor
import com.example.libary.theme.AccentInactive
import com.example.libary.theme.BlackColor
import com.example.libary.theme.Desc
import com.example.libary.theme.InputBGColor
import com.example.libary.theme.InputStroke
import com.example.libary.theme.WhiteColor

enum class BigButtonState(
    val containerColor: Color,
    val textColor: Color,
    val enabled: Boolean = true,
    val border: BorderStroke? = null
) {
    Property(
        containerColor = AccentColor,
        textColor = Color.White,
    ),
    Inactive(
        containerColor = AccentInactive,
        textColor = WhiteColor,
        enabled = false
    ),
    Secondary(
        containerColor = Color.White,
        textColor = AccentColor,
        border = BorderStroke(1.dp, AccentColor)
    ),
    Tetriary(
        containerColor = InputBGColor,
        textColor = BlackColor,
    )
}

// большая кнопка
// state - состояние кнопки
// modifier - модификаторы
// onClick - вызовиться при нажатии на кнопку
// text - текст кнопки
@Composable
fun BigButton(
    state: BigButtonState,
    modifier: Modifier = Modifier,
    onClick: () -> Unit = {},
    text: String = ""
) {
    Button(
        onClick = {
            if (state.enabled) onClick()
        },
        modifier = modifier
            .width(335.dp)
            .height(56.dp),
        border = state.border,
        shape = AbsoluteRoundedCornerShape(10.dp),
        colors = ButtonDefaults.buttonColors(
            containerColor = state.containerColor
        ),
        contentPadding = PaddingValues(0.dp)
    ) {
        Text(
            text = text,
            color = state.textColor,
            fontWeight = FontWeight.W600,
            fontSize = 17.sp
        )
    }
}

enum class SmallButtonState(
    val containerColor: Color,
    val textColor: Color,
    val enabled: Boolean = true,
    val border: BorderStroke? = null
) {
    Property(
        containerColor = AccentColor,
        textColor = Color.White,
    ),
    Inactive(
        containerColor = AccentInactive,
        textColor = WhiteColor,
        enabled = false
    ),
    Secondary(
        containerColor = Color.White,
        textColor = AccentColor,
        border = BorderStroke(1.dp, AccentColor)
    ),
    Tetriary(
        containerColor = InputBGColor,
        textColor = BlackColor,
    )
}

// маленькая кнопка
// state - состояние кнопки
// modifier - модификаторы
// onClick - вызовиться при нажатии на кнопку
// text - текст кнопки
@Composable
fun SmallButton(
    state: SmallButtonState,
    modifier: Modifier = Modifier,
    onClick: () -> Unit = {},
    text: String = ""
) {
    Button(
        onClick = {
            if (state.enabled) onClick()
        },
        modifier = modifier
            .width(96.dp)
            .height(40.dp),
        border = state.border,
        shape = AbsoluteRoundedCornerShape(10.dp),
        colors = ButtonDefaults.buttonColors(
            containerColor = state.containerColor
        ),
        contentPadding = PaddingValues(horizontal = 13.5.dp, vertical = 0.dp)
    ) {
        Text(
            text = text,
            color = state.textColor,
            maxLines = 1,
            fontWeight = FontWeight.W600,
            fontSize = 14.sp,
            overflow = TextOverflow.Ellipsis
        )
    }
}

// кнопка корзины
// price - сумма которая лежит в корзине
// modifier - модификаторы
// onClick - вызовиться при нажатии на кнопку
// text - текст кнопки
@Composable
fun CartButton(
    modifier: Modifier = Modifier,
    onClick: () -> Unit = {},
    price: String = ""
) {
    Button(
        onClick = onClick,
        modifier = modifier
            .width(335.dp)
            .height(56.dp),
        contentPadding = PaddingValues(0.dp),
        colors = ButtonDefaults.buttonColors(
            containerColor = AccentColor
        ),
        shape = AbsoluteRoundedCornerShape(10.dp)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically
            ) {
                Icon(
                    painter = painterResource(R.drawable.cart),
                    contentDescription = null
                )

                Spacer(Modifier.width(16.dp))

                Text(
                    text = "В корзину",
                    fontWeight = FontWeight.W600,
                    fontSize = 17.sp,
                    color = WhiteColor
                )
            }

            Text(
                text = "$price ₽",
                fontWeight = FontWeight.W600,
                fontSize = 17.sp,
                color = WhiteColor
            )
        }
    }
}

enum class LoginState(
    val icon: Int,
    val text: String
) {
    VK(R.drawable.vk, "Войти с VK"),
    Yandex(R.drawable.yandex, "Войти с Yandex")
}

// кнопка авторизации
// state - состояние кнопки
// modifier - модификаторы
// onClick - вызовиться при нажатии на кнопку
@Composable
fun LoginButton(
    modifier: Modifier = Modifier,
    state: LoginState = LoginState.VK,
    onClick: () -> Unit = {}
) {
    Button(
        onClick = onClick,
        modifier = modifier
            .width(335.dp)
            .height(60.dp),
        shape = AbsoluteRoundedCornerShape(10.dp),
        border = BorderStroke(1.dp, InputStroke),
        colors = ButtonDefaults.buttonColors(
            containerColor = Color.White
        )
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                painter = painterResource(state.icon),
                contentDescription = null,
                modifier = Modifier.size(32.dp)
            )

            Spacer(Modifier.width(16.dp))

            Text(
                text = state.text,
                color = Color.Black,
                fontWeight = FontWeight.W500,
                fontSize = 17.sp
            )
        }
    }
}

enum class ChipButtonState(
    val containerColor: Color,
    val textColor: Color
) {
    On(AccentColor, Color.White),
    Off(InputBGColor, Desc)
}

// chip кнопка
// state - состояние кнопки
// modifier - модификаторы
// onClick - вызовиться при нажатии на кнопку
// text - текст кнопки
@Composable
fun ChipButton(
    modifier: Modifier = Modifier,
    onClick: () -> Unit = {},
    state: ChipButtonState = ChipButtonState.On,
    text: String = ""
) {
    Button(
        onClick = onClick,
        modifier = modifier
            .width(129.dp)
            .height(48.dp),
        shape = AbsoluteRoundedCornerShape(10.dp),
        colors = ButtonDefaults.buttonColors(
            containerColor = state.containerColor
        ),
        contentPadding = PaddingValues(0.dp)
    ) {
        Text(
            text = text,
            color = state.textColor,
            maxLines = 1,
            fontWeight = FontWeight.W600,
            fontSize = 14.sp,
            overflow = TextOverflow.Ellipsis
        )
    }
}

@Preview
@Composable
private fun BigButtonPrev() {
    Column {
        BigButtonState.entries.forEach {
            BigButton(
                state = it,
                text = "test"
            )
            Spacer(Modifier.height(5.dp))
        }
    }
}