package cl.homelogic.platform.designsystem.components.atoms

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MaterialTheme.typography
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import cl.homelogic.platform.designsystem.extensions.clickable
import cl.homelogic.platform.designsystem.extensions.scaledByFontSize
import cl.homelogic.platform.designsystem.foundations.FeedbackType
import cl.homelogic.platform.designsystem.foundations.LocalHapticManager
import cl.homelogic.platform.designsystem.foundations.Padding
import cl.homelogic.platform.designsystem.foundations.Shapes
import cl.homelogic.platform.designsystem.foundations.Sizes
import cl.homelogic.platform.designsystem.foundations.Symbols

enum class ButtonStyle {
    PRIMARY,
    SECONDARY,
    TERTIARY,
}

@Composable
fun SimpleButton(
    modifier: Modifier = Modifier,
    style: ButtonStyle = ButtonStyle.PRIMARY,
    text: String = "",
    leadingSymbol: Symbols? = null,
    enabled: Boolean = true,
    hapticFeedback: Boolean = true,
    onClick: () -> Unit,
) {
    val haptic = LocalHapticManager.current

    val (background, textColor) = when (style) {
        ButtonStyle.PRIMARY -> {
            MaterialTheme.colorScheme.tertiary to MaterialTheme.colorScheme.onTertiary
        }

        ButtonStyle.SECONDARY -> {
            MaterialTheme.colorScheme.tertiaryContainer to MaterialTheme.colorScheme.onSurface
        }

        ButtonStyle.TERTIARY -> {
            Color.Transparent to MaterialTheme.colorScheme.onSurface
        }
    }

    Button(
        onClick = {
            if (hapticFeedback) haptic.performFeedback(FeedbackType.SegmentTick)
            onClick()
        },
        shape = Shapes.RoundCorner.big,
        enabled = enabled,
        colors = ButtonDefaults.buttonColors().copy(
            containerColor = background,
            contentColor = textColor,
            disabledContainerColor = background,
            disabledContentColor = textColor,
        ),
        modifier = modifier
            .fillMaxWidth()
            .height(Sizes.Buttons.Regular.scaledByFontSize())
            .clickable(enabled),
    ) {
        SymbolText(
            modifier = Modifier.padding(end = Padding.small),
            color = textColor,
            icon = leadingSymbol,
        )
        Text(
            text = text,
            style = typography.bodyLarge,
        )
    }
}
