import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class FloatingMenuButton extends StatelessWidget {
  const FloatingMenuButton({
    this.popupMenuButton,
    this.drawer,
    this.globalKey,
    this.showGraphsOverlay,
    this.worldTickTimer,
    this.worldReplicateBoidsTimer,
    this.worldReplicateConsumablesTimer,
    super.key,
  });

  final PopupMenuButton? popupMenuButton;
  final Drawer? drawer;
  final GlobalKey<ScaffoldState>? globalKey;
  final bool? showGraphsOverlay;
  final Timer? worldTickTimer;
  final Timer? worldReplicateBoidsTimer;
  final Timer? worldReplicateConsumablesTimer;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: (drawer != null) ? 112 : 56,
      decoration: BoxDecoration(
        color: Theme.of(context).colorScheme.surface,
        borderRadius: BorderRadius.circular(999),
        boxShadow: [
          BoxShadow(
            // check if dark mode
            color: (Theme.of(context).brightness == Brightness.light)
                ? Colors.grey.withOpacity(0.5)
                : Colors.black.withOpacity(0.5),
            spreadRadius: 1,
            blurRadius: 7,
            offset: const Offset(0, 3),
          ),
        ],
      ),
      padding: const EdgeInsets.all(0),
      child: Row(
        children: [
          IconButton(
            // onPressed: () => context.pop(),
            onPressed: () {
              showShadDialog(
                context: context,
                builder: (context) {
                  return ShadDialog(
                    title: const Text('Exit this Simulation'),
                    description: const Text(
                        'Are you sure you want to exit this simulation?'),
                    actions: [
                      ShadButton(
                        text: const Text('Cancel'),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                      ),
                      ShadButton(
                        text: const Text('Exit Simulation'),
                        onPressed: () {
                          if (worldTickTimer != null) {
                            worldTickTimer!.cancel();
                          }
                          if (worldReplicateBoidsTimer != null) {
                            worldReplicateBoidsTimer!.cancel();
                          }
                          if (worldReplicateConsumablesTimer != null) {
                            worldReplicateConsumablesTimer!.cancel();
                          }
                          Navigator.of(context).pop();
                          Navigator.of(context).pop();
                        },
                      ),
                    ],
                  );
                },
              );
            },
            icon: const Icon(
              Icons.chevron_left,
              size: 38,
            ),
          ),
          if (drawer != null)
            IconButton(
              // onPressed: () => context.pop(),
              onPressed: () {
                globalKey!.currentState!.openDrawer();
              },
              icon: const Icon(
                Icons.auto_graph,
                size: 38,
              ),
            ),
          // ShadButton.outline(
          //   icon: const Icon(
          //     Icons.chevron_left,
          //     size: 16,
          //   ),
          //   onPressed: () => Navigator.pop(context),
          // ),
          if (popupMenuButton != null)
            Row(
              children: [
                const SizedBox(width: 8),
                popupMenuButton!,
                const SizedBox(width: 8),
              ],
            )
        ],
      ),
    );
  }
}
