import 'package:flutter/material.dart';
import '../../../l10n/l10n_x.dart';
import '../../../shared/widgets/empty_state_widget.dart';

class StateView extends StatelessWidget {
  final bool isLoading;
  final Object? error;
  final VoidCallback? onRetry;
  final bool isEmpty;
  final Widget child;
  final Widget? loading;
  final String? emptyTitle;
  final String? emptyMessage;
  final EmptyStateType emptyType;

  const StateView({
    super.key,
    required this.child,
    this.isLoading = false,
    this.error,
    this.onRetry,
    this.isEmpty = false,
    this.loading,
    this.emptyTitle,
    this.emptyMessage,
    this.emptyType = EmptyStateType.noResults,
  });

  @override
  Widget build(BuildContext context) {
    if (isLoading) {
      return loading ??
          const Center(
            child: CircularProgressIndicator(),
          );
    }

    if (error != null) {
      final l = context.l10n;
      return Center(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Icon(Icons.error_outline, size: 44, color: Theme.of(context).colorScheme.error),
              const SizedBox(height: 12),
              Text(
                l.stateSomethingWentWrong,
                style: Theme.of(context).textTheme.titleMedium,
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 8),
              Text(
                error.toString(),
                style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                      color: Theme.of(context).colorScheme.onSurfaceVariant,
                    ),
                textAlign: TextAlign.center,
              ),
              if (onRetry != null) ...[
                const SizedBox(height: 16),
                FilledButton(
                  onPressed: onRetry,
                  child: Text(l.retry),
                )
              ]
            ],
          ),
        ),
      );
    }

    if (isEmpty) {
      final l = context.l10n;
      return Center(
        child: EmptyStateWidget(
          type: emptyType,
          customTitle: emptyTitle,
          customMessage: emptyMessage,
          actionLabel: onRetry != null ? l.retry : null,
          onActionPressed: onRetry,
        ),
      );
    }

    return child;
  }
}

