package app.closer.ui.auth

import app.closer.R
import app.closer.ui.theme.closerCardColor
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextFieldDefaults
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.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
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.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

internal val AuthBackgroundBrush: Brush
    @Composable
    get() = Brush.linearGradient(
        colors = listOf(
            MaterialTheme.colorScheme.background,
            MaterialTheme.colorScheme.surfaceVariant,
            MaterialTheme.colorScheme.secondaryContainer
        ),
        start = Offset.Zero,
        end = Offset.Infinite
    )

internal val AuthInk: Color
    @Composable get() = MaterialTheme.colorScheme.onBackground
internal val AuthMuted: Color
    @Composable get() = MaterialTheme.colorScheme.onSurfaceVariant
internal val AuthPrimary: Color
    @Composable get() = MaterialTheme.colorScheme.primary
internal val AuthPrimaryDeep: Color
    @Composable get() = MaterialTheme.colorScheme.onPrimaryContainer
internal val AuthOnPrimary: Color
    @Composable get() = MaterialTheme.colorScheme.onPrimary

@Composable
internal fun AuthLogoMark(
    modifier: Modifier = Modifier,
    size: Dp = 88.dp,
    radius: Dp = 24.dp,
    elevation: Dp = 18.dp
) {
    val shape = RoundedCornerShape(radius)
    Box(
        modifier = modifier
            .size(size)
            .shadow(elevation = elevation, shape = shape, clip = false)
            .clip(shape)
    ) {
        Image(
            painter = painterResource(R.drawable.ic_launcher_background),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier.matchParentSize()
        )
        Image(
            painter = painterResource(R.drawable.ic_launcher_foreground),
            contentDescription = "Closer",
            contentScale = ContentScale.Fit,
            modifier = Modifier.matchParentSize().alpha(0.96f)
        )
    }
}

@Composable
internal fun GoogleSignInButton(
    onClick: () -> Unit,
    enabled: Boolean = true,
    modifier: Modifier = Modifier
) {
    OutlinedButton(
        onClick = onClick,
        enabled = enabled,
        modifier = modifier.fillMaxWidth().height(52.dp),
        colors = ButtonDefaults.outlinedButtonColors(
            containerColor = Color.White,
            contentColor = Color(0xFF1F1F1F)
        ),
        border = BorderStroke(1.dp, Color(0xFF747775)),
        contentPadding = PaddingValues(start = 12.dp, end = 12.dp)
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            Image(
                painter = painterResource(id = R.drawable.ic_google_g),
                contentDescription = null,
                modifier = Modifier.size(18.dp)
            )
            Text(
                text = "Continue with Google",
                style = MaterialTheme.typography.labelLarge.copy(
                    fontWeight = FontWeight.Medium,
                    fontSize = 14.sp,
                    lineHeight = 20.sp
                )
            )
        }
    }
}

@Composable
internal fun authTextFieldColors() = OutlinedTextFieldDefaults.colors(
    focusedBorderColor = AuthPrimaryDeep,
    unfocusedBorderColor = AuthMuted.copy(alpha = 0.24f),
    focusedLabelColor = AuthPrimaryDeep,
    unfocusedLabelColor = AuthMuted,
    cursorColor = AuthPrimaryDeep,
    focusedContainerColor = closerCardColor(alpha = 0.92f),
    unfocusedContainerColor = closerCardColor(alpha = 0.78f),
    focusedTextColor = AuthInk,
    unfocusedTextColor = AuthInk,
    focusedTrailingIconColor = AuthPrimaryDeep,
    unfocusedTrailingIconColor = AuthMuted
)
