// custom_menu_icon.dart

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class CustomMenu extends StatefulWidget {
  const CustomMenu({super.key});

  @override
  CustomMenuState createState() => CustomMenuState();
}

class CustomMenuState extends State<CustomMenu> {
  OverlayEntry? _menuOverlay;

  void _toggleMenuOverlay() {
    if (_menuOverlay == null) {
      final RenderBox renderBox = context.findRenderObject() as RenderBox;
      final position = renderBox.localToGlobal(Offset.zero);

      _menuOverlay = OverlayEntry(
        builder: (context) => Stack(
          children: [
            // Detect taps outside the menu
            GestureDetector(
              onTap: _removeMenuOverlay,
              behavior: HitTestBehavior.translucent,
              child: Container(
                color: Colors.transparent,
              ),
            ),
            // Positioned menu overlay
            Positioned(
              top: position.dy + 60,
              left: position.dx + 10,
              child: Material(
                color: Colors.transparent,
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.white.withOpacity(0.95),
                    borderRadius: BorderRadius.circular(12),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black26,
                        blurRadius: 10,
                        offset: Offset(0, 4),
                      ),
                    ],
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      _buildMenuItem('Home', Icons.home, _showHomeDialog),
                      _buildDivider(),
                      _buildMenuItem(
                          'Profile', Icons.person, _showProfileDialog),
                      _buildDivider(),
                      _buildMenuItem(
                          'Settings', Icons.settings, _showSettingsDialog),
                      _buildDivider(),
                      _buildMenuItem('Logout', Icons.logout, _showLogoutDialog),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      );
      Overlay.of(context).insert(_menuOverlay!);
    } else {
      _removeMenuOverlay();
    }
  }

  void _removeMenuOverlay() {
    _menuOverlay?.remove();
    _menuOverlay = null;
  }

  Widget _buildMenuItem(String text, IconData icon, VoidCallback onTap) {
    return InkWell(
      onTap: () {
        _removeMenuOverlay();
        onTap();
      },
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        child: Row(
          children: [
            Icon(icon, size: 24, color: Colors.blueAccent),
            const SizedBox(width: 12),
            Text(
              text,
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildDivider() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Divider(
        color: Colors.grey.shade300,
        thickness: 1,
        height: 1,
      ),
    );
  }

  void _showHomeDialog() {
    _showDialog('Home', AppLocalizations.of(context)!.homeDialogContent);
  }

  void _showProfileDialog() {
    _showDialog('Profile', AppLocalizations.of(context)!.profileDialogContent);
  }

  void _showSettingsDialog() {
    _showDialog(
        'Settings', AppLocalizations.of(context)!.settingsDialogContent);
  }

  void _showLogoutDialog() {
    _showDialog('Logout', AppLocalizations.of(context)!.logoutDialogContent,
        isLogout: true);
  }

  void _showDialog(String title, String content, {bool isLogout = false}) {
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text(title),
          content: Text(content),
          actions: [
            TextButton(
              onPressed: () => Navigator.of(context).pop(),
              child: Text(isLogout ? 'Cancel' : 'Close'),
            ),
            if (isLogout)
              TextButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: Text('Logout', style: TextStyle(color: Colors.red)),
              ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _toggleMenuOverlay,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Image.asset(
          'assets/logo.png',
          height: 40,
          width: 40,
        ),
      ),
    );
  }

  @override
  void dispose() {
    _removeMenuOverlay();
    super.dispose();
  }
}
