import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:sigap/src/utils/constants/colors.dart';
import 'package:sigap/src/utils/constants/sizes.dart';
import 'package:sigap/src/utils/helpers/helper_functions.dart';

class ImageSourceDialog {
  static Future<void> show({
    required String title,
    required String message,
    required Function(ImageSource source) onSourceSelected,
    bool galleryOption = true,
  }) {
    return Get.dialog(
      Dialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(TSizes.borderRadiusMd),
        ),
        child: Builder(
          builder:
              (context) => Padding(
                padding: const EdgeInsets.all(TSizes.defaultSpace),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(
                      title,
                      style: TextStyle(
                        fontSize: TSizes.fontSizeLg,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: TSizes.md),
                    Text(
                      message,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: TSizes.fontSizeSm,
                        color: TColors.textSecondary,
                      ),
                    ),
                    const SizedBox(height: TSizes.spaceBtwItems),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        _buildImageSourceOption(
                          context: context,
                          icon: Icons.camera_alt,
                          label: 'Camera',
                          onTap: () {
                            onSourceSelected(ImageSource.camera);
                            Get.back();
                          },
                        ),
                        if (galleryOption)
                          _buildImageSourceOption(
                            context: context,
                            icon: Icons.image,
                            label: 'Gallery',
                            onTap: () {
                              onSourceSelected(ImageSource.gallery);
                              Get.back();
                            },
                          ),
                      ],
                    ),
                  ],
                ),
              ),
        ),
      ),
    );
  }

  static Widget _buildImageSourceOption({
    required BuildContext context,
    required IconData icon,
    required String label,
    required VoidCallback onTap,
  }) {
    final isDark = THelperFunctions.isDarkMode(context);
    return GestureDetector(
      onTap: onTap,
      child: Column(
        children: [
          Container(
            padding: const EdgeInsets.all(TSizes.md),
            decoration: BoxDecoration(
              color:
                  isDark
                      ? TColors.cardDark.withOpacity(0.7)
                      : TColors.primary.withOpacity(0.1),
              shape: BoxShape.circle,
            ),
            child: Icon(
              icon,
              color: isDark ? TColors.cardForegroundDark : TColors.primary,
              size: TSizes.iconLg,
            ),
          ),
          const SizedBox(height: TSizes.sm),
          Text(
            label,
            style: TextStyle(
              color: isDark ? TColors.cardForegroundDark : TColors.primary,
            ),
          ),
        ],
      ),
    );
  }
}
