import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:sefer_driver/constant/colors.dart';
import 'package:sefer_driver/constant/style.dart';
import 'package:sefer_driver/controller/functions/tts.dart';

class DialogConfig {
  static const Duration animationDuration = Duration(milliseconds: 200);
  static const double blurStrength = 8.0;
  static const double cornerRadius = 14.0;
  static final BoxDecoration decoration = BoxDecoration(
    borderRadius: BorderRadius.circular(cornerRadius),
    boxShadow: [
      BoxShadow(
        color: Colors.black.withAlpha(38), // 0.15 opacity
        blurRadius: 16,
        offset: const Offset(0, 8),
      ),
    ],
  );
}

class MyDialog extends GetxController {
  void getDialog(String title, String? midTitle, VoidCallback onPressed) {
    final textToSpeechController = Get.put(TextToSpeechController());

    HapticFeedback.mediumImpact();

    Get.dialog(
      TweenAnimationBuilder<double>(
        duration: DialogConfig.animationDuration,
        tween: Tween(begin: 0.0, end: 1.0),
        builder: (context, value, child) {
          return Transform.scale(
            scale: 0.95 + (0.05 * value),
            child: Opacity(opacity: value, child: child),
          );
        },
        child: BackdropFilter(
          filter: ImageFilter.blur(
            sigmaX: DialogConfig.blurStrength,
            sigmaY: DialogConfig.blurStrength,
          ),
          child: Builder(builder: (context) {
            final theme = Theme.of(context);
            return CupertinoAlertDialog(
              title: Column(
                children: [
                  Text(
                    title.tr,
                    style: AppStyle.title.copyWith(
                      fontSize: 20,
                      fontWeight: FontWeight.w700,
                      letterSpacing: -0.5,
                      color: AppColor.primaryColor,
                    ),
                  ),
                  const SizedBox(height: 8),
                ],
              ),
              content: Column(
                children: [
                  CupertinoButton(
                    padding: const EdgeInsets.all(8),
                    onPressed: () async {
                      HapticFeedback.selectionClick();
                      await textToSpeechController.speakText(title);
                      await textToSpeechController.speakText(midTitle!);
                    },
                    child: Container(
                      padding: const EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color:
                            AppColor.primaryColor.withAlpha(26), // 0.1 opacity
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Icon(
                        CupertinoIcons.speaker_2_fill,
                        color: AppColor.primaryColor,
                        size: 24,
                      ),
                    ),
                  ),
                  const SizedBox(height: 8),
                  Text(
                    midTitle!,
                    style: AppStyle.title.copyWith(
                      fontSize: 16,
                      height: 1.3,
                      color: theme.textTheme.bodyLarge?.color ?? AppColor.writeColor,
                    ),
                    textAlign: TextAlign.center,
                  ),
                ],
              ),
              actions: [
                CupertinoDialogAction(
                  onPressed: () {
                    HapticFeedback.lightImpact();
                    Navigator.of(context, rootNavigator: true).pop();
                  },
                  child: Text(
                    'Cancel'.tr,
                    style: TextStyle(
                      color: AppColor.redColor,
                      fontWeight: FontWeight.w600,
                      fontSize: 17,
                    ),
                  ),
                ),
                CupertinoDialogAction(
                  onPressed: () {
                    HapticFeedback.mediumImpact();
                    Navigator.of(context, rootNavigator: true).pop();
                    onPressed();
                  },
                  child: Text(
                    'OK'.tr,
                    style: TextStyle(
                      color: AppColor.greenColor,
                      fontWeight: FontWeight.w600,
                      fontSize: 17,
                    ),
                  ),
                ),
              ],
            );
          }),
        ),
      ),

      barrierDismissible: true,
      barrierColor: Colors.black.withAlpha(102), // 0.4 opacity
    );
  }
}

class MyDialogContent extends GetxController {
  void getDialog(String title, Widget? content, VoidCallback onPressed) {
    final textToSpeechController = Get.put(TextToSpeechController());

    HapticFeedback.mediumImpact();

    Get.dialog(
      TweenAnimationBuilder<double>(
        duration: DialogConfig.animationDuration,
        tween: Tween(begin: 0.0, end: 1.0),
        builder: (context, value, child) {
          return Transform.scale(
            scale: 0.95 + (0.05 * value),
            child: Opacity(opacity: value, child: child),
          );
        },
        child: BackdropFilter(
          filter: ImageFilter.blur(
            sigmaX: DialogConfig.blurStrength,
            sigmaY: DialogConfig.blurStrength,
          ),
          child: Builder(builder: (context) {
            return CupertinoAlertDialog(
              title: Column(
                children: [
                  Text(
                    title,
                    style: AppStyle.title.copyWith(
                      fontSize: 20,
                      fontWeight: FontWeight.w700,
                      letterSpacing: -0.5,
                      color: AppColor.primaryColor,
                    ),
                  ),
                  const SizedBox(height: 8),
                ],
              ),
              content: Column(
                children: [
                  CupertinoButton(
                    padding: const EdgeInsets.all(8),
                    onPressed: () async {
                      HapticFeedback.selectionClick();
                      await textToSpeechController.speakText(title);
                    },
                    child: Container(
                      padding: const EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color:
                            AppColor.primaryColor.withAlpha(26), // 0.1 opacity
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Icon(
                        CupertinoIcons.headphones,
                        color: AppColor.primaryColor,
                        size: 24,
                      ),
                    ),
                  ),
                  const SizedBox(height: 12),
                  content!,
                ],
              ),
              actions: [
                CupertinoDialogAction(
                  onPressed: () {
                    HapticFeedback.lightImpact();
                    Navigator.of(context, rootNavigator: true).pop();
                  },
                  child: Text(
                    'Cancel',
                    style: TextStyle(
                      color: AppColor.redColor,
                      fontWeight: FontWeight.w600,
                      fontSize: 17,
                    ),
                  ),
                ),
                CupertinoDialogAction(
                  onPressed: () {
                    HapticFeedback.mediumImpact();
                    Navigator.of(context, rootNavigator: true).pop();
                    onPressed();
                  },
                  child: Text(
                    'OK'.tr,
                    style: TextStyle(
                      color: AppColor.greenColor,
                      fontWeight: FontWeight.w600,
                      fontSize: 17,
                    ),
                  ),
                ),
              ],
            );
          }),
        ),
      ),

      barrierDismissible: true,
      barrierColor: Colors.black.withAlpha(102), // 0.4 opacity
    );
  }
}
