import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:localization/localization.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.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/swipe_module/dialoge/confirm_dialog.dart';
import 'package:test_sa/new_views/swipe_module/dialoge/info_dialog.dart';
import 'package:test_sa/views/full_screen_image_viewer.dart';
import 'package:test_sa/views/widgets/dialogs/confirm_dialog.dart';

import '../controllers/providers/settings/setting_provider.dart';
import '../l10n/app_localizations.dart';

extension BuildContextExtension on BuildContext {
  AppLocalizations get translation => AppLocalizations.of(this)!;

  List<String> get getIssues => [translation.reason1, translation.reason2, translation.reason3, translation.reason4, translation.reason5];

  bool get isDark => Provider.of<SettingProvider>(this).theme == "dark";

  bool get isDarkNotListen => Provider.of<SettingProvider>(this, listen: false).theme == "dark";

  bool get isAr => Provider.of<SettingProvider>(this).language == "ar";

  SettingProvider get settingProvider => Provider.of<SettingProvider>(this, listen: false);

  UserProvider get userProvider => Provider.of<UserProvider>(this, listen: false);

  Future<T?> pushRoute<T>(Widget child) => Navigator.push<T>(this, Platform.isIOS ? CupertinoPageRoute<T>(builder: (context) => child) : MaterialPageRoute<T>(builder: (context) => child));

  void fullScreenImageViewer(Widget child) {
    Navigator.of(this).push(
      CupertinoPageRoute(
        fullscreenDialog: true,
        builder: (_) => FullScreenImageViewer(child),
      ),
    );
  }

  bool isTablet() {
    final mediaQueryData = MediaQuery.of(this);
    final double screenWidth = mediaQueryData.size.width;
    final double screenHeight = mediaQueryData.size.height;
    final double devicePixelRatio = mediaQueryData.devicePixelRatio;
    bool isTablet = (devicePixelRatio < 2 && (screenWidth >= 700 || screenHeight >= 700)) || (devicePixelRatio >= 2 && (screenWidth >= 1000 || screenHeight >= 1000));
    return isTablet;
  }

  void showConfirmDialog(String message, {String? title, VoidCallback? onTap, String? okTitle}) => showDialog(
        context: this,
        builder: (BuildContext cxt) => ConfirmDialog(message: message, onTap: onTap, title: title, okTitle: okTitle),
      );

  void showInfoDialog(String message, {String? title, VoidCallback? onTap, String? okTitle, List<InfoContent> content = const []}) => showDialog(
        context: this,
        builder: (BuildContext cxt) => InfoDialog(message: message, onTap: onTap, title: title, okTitle: okTitle, content: content),
      );

  //
  void showSingleBtnDialog(String message, {String? title, VoidCallback? onTap, String? okTitle}) => showDialog(
        context: this,
        builder: (BuildContext cxt) => Platform.isIOS
            ? CupertinoAlertDialog(
                title: Text(title ?? 'Error'),
                content: Text(message),
                actions: [
                  CupertinoButton(
                    onPressed: onTap ??
                        () {
                          Navigator.pop(cxt);
                        },
                    child: Text(okTitle ?? "Ok"),
                  )
                ],
              )
            : AlertDialog(
                title: Text(title ?? 'Error'),
                content: Text(message),
                actionsAlignment: MainAxisAlignment.center,
                contentPadding: const EdgeInsets.only(left: 24.0, top: 20.0, right: 24.0, bottom: 16.0),
                actions: [
                  FilledButton(
                    style: ButtonStyle(backgroundColor: WidgetStateProperty.all(AppColor.primary10)),
                    onPressed: onTap ??
                        () {
                          Navigator.pop(cxt);
                        },
                    child: Text(okTitle ?? "Ok"),
                  )
                ],
              ),
      );

  Future showBottomSheet(Widget childWidget, {bool? isDismissible, String? title, bool showCancelButton = false}) => showModalBottomSheet(
        context: this,
        useSafeArea: true,
        isScrollControlled: true,
        isDismissible: isDismissible ?? true,
        enableDrag: isDismissible ?? true,
        backgroundColor: Colors.transparent,
        builder: (context) =>
            SingleChildScrollView(padding: const EdgeInsets.all(0), child: childWidget.bottomSafeArea).bottomSheetContainerNew(context, title: title, showCancelButton: showCancelButton),
      );
}
