import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:open_file/open_file.dart';
import 'package:share_plus/share_plus.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
import 'package:test_sa/views/widgets/item_views/info_header_widget.dart';

/// Reusable success dialog for file operations (export, print, etc.)
class FileSuccessDialog {
  /// Shows a success dialog with Open and Share options
  ///
  /// [context] - The build context
  /// [filePath] - Path to the successfully generated file
  /// [title] - Dialog title (e.g., 'Export Successful', 'Print Successful')
  /// [message] - Dialog message (e.g., 'Your file has been exported successfully')
  /// [logPrefix] - Prefix for log messages (e.g., 'exported', 'printed')
  static Future<void> show({
    required BuildContext context,
    required String filePath,
    required String title,
    required String message,
    String logPrefix = 'file',
  }) async {
    return showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext dialogContext) {
        return Dialog(
          backgroundColor: AppColor.background(context),
          shape: RoundedRectangleBorder(
            side: BorderSide(color: AppColor.borderColor(context)),
            borderRadius: BorderRadius.circular(14),
          ),
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Align(
                  alignment: Alignment.topRight,
                  child: 'clear'.toSvgAsset(color: AppColor.icon2Color(context), width: 18, height: 18).onPress(() {
                    Navigator.of(dialogContext).pop();
                  }),
                ),
                'done'.toSvgAsset(color: AppColor.green70, width: 56, height: 56),
                const SizedBox(height: 24),
                title.heading5(context).custom(fontWeight: FontWeight.w600, color: AppColor.textStyleColor(context)),
                const SizedBox(height: 12),
                message.heading6(context).custom(align: TextAlign.center, fontWeight: FontWeight.w500, color: AppColor.lightTextColor(context)),
                const SizedBox(height: 32),
                // Action buttons
                Row(
                  children: [
                    // Open button
                    Expanded(
                      child: AppFilledButton(
                        onPressed: () async {
                          log('📂 Opening $logPrefix file...');
                          final result = await OpenFile.open(filePath);
                          if (result.type != ResultType.done) {
                            log('⚠️ Could not open file: ${result.message}');
                            'No app found to open this file type. File saved to: $filePath'.showToast;
                          }
                        },
                        label: 'Open',
                        // buttonColor: AppColor.primary10,
                        showIcon: true,
                        icon: Icon(Icons.folder_open, color: AppColor.background(context)),
                        height: 48,
                      ),
                    ),
                    const SizedBox(width: 12),
                    // Share button
                    Expanded(
                      child: AppFilledButton(
                        onPressed: () async {
                          log('📤 Sharing $logPrefix file...');
                          try {
                            final xFile = XFile(filePath);
                            await Share.shareXFiles(
                              [xFile],
                              text: '${title.replaceAll(' Successful', '')} file',
                            );
                            log('✅ File shared successfully');
                          } catch (e) {
                            log('❌ Error sharing file: $e');
                            'Failed to share file: $e'.showToast;
                          }
                        },
                        label: 'Share',
                        buttonColor: AppColor.green40,
                        textColor: AppColor.green60,
                        showIcon: true,
                        icon: const Icon(Icons.share, color: AppColor.green60),
                        height: 48,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
