import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sigap/src/features/auth/presentasion/controllers/signup/step/selfie-verification/face_liveness_detection_controller.dart';
import 'package:sigap/src/utils/constants/colors.dart';
import 'package:sigap/src/utils/constants/sizes.dart';

class ErrorStateWidget extends StatelessWidget {
  final String message;

  const ErrorStateWidget({
    super.key,
    required this.message,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    String userFriendlyMessage = message;
    IconData errorIcon = Icons.error_outline;
    Color errorColor = TColors.warning;

    // Convert technical errors to user-friendly messages
    if (message.contains('server_config_error') ||
        message.contains('environment variables')) {
      userFriendlyMessage =
          'The face verification service is temporarily unavailable. Please try again later.';
      errorIcon = Icons.cloud_off_outlined;
    } else if (message.contains('network') || message.contains('connection')) {
      userFriendlyMessage =
          'Network error. Please check your internet connection and try again.';
      errorIcon = Icons.wifi_off_outlined;
    } else if (message.contains('timeout')) {
      userFriendlyMessage =
          'The request timed out. Please try again when you have a stronger connection.';
      errorIcon = Icons.timer_off_outlined;
    } else if (message.contains('Camera initialization failed') ||
        message.contains('permission') ||
        message.contains('access')) {
      userFriendlyMessage =
          'Unable to access camera. Please check your camera permissions and try again.';
      errorIcon = Icons.camera_alt_outlined;
      errorColor = TColors.error;
    } else if (message.contains('decode') ||
        message.contains('Body can not be decoded')) {
      userFriendlyMessage =
          'There was a problem processing your image. Please try again.';
      errorIcon = Icons.image_not_supported_outlined;
    } else if (message.contains('invalid_request_format')) {
      userFriendlyMessage =
          'There was a problem with the image format. Please try again with a different image.';
      errorIcon = Icons.broken_image_outlined;
    } else if (message.contains('Failed to initialize face detection')) {
      userFriendlyMessage =
          'Face detection service is not available. Please try again later.';
      errorIcon = Icons.face_retouching_off_outlined;
    }

    return Center(
      child: Padding(
        padding: const EdgeInsets.all(TSizes.defaultSpace),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              padding: const EdgeInsets.all(TSizes.lg),
              decoration: BoxDecoration(
                color: errorColor.withOpacity(0.1),
                shape: BoxShape.circle,
              ),
              child: Icon(errorIcon, color: errorColor, size: 64),
            ),

            const SizedBox(height: TSizes.spaceBtwItems),

            Text(
              'Verification Error',
              style: theme.textTheme.headlineSmall?.copyWith(
                fontWeight: FontWeight.bold,
                color: errorColor,
              ),
            ),

            const SizedBox(height: TSizes.sm),

            Padding(
              padding: const EdgeInsets.symmetric(horizontal: TSizes.md),
              child: Text(
                userFriendlyMessage,
                style: theme.textTheme.bodyMedium?.copyWith(
                  color: theme.textTheme.bodySmall?.color,
                  height: 1.4,
                ),
                textAlign: TextAlign.center,
              ),
            ),
            
            const SizedBox(height: TSizes.spaceBtwSections),

            // Action buttons
            Column(
              children: [
                SizedBox(
                  width: double.infinity,
                  child: ElevatedButton.icon(
                    onPressed: () {
                      // Reset and try again
                      try {
                        final controller = Get.find<FaceLivenessController>();
                        controller.handleCancellation();
                      } catch (e) {
                        // Handle case where controller isn't available
                        Get.back();
                      }
                    },
                    icon: const Icon(Icons.refresh_rounded),
                    label: const Text('Try Again'),
                    style: theme.elevatedButtonTheme.style,
                  ),
                ),

                const SizedBox(height: TSizes.md),

                SizedBox(
                  width: double.infinity,
                  child: OutlinedButton.icon(
                    onPressed: () => Get.back(),
                    icon: const Icon(Icons.arrow_back_rounded),
                    label: const Text('Go Back'),
                    style: theme.outlinedButtonTheme.style,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
