import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:sigap/src/utils/validators/image_validator.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Image Format Tester')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Test image format validation',
              style: Theme.of(context).textTheme.titleLarge,
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () => _testImageFromCamera(context),
              child: const Text('Test Camera Image'),
            ),
            const SizedBox(height: 12),
            ElevatedButton(
              onPressed: () => _testImageFromGallery(context),
              child: const Text('Test Gallery Image'),
            ),
            const SizedBox(height: 24),
            const Divider(),
            const SizedBox(height: 8),
            Text(
              'Image Validation Rules:',
              style: Theme.of(context).textTheme.titleMedium,
            ),
            const SizedBox(height: 8),
            const Text('• Allowed types: JPG, JPEG, PNG'),
            const Text('• Maximum file size: 4MB'),
          ],
        ),
      ),
    );
  }

  Future<void> _testImageFromCamera(BuildContext context) async {
    final picker = ImagePicker();
    final XFile? image = await picker.pickImage(source: ImageSource.camera);

    if (image != null) {
      _validateAndShowResult(context, image);
    }
  }

  Future<void> _testImageFromGallery(BuildContext context) async {
    final picker = ImagePicker();
    final XFile? image = await picker.pickImage(source: ImageSource.gallery);

    if (image != null) {
      _validateAndShowResult(context, image);
    }
  }

  Future<void> _validateAndShowResult(BuildContext context, XFile image) async {
    final bool isValidExtension = ImageValidator.isValidImageExtension(
      image.path,
    );
    final bool isValidSize = await ImageValidator.isFileSizeValid(image.path);
    final mimeType = ImageValidator.getMimeType(image.path).toString();

    Get.dialog(
      AlertDialog(
        title: const Text('Image Validation Results'),
        content: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            Text('Filename: ${image.name}'),
            const SizedBox(height: 8),
            Text('Path: ${image.path}'),
            const SizedBox(height: 8),
            Text('MIME Type: $mimeType'),
            const SizedBox(height: 16),
            Row(
              children: [
                Text('Valid Extension: '),
                Icon(
                  isValidExtension ? Icons.check_circle : Icons.cancel,
                  color: isValidExtension ? Colors.green : Colors.red,
                ),
              ],
            ),
            const SizedBox(height: 8),
            Row(
              children: [
                Text('Valid Size: '),
                Icon(
                  isValidSize ? Icons.check_circle : Icons.cancel,
                  color: isValidSize ? Colors.green : Colors.red,
                ),
              ],
            ),
            const SizedBox(height: 8),
            Row(
              children: [
                Text('Overall Valid: '),
                Icon(
                  (isValidExtension && isValidSize)
                      ? Icons.check_circle
                      : Icons.cancel,
                  color:
                      (isValidExtension && isValidSize)
                          ? Colors.green
                          : Colors.red,
                ),
              ],
            ),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(),
            child: const Text('Close'),
          ),
        ],
      ),
    );
  }
}
