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

import 'auto_dismiss_menu.dart';
import 'server_action_menu.dart';
import 'server_avatar.dart';

class ServerMenuItem extends StatelessWidget {
  final String serverName;
  final String iconUrl;
  final bool isActive;
  final bool collapsed;


  final Color foregroundActive;
  final Color foregroundInactive;
  final VoidCallback? onTap;
  final VoidCallback? onChangeIcon;
  final VoidCallback? onEdit;
  final VoidCallback? onChangePassword;
  final VoidCallback? onTogglePause;
  final VoidCallback? onDelete;

  const ServerMenuItem({
    super.key,
    required this.serverName,
    required this.isActive,
    required this.collapsed,
    required this.foregroundActive,
    required this.foregroundInactive,
    this.iconUrl = '',
    this.onTap,
    this.onChangeIcon,
    this.onEdit,
    this.onChangePassword,
    this.onTogglePause,
    this.onDelete,
  });

  List<FItem> _menuItems() => buildServerActionMenuItems(
    onChangeIcon: onChangeIcon,
    onEdit: onEdit,
    onChangePassword: onChangePassword,
    onTogglePause: onTogglePause,
    onDelete: onDelete,
  );

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final foreground = isActive ? foregroundActive : foregroundInactive;

    final child = collapsed
        ? _buildCollapsed(foreground)
        : _buildExpanded(theme, foreground);

    final items = _menuItems();
    if (items.isEmpty) return GestureDetector(onTap: onTap, child: child);

    return FContextMenu(
      menuBuilder: autoDismissMenuBuilder,
      menu: [FItemGroup(children: items)],
      child: GestureDetector(
        onTap: onTap,
        child: MouseRegion(cursor: SystemMouseCursors.click, child: child),
      ),
    );
  }

  Widget _buildCollapsed(Color foreground) {
    return Tooltip(
      message: serverName,
      preferBelow: false,
      child: Padding(
        padding: const EdgeInsets.only(bottom: 2),
        child: SizedBox(
          width: 44,
          height: 38,
          child: Row(
            children: [
              Container(
                width: 3,
                height: 20,
                decoration: BoxDecoration(
                  color: isActive ? foregroundActive : Colors.transparent,
                  borderRadius: BorderRadius.circular(1.5),
                ),
              ),
              const Spacer(),
              ServerAvatar(
                name: serverName,
                iconUrl: iconUrl,
                size: 30,
                isActive: isActive,
                showActiveBorder: true,
                shape: ServerAvatarShape.circle,
              ),
              const Spacer(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildExpanded(ThemeData theme, Color foreground) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 8),
      child: Row(
        children: [
          Container(
            width: 2,
            height: 16,
            margin: const EdgeInsets.only(right: 10),
            decoration: BoxDecoration(
              color: isActive ? foregroundActive : Colors.transparent,
              borderRadius: BorderRadius.circular(1),
            ),
          ),
          ServerAvatar(
            name: serverName,
            iconUrl: iconUrl,
            size: 28,
            isActive: isActive,
            showActiveBorder: false,
            shape: ServerAvatarShape.roundedSquare,
          ),
          const SizedBox(width: 10),
          Expanded(
            child: Text(
              serverName,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: theme.textTheme.bodyMedium?.copyWith(
                color: foreground,
                fontWeight: isActive ? FontWeight.w600 : FontWeight.w400,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
