import 'package:core_localization/core_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import '../blocs/client_home_state.dart';
import '../navigation/client_home_navigator.dart';
import '../widgets/actions_widget.dart';
import '../widgets/coverage_widget.dart';
import '../widgets/draggable_widget_wrapper.dart';
import '../widgets/live_activity_widget.dart';
import '../widgets/reorder_widget.dart';
import '../widgets/spending_widget.dart';

/// A widget that builds dashboard content based on widget ID.
///
/// This widget encapsulates the logic for rendering different dashboard
/// widgets based on their unique identifiers and current state.
class DashboardWidgetBuilder extends StatelessWidget {
  /// The unique identifier for the widget to build.
  final String id;

  /// The current dashboard state.
  final ClientHomeState state;

  /// Whether the widget is in edit mode.
  final bool isEditMode;

  /// Creates a [DashboardWidgetBuilder].
  const DashboardWidgetBuilder({
    required this.id,
    required this.state,
    required this.isEditMode,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final TranslationsClientHomeWidgetsEn i18n = t.client_home.widgets;
    final Widget widgetContent = _buildWidgetContent(context);

    if (isEditMode) {
      return DraggableWidgetWrapper(
        id: id,
        title: _getWidgetTitle(i18n),
        isVisible: state.widgetVisibility[id] ?? true,
        child: widgetContent,
      );
    }

    // Hide widget if not visible in normal mode
    if (!(state.widgetVisibility[id] ?? true)) {
      return const SizedBox.shrink();
    }

    return widgetContent;
  }

  /// Builds the actual widget content based on the widget ID.
  Widget _buildWidgetContent(BuildContext context) {
    switch (id) {
      case 'actions':
        return ActionsWidget(
          onRapidPressed: () => Modular.to.pushRapidOrder(),
          onCreateOrderPressed: () => Modular.to.pushCreateOrder(),
        );
      case 'reorder':
        return ReorderWidget(
          orders: state.reorderItems,
          onReorderPressed: (Map<String, dynamic> data) {
            ClientHomeSheets.showOrderFormSheet(
              context,
              data,
              onSubmit: (Map<String, dynamic> submittedData) {
                // Handle form submission if needed
              },
            );
          },
        );
      case 'spending':
        return SpendingWidget(
          weeklySpending: state.dashboardData.weeklySpending,
          next7DaysSpending: state.dashboardData.next7DaysSpending,
          weeklyShifts: state.dashboardData.weeklyShifts,
          next7DaysScheduled: state.dashboardData.next7DaysScheduled,
        );
      case 'coverage':
        return CoverageWidget(
          totalNeeded: state.dashboardData.totalNeeded,
          totalConfirmed: state.dashboardData.totalFilled,
          coveragePercent: state.dashboardData.totalNeeded > 0
              ? ((state.dashboardData.totalFilled /
                            state.dashboardData.totalNeeded) *
                        100)
                    .toInt()
              : 0,
        );
      case 'liveActivity':
        return LiveActivityWidget(onViewAllPressed: () {});
      default:
        return const SizedBox.shrink();
    }
  }

  /// Returns the display title for the widget based on its ID.
  String _getWidgetTitle(dynamic i18n) {
    switch (id) {
      case 'actions':
        return i18n.actions;
      case 'reorder':
        return i18n.reorder;
      case 'coverage':
        return i18n.coverage;
      case 'spending':
        return i18n.spending;
      case 'liveActivity':
        return i18n.live_activity;
      default:
        return '';
    }
  }
}
