import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sendtrain/providers/action_timer.dart';

Future showGenericDialog(dynamic object, BuildContext parentContext) {
  return showGeneralDialog(
      barrierColor: Colors.black.withOpacity(0.5),
      transitionDuration: const Duration(milliseconds: 220),
      transitionBuilder: (BuildContext context, Animation<double> animation,
          Animation<double> secondaryAnimation, Widget child) {
        Animation<Offset> custom = Tween<Offset>(
                begin: const Offset(0.0, 1.0), end: const Offset(0.0, 0.0))
            .animate(animation);
        return SlideTransition(
            position: custom, child: Dialog.fullscreen(child: object));
      },
      barrierDismissible: true,
      barrierLabel: '',
      context: parentContext,
      pageBuilder: (context, animation1, animation2) {
        return Container();
      });
}

Future showCrudDialog(String title, String content, BuildContext context,
    [Function? callback]) {
  return showAdaptiveDialog(
    context: context,
    builder: (BuildContext context) => AlertDialog(
      title: Text(title),
      content: Text(content),
      actions: <Widget>[
        TextButton(
          onPressed: () => {
            Navigator.pop(context, 'Cancel'),
          },
          child: const Text('Cancel'),
        ),
        TextButton(
          onPressed: () => {
            if (callback != null) {callback()},
            Navigator.pop(context, 'OK')
          },
          child: const Text('OK'),
        ),
      ],
    ),
  );
}

Future showRemovalDialog(String title, String content, BuildContext context,
    [Function? callback]) {
  return showCrudDialog(title, content, context, callback);
}

Future showUpdateDialog(String title, String content, BuildContext context,
    [Function? callback]) {
  return showCrudDialog(title, content, context, callback);
}

// TODO - factor out, this should be more generic
Future<bool?> showBackDialog(BuildContext context) async {
  ActionTimer at = Provider.of<ActionTimer>(context, listen: false);

  if (at.pending || at.complete) {
    await at.clear();
    return true;
  } else {
    return await showDialog<bool>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Are you sure?'),
          content: const Text(
            'Leaving will stop the current activity. Are you sure you want to leave?',
          ),
          actions: <Widget>[
            TextButton(
              style: TextButton.styleFrom(
                textStyle: Theme.of(context).textTheme.labelLarge,
              ),
              child: const Text('Nevermind'),
              onPressed: () async {
                Navigator.pop(context, false);
              },
            ),
            TextButton(
              style: TextButton.styleFrom(
                textStyle: Theme.of(context).textTheme.labelLarge,
              ),
              child: const Text('Leave'),
              onPressed: () async {
                ActionTimer at =
                    Provider.of<ActionTimer>(context, listen: false);
                await at.clear();

                if (context.mounted) {
                  Navigator.pop(context, true);
                }
              },
            ),
          ],
        );
      },
    );
  }
}
