package com.tt.honeyDue.ui.components.auth

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.tt.honeyDue.auth.GoogleSignInManager
import com.tt.honeyDue.auth.GoogleSignInResult
import kotlinx.coroutines.launch

@Composable
actual fun GoogleSignInButton(
    onSignInStarted: () -> Unit,
    onSignInSuccess: (idToken: String) -> Unit,
    onSignInError: (message: String) -> Unit,
    enabled: Boolean
) {
    val context = LocalContext.current
    val scope = rememberCoroutineScope()
    var isLoading by remember { mutableStateOf(false) }

    val googleSignInManager = remember { GoogleSignInManager(context) }

    OutlinedButton(
        onClick = {
            if (!isLoading && enabled) {
                isLoading = true
                onSignInStarted()

                scope.launch {
                    when (val result = googleSignInManager.signIn()) {
                        is GoogleSignInResult.Success -> {
                            isLoading = false
                            onSignInSuccess(result.idToken)
                        }
                        is GoogleSignInResult.Error -> {
                            isLoading = false
                            onSignInError(result.message)
                        }
                        GoogleSignInResult.Cancelled -> {
                            isLoading = false
                            // User cancelled, no error needed
                        }
                    }
                }
            }
        },
        modifier = Modifier
            .fillMaxWidth()
            .height(56.dp),
        enabled = enabled && !isLoading,
        shape = RoundedCornerShape(12.dp),
        border = BorderStroke(1.dp, MaterialTheme.colorScheme.outline),
        colors = ButtonDefaults.outlinedButtonColors(
            containerColor = MaterialTheme.colorScheme.surface
        )
    ) {
        if (isLoading) {
            CircularProgressIndicator(
                modifier = Modifier.size(24.dp),
                strokeWidth = 2.dp
            )
        } else {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.Center
            ) {
                // Google "G" logo using Material colors
                Box(
                    modifier = Modifier.size(24.dp),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = "G",
                        style = MaterialTheme.typography.titleMedium,
                        fontWeight = FontWeight.Bold,
                        color = Color(0xFF4285F4) // Google Blue
                    )
                }

                Spacer(modifier = Modifier.width(12.dp))

                Text(
                    text = "Continue with Google",
                    style = MaterialTheme.typography.titleMedium,
                    fontWeight = FontWeight.Medium,
                    color = MaterialTheme.colorScheme.onSurface
                )
            }
        }
    }
}
