import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../blocs/client_home_bloc.dart';
import '../blocs/client_home_event.dart';

/// A wrapper for dashboard widgets in edit mode.
///
/// Displays drag handles, visibility toggles, and wraps the actual widget
/// content with appropriate styling for the edit state.
class DraggableWidgetWrapper extends StatelessWidget {

  /// Creates a [DraggableWidgetWrapper].
  const DraggableWidgetWrapper({
    required this.id,
    required this.title,
    required this.child,
    required this.isVisible,
    super.key,
  });
  /// The unique identifier for this widget.
  final String id;

  /// The display title for this widget.
  final String title;

  /// The actual widget content to wrap.
  final Widget child;

  /// Whether this widget is currently visible.
  final bool isVisible;

  @override
  Widget build(BuildContext context) {
    return Column(
      spacing: UiConstants.space2,
      children: <Widget>[
        Row(
          children: <Widget>[
            Container(
              padding: const EdgeInsets.symmetric(
                horizontal: UiConstants.space2,
                vertical: UiConstants.space1,
              ),
              decoration: BoxDecoration(
                color: UiColors.white,
                borderRadius: UiConstants.radiusMd,
                border: Border.all(color: UiColors.border),
              ),
              child: Row(
                children: <Widget>[
                  const Icon(
                    UiIcons.gripVertical,
                    size: 14,
                    color: UiColors.iconSecondary,
                  ),
                  const SizedBox(width: UiConstants.space2),
                  Text(title, style: UiTypography.footnote1m),
                ],
              ),
            ),
            const SizedBox(width: UiConstants.space2),
            GestureDetector(
              onTap: () => BlocProvider.of<ClientHomeBloc>(
                context,
              ).add(ClientHomeWidgetVisibilityToggled(id)),
              child: Container(
                padding: const EdgeInsets.all(UiConstants.space1),
                decoration: BoxDecoration(
                  color: UiColors.white,
                  borderRadius: UiConstants.radiusMd,
                  border: Border.all(color: UiColors.border),
                ),
                child: Icon(
                  isVisible ? UiIcons.success : UiIcons.error,
                  size: 14,
                  color: isVisible ? UiColors.primary : UiColors.iconSecondary,
                ),
              ),
            ),
          ],
        ),

        // Widget content
        Opacity(
          opacity: isVisible ? 1.0 : 0.4,
          child: IgnorePointer(
            ignoring: !isVisible,
            child: child,
          ),
        ),
      ],
    );
  }
}
