package com.laseroptek.raman.utils

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
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.drawBehind
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.geometry.toRect
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.asAndroidPath
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt


@Composable
fun GradientButton(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    text: String,
    width: Dp = 80.dp,
    height: Dp = 42.dp,
    cornerRadius: Dp = 52.dp,

    dropShadowColor: Color = Color(0,0,0).copy(alpha = 0.14f),
    dropShadowOffsetX: Dp = 0.dp,
    dropShadowOffsetY: Dp = 5.dp,
    dropShadowBlurRadius: Dp = 7.dp,
    dropShadowSpread: Dp = 0.dp,

    innerShadowColor: Color = Color(255,255,255).copy(alpha = 1f),
    innerShadowOffsetX: Dp = 0.dp,
    innerShadowOffsetY: Dp = 4.dp,
    innerShadowBlurRadius: Dp = 1.dp,
    innerShadowSpread: Dp = 0.dp,

    gradientStartColor: Color = Color(250,250,250).copy(alpha = 1f),
    gradientEndColor: Color = Color(198,198,198).copy(alpha = 1f),

    borderGradientStartColor: Color = Color(81,81,81).copy(alpha = 1f),
    borderGradientEndColor: Color = Color(64,64,64).copy(alpha = 1f),
    borderWidth: Dp = 3.dp,

    textColor: Color = Color.Black,
    iconId: Int? = null // Add an optional iconId parameter
) {
    Button(
        onClick = onClick,
        modifier = modifier
            .size(width, height)
            .shadow(
                elevation = dropShadowBlurRadius,
                shape = RoundedCornerShape(cornerRadius),
                ambientColor = dropShadowColor,
                spotColor = dropShadowColor,
                clip = false
            )
            .graphicsLayer {
                translationX = dropShadowOffsetX.toPx()
                translationY = dropShadowOffsetY.toPx()
            }
            .drawBehind {
                // Draw the background gradient
                drawRoundRect(
                    brush = Brush.linearGradient(
                        colors = listOf(gradientStartColor, gradientEndColor)
                    ),
                    cornerRadius = CornerRadius(cornerRadius.toPx())
                )

                // Draw the inner shadow
                drawInnerShadow(
                    color = innerShadowColor,
                    offsetX = innerShadowOffsetX.toPx(),
                    offsetY = innerShadowOffsetY.toPx(),
                    blurRadius = innerShadowBlurRadius.toPx(),
                    spread = innerShadowSpread.toPx(),
                    cornerRadius = cornerRadius.toPx()
                )

                // Draw the gradient border
                drawGradientBorder(
                    borderWidth = borderWidth.toPx(),
                    cornerRadius = cornerRadius.toPx(),
                    gradientStartColor = borderGradientStartColor,
                    gradientEndColor = borderGradientEndColor
                )
            },
        colors = ButtonDefaults.buttonColors(
            containerColor = Color.Transparent, // Make the button's background transparent
            contentColor = textColor
        ),
        contentPadding = PaddingValues(0.dp)
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            // Display the icon if iconId is provided
            if (iconId != null) {
                Image(
                    painter = painterResource(id = iconId),
                    contentDescription = "Button Icon",
                    modifier = Modifier.size(24.dp) // Adjust the size as needed
                )
                Spacer(modifier = Modifier.width(8.dp)) // Add space between icon and text
            }
            Text(text = text)
        }
    }
}

fun DrawScope.drawInnerShadow(
    color: Color,
    offsetX: Float,
    offsetY: Float,
    blurRadius: Float,
    spread: Float,
    cornerRadius: Float
) {
    val shadowPath = Path().apply {
        addRoundRect(
            RoundRect(
                rect = size.toRect(),
                cornerRadius = CornerRadius(cornerRadius)
            )
        )
    }

    val shadowPaint = android.graphics.Paint().apply {
        this.color = color.toArgb()
        maskFilter = android.graphics.BlurMaskFilter(blurRadius, android.graphics.BlurMaskFilter.Blur.NORMAL)
    }

    val shadowBitmap = android.graphics.Bitmap.createBitmap(
        size.width.roundToInt(),
        size.height.roundToInt(),
        android.graphics.Bitmap.Config.ARGB_8888
    )

    val shadowCanvas = android.graphics.Canvas(shadowBitmap)
    shadowCanvas.drawPath(shadowPath.asAndroidPath(), shadowPaint)

    //val shadowOffset = Offset(offsetX, offsetY)
    // Convert Offset to IntOffset
    val shadowIntOffset = androidx.compose.ui.unit.IntOffset(offsetX.roundToInt(), offsetY.roundToInt())

    drawPath(
        path = shadowPath,
        color = color,
        alpha = 1f,
        blendMode = androidx.compose.ui.graphics.BlendMode.SrcIn
    )
    drawPath(
        path = shadowPath,
        color = color,
        alpha = 1f,
        blendMode = androidx.compose.ui.graphics.BlendMode.SrcOut
    )
    drawImage(
        image = shadowBitmap.asImageBitmap(),
        dstOffset = shadowIntOffset,
        alpha = 1f,
        blendMode = androidx.compose.ui.graphics.BlendMode.SrcAtop
    )
}

fun DrawScope.drawGradientBorder(
    borderWidth: Float,
    cornerRadius: Float,
    gradientStartColor: Color,
    gradientEndColor: Color
) {
    val path = Path().apply {
        addRoundRect(
            RoundRect(
                rect = size.toRect(),
                cornerRadius = CornerRadius(cornerRadius)
            )
        )
    }

    drawPath(
        path = path,
        brush = Brush.linearGradient(
            colors = listOf(gradientStartColor, gradientEndColor)
        ),
        style = Stroke(width = borderWidth)
    )
}

@Preview(showBackground = true)
@Composable
fun GradientButtonPreview() {
    Box(modifier = Modifier.fillMaxSize().background(Color.Gray)) {
        GradientButton(
            onClick = { /* Handle click */ },
            text = "Gradient",
            modifier = Modifier.padding(16.dp)
        )
    }
}