import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';
import 'package:sigap/src/cores/services/location_service.dart';
import 'package:sigap/src/utils/constants/app_routes.dart';
import 'package:sigap/src/utils/constants/image_strings.dart';
import 'package:sigap/src/utils/constants/sizes.dart';
import 'package:sigap/src/utils/popups/loaders.dart';

class LocationWarningScreen extends StatelessWidget {
  const LocationWarningScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final locationService = Get.find<LocationService>();
    final isMocked = locationService.isMockedLocation.value;
    final isOutsideJember = !locationService.isInJember();
    
    // Get theme data for consistent styling
    final theme = Theme.of(context);

    return Scaffold(
      // Use theme background color instead of hardcoded color
      backgroundColor: theme.scaffoldBackgroundColor,
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(TSizes.defaultSpace),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              // Warning Icon - use theme error color
              Lottie.asset(
                TImages.unverifyLocationAnimation,
                width: 200,
                height: 200,
                fit: BoxFit.cover,
                errorBuilder: (context, error, stackTrace) {
                  return const Icon(Icons.error, size: 100, color: Colors.red);
                },
              ),

              const SizedBox(height: TSizes.spaceBtwSections),

              // Warning Title - use theme headline style
              Text(
                'Location Restriction',
                style: theme.textTheme.headlineMedium?.copyWith(
                  fontWeight: FontWeight.bold,
                ),
                textAlign: TextAlign.center,
              ),

              const SizedBox(height: TSizes.spaceBtwItems),

              // Warning Message - use theme body style
              Text(
                isMocked
                    ? 'We detected that you are using a mock location app. Please disable it to continue.'
                    : isOutsideJember
                    ? 'This application is only available within Jember region. Please try again when you are in Jember.'
                    : 'We could not verify your location. Please ensure your location services are enabled and try again.',
                style: theme.textTheme.bodyLarge?.copyWith(height: 1.5),
                textAlign: TextAlign.center,
              ),

              const SizedBox(height: TSizes.spaceBtwSections * 1.5),

              // Try Again Button - full width
              SizedBox(
                width: double.infinity,
                height: 56,
                child: ElevatedButton(
                  onPressed: () async {
                    final isValid =
                        await locationService.isLocationValidForFeature();
                    if (isValid) {
                      Get.offAllNamed(AppRoutes.roleSelection);
                    } else {
                      TLoaders.errorSnackBar(
                        title: 'Location Verification Failed',
                        message:
                            isMocked
                                ? 'Please disable mock location apps to continue.'
                                : 'Ensure you are within Jember region and location services are enabled.',
                      );
                    }
                  },
                  style: ElevatedButton.styleFrom(
                    minimumSize: const Size.fromHeight(56),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(TSizes.buttonRadius),
                    ),
                    elevation: 0,
                  ),
                  child: Text(
                    'Try Again',
                    style: Theme.of(context).textTheme.titleMedium,
                  ),
                ),
              ),

              const SizedBox(height: TSizes.spaceBtwItems),

              // Exit App Button - full width
              SizedBox(
                width: double.infinity,
                child: TextButton(
                onPressed: () {
                    // This will close the app
                    Get.back();
                },
                  style: TextButton.styleFrom(
                    foregroundColor: theme.colorScheme.secondary,
                  ),
                child: Text(
                'Exit App',
                style: theme.textTheme.bodyLarge),
                ),
              ),
            ],
            
          ),
        ),
      ),
    );
  }
}
